diff --git a/web/api/legacy/api_test.go b/web/api/legacy/api_test.go index 2ecb7f9df..34bf764ef 100644 --- a/web/api/legacy/api_test.go +++ b/web/api/legacy/api_test.go @@ -55,20 +55,25 @@ func TestQuery(t *testing.T) { status: http.StatusOK, bodyRe: `{"type":"error","value":"Parse error at char 1: no expression found in input","version":1}`, }, + { + queryStr: "expr=1.4", + status: http.StatusOK, + bodyRe: `{"type":"scalar","value":"1.4","version":1}`, + }, { queryStr: "expr=testmetric", status: http.StatusOK, - bodyRe: `{"type":"vector","value":\[\{"metric":{"__name__":"testmetric"},"value":"0","timestamp":\d+\.\d+}\],"version":1\}`, + bodyRe: `{"type":"vector","value":\[{"metric":{"__name__":"testmetric"},"value":"0","timestamp":\d+\.\d+}\],"version":1}`, }, { queryStr: "expr=testmetric×tamp=" + testTimestamp.String(), status: http.StatusOK, - bodyRe: `{"type":"vector","value":\[\{"metric":{"__name__":"testmetric"},"value":"0","timestamp":` + testTimestamp.String() + `}\],"version":1\}`, + bodyRe: `{"type":"vector","value":\[{"metric":{"__name__":"testmetric"},"value":"0","timestamp":` + testTimestamp.String() + `}\],"version":1}`, }, { queryStr: "expr=testmetric×tamp=" + testTimestamp.Add(-time.Hour).String(), status: http.StatusOK, - bodyRe: `{"type":"vector","value":\[\],"version":1\}`, + bodyRe: `{"type":"vector","value":\[\],"version":1}`, }, { queryStr: "timestamp=invalid", diff --git a/web/api/legacy/query.go b/web/api/legacy/query.go index 511d75bc8..d0e2a9fac 100644 --- a/web/api/legacy/query.go +++ b/web/api/legacy/query.go @@ -133,11 +133,16 @@ func (pv plainVec) String() string { // which does not fit the response format for the legacy API. type plainScalar promql.Scalar -func (pv plainScalar) Type() promql.ExprType { +func (ps plainScalar) MarshalJSON() ([]byte, error) { + s := strconv.FormatFloat(float64(ps.Value), 'f', -1, 64) + return json.Marshal(&s) +} + +func (plainScalar) Type() promql.ExprType { return promql.ExprScalar } -func (pv plainScalar) String() string { +func (plainScalar) String() string { return "" }