diff --git a/CHANGELOG.md b/CHANGELOG.md index f32e39e4efe..ed47a565883 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ * [BUGFIX] Ring: Fix `DoBatch` counting a key more than once when more replicas than the quorum succeed, which in rare cases could hide a write error from a key that did not reach quorum. A distributor push now always waits until every series reaches quorum, so it returns the error of a series that misses quorum, and takes as long as its slowest series to reach quorum. #7975 * [BUGFIX] Compactor: Stop downloading the parquet files and the `parquet-converter-mark.json` and `parquet-no-convert-mark.json` markers written by the parquet converter under a block's prefix. Compaction never reads them, so they only used local disk and bandwidth until the compaction job ended. #7972 * [BUGFIX] Ring: Fix shuffle-sharding subrings treating instances with a stale heartbeat as healthy when using the DynamoDB KV store. #7976 +* [BUGFIX] Querier: Return `400 bad_data` instead of `500 server_error` for an instant query with an invalid `time` parameter. #7980 ## 1.22.0 2026-11-19 diff --git a/pkg/api/queryapi/query_api_test.go b/pkg/api/queryapi/query_api_test.go index 7dd13ba7549..682b4e3a057 100644 --- a/pkg/api/queryapi/query_api_test.go +++ b/pkg/api/queryapi/query_api_test.go @@ -258,6 +258,40 @@ func Test_InvalidCodec(t *testing.T) { require.Equal(t, http.StatusNotAcceptable, rec.Code) } +func Test_InvalidParamReturnsBadRequest(t *testing.T) { + engine := engine2.New( + promql.EngineOpts{ + MaxSamples: 100, + Timeout: time.Second * 2, + }, + engine2.ThanosEngineConfig{Enabled: false}, + prometheus.NewRegistry()) + + queryAPI := NewQueryAPI(engine, &mockSampleAndChunkQueryable{}, querier.StatsRenderer, log.NewNopLogger(), []v1.Codec{v1.JSONCodec{}}, regexp.MustCompile(".*"), stats.PhaseTrackerConfig{}) + router := mux.NewRouter() + router.Path("/api/v1/query").Methods("POST").Handler(queryAPI.Wrap(queryAPI.InstantQueryHandler)) + router.Path("/api/v1/query_range").Methods("POST").Handler(queryAPI.Wrap(queryAPI.RangeQueryHandler)) + + for _, path := range []string{ + "/api/v1/query?query=test&time=invalid", + "/api/v1/query_range?query=test&start=invalid&end=1&step=1", + "/api/v1/query_range?query=test&start=0&end=invalid&step=1", + "/api/v1/query_range?query=test&start=0&end=1&step=invalid", + } { + t.Run(path, func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, path, nil) + ctx := context.Background() + _, ctx = stats.ContextWithEmptyStats(ctx) + req = req.WithContext(user.InjectOrgID(ctx, "user1")) + + rec := httptest.NewRecorder() + router.ServeHTTP(rec, req) + require.Equal(t, http.StatusBadRequest, rec.Code) + require.Contains(t, rec.Body.String(), `"errorType":"bad_data"`) + }) + } +} + func Test_CustomAPI_StatsRenderer(t *testing.T) { engine := engine2.New( promql.EngineOpts{ diff --git a/pkg/api/queryapi/util.go b/pkg/api/queryapi/util.go index ea35f1b6ee4..eb25c1c1ab9 100644 --- a/pkg/api/queryapi/util.go +++ b/pkg/api/queryapi/util.go @@ -7,10 +7,10 @@ import ( "net/http" "time" - "github.com/gogo/status" "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/util/annotations" "github.com/weaveworks/common/httpgrpc" + "google.golang.org/grpc/status" "github.com/cortexproject/cortex/pkg/util" ) diff --git a/pkg/api/queryapi/util_test.go b/pkg/api/queryapi/util_test.go index f3caf3ec8e7..3ad652bb2d0 100644 --- a/pkg/api/queryapi/util_test.go +++ b/pkg/api/queryapi/util_test.go @@ -1,10 +1,14 @@ package queryapi import ( + "errors" + "net/http" "testing" "time" + pkgerrors "github.com/pkg/errors" "github.com/stretchr/testify/require" + "github.com/weaveworks/common/httpgrpc" ) func Test_Convert(t *testing.T) { @@ -13,3 +17,42 @@ func Test_Convert(t *testing.T) { require.Equal(t, time, convertMsToTime(time).UnixMilli()) require.Equal(t, time, convertMsToDuration(time).Milliseconds()) } + +func Test_DecorateWithParamName(t *testing.T) { + badRequest := httpgrpc.Errorf(http.StatusBadRequest, "cannot parse %q to a valid timestamp", "invalid") + + for name, tc := range map[string]struct { + err error + expectedCode int32 + expectedMsg string + }{ + "httpgrpc error": { + err: badRequest, + expectedCode: http.StatusBadRequest, + expectedMsg: `invalid parameter "time"; cannot parse "invalid" to a valid timestamp`, + }, + "wrapped httpgrpc error": { + err: pkgerrors.Wrapf(badRequest, "Invalid time value for '%s'", "time"), + expectedCode: http.StatusBadRequest, + expectedMsg: `invalid parameter "time"; Invalid time value for 'time': rpc error: code = Code(400) desc = cannot parse "invalid" to a valid timestamp`, + }, + "plain error": { + err: errors.New("something went wrong"), + expectedMsg: `invalid parameter "time"; something went wrong`, + }, + } { + t.Run(name, func(t *testing.T) { + err := DecorateWithParamName(tc.err, "time") + + resp, ok := httpgrpc.HTTPResponseFromError(err) + if tc.expectedCode == 0 { + require.False(t, ok) + require.EqualError(t, err, tc.expectedMsg) + return + } + require.True(t, ok) + require.Equal(t, tc.expectedCode, resp.Code) + require.Equal(t, tc.expectedMsg, string(resp.Body)) + }) + } +}