mirror of
https://github.com/prometheus/prometheus.git
synced 2025-01-22 19:26:56 -08:00
web/api: improve errors, add tests
This commit is contained in:
parent
cb10ceac18
commit
7be94ce962
|
@ -112,7 +112,13 @@ func (api *API) query(r *http.Request) (interface{}, *apiError) {
|
|||
|
||||
res := qry.Exec()
|
||||
if res.Err != nil {
|
||||
return nil, &apiError{errorBadData, res.Err}
|
||||
switch res.Err.(type) {
|
||||
case promql.ErrQueryCanceled:
|
||||
return nil, &apiError{errorCanceled, res.Err}
|
||||
case promql.ErrQueryTimeout:
|
||||
return nil, &apiError{errorTimeout, res.Err}
|
||||
}
|
||||
return nil, &apiError{errorExec, res.Err}
|
||||
}
|
||||
return &queryData{
|
||||
ResultType: res.Value.Type(),
|
||||
|
@ -143,18 +149,18 @@ func (api *API) queryRange(r *http.Request) (interface{}, *apiError) {
|
|||
|
||||
qry, err := api.QueryEngine.NewRangeQuery(r.FormValue("query"), start, end, step)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case promql.ErrQueryCanceled:
|
||||
return nil, &apiError{errorCanceled, err}
|
||||
case promql.ErrQueryTimeout:
|
||||
return nil, &apiError{errorTimeout, err}
|
||||
}
|
||||
return nil, &apiError{errorExec, err}
|
||||
return nil, &apiError{errorBadData, err}
|
||||
}
|
||||
|
||||
res := qry.Exec()
|
||||
if res.Err != nil {
|
||||
return nil, &apiError{errorBadData, err}
|
||||
switch res.Err.(type) {
|
||||
case promql.ErrQueryCanceled:
|
||||
return nil, &apiError{errorCanceled, res.Err}
|
||||
case promql.ErrQueryTimeout:
|
||||
return nil, &apiError{errorTimeout, res.Err}
|
||||
}
|
||||
return nil, &apiError{errorExec, res.Err}
|
||||
}
|
||||
return &queryData{
|
||||
ResultType: res.Value.Type(),
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
clientmodel "github.com/prometheus/client_golang/model"
|
||||
|
||||
"github.com/prometheus/prometheus/promql"
|
||||
"github.com/prometheus/prometheus/storage/metric"
|
||||
"github.com/prometheus/prometheus/util/route"
|
||||
)
|
||||
|
||||
|
@ -90,6 +91,82 @@ func TestEndpoints(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"start": []string{"0"},
|
||||
"end": []string{"2"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
response: &queryData{
|
||||
ResultType: promql.ExprMatrix,
|
||||
Result: promql.Matrix{
|
||||
&promql.SampleStream{
|
||||
Values: metric.Values{
|
||||
{Value: 0, Timestamp: start},
|
||||
{Value: 1, Timestamp: start.Add(1 * time.Second)},
|
||||
{Value: 2, Timestamp: start.Add(2 * time.Second)},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
// Missing query params in range queries.
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"end": []string{"2"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"start": []string{"0"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"time()"},
|
||||
"start": []string{"0"},
|
||||
"end": []string{"2"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
// Missing evaluation time.
|
||||
{
|
||||
endpoint: api.query,
|
||||
query: url.Values{
|
||||
"query": []string{"0.333"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
// Bad query expression.
|
||||
{
|
||||
endpoint: api.query,
|
||||
query: url.Values{
|
||||
"query": []string{"invalid][query"},
|
||||
"time": []string{"1970-01-01T01:02:03+01:00"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.queryRange,
|
||||
query: url.Values{
|
||||
"query": []string{"invalid][query"},
|
||||
"start": []string{"0"},
|
||||
"end": []string{"100"},
|
||||
"step": []string{"1"},
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.labelValues,
|
||||
params: map[string]string{
|
||||
|
@ -100,13 +177,6 @@ func TestEndpoints(t *testing.T) {
|
|||
"test_metric2",
|
||||
},
|
||||
},
|
||||
{
|
||||
endpoint: api.labelValues,
|
||||
params: map[string]string{
|
||||
"name": "not!!!allowed",
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
{
|
||||
endpoint: api.labelValues,
|
||||
params: map[string]string{
|
||||
|
@ -117,6 +187,14 @@ func TestEndpoints(t *testing.T) {
|
|||
"boo",
|
||||
},
|
||||
},
|
||||
// Bad name parameter.
|
||||
{
|
||||
endpoint: api.labelValues,
|
||||
params: map[string]string{
|
||||
"name": "not!!!allowed",
|
||||
},
|
||||
errType: errorBadData,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
@ -147,7 +225,7 @@ func TestEndpoints(t *testing.T) {
|
|||
t.Fatalf("Expected error of type %q but got none", test.errType)
|
||||
}
|
||||
if !reflect.DeepEqual(resp, test.response) {
|
||||
t.Fatalf("Response does not match, expected:\n%v\ngot:\n%v", test.response, resp)
|
||||
t.Fatalf("Response does not match, expected:\n%#v\ngot:\n%#v", test.response, resp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue