Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix(querier): stop zero-start OOM and reject malformed time windows (#13246)

Tushar Vatsed50e4aed1fe
diff --git a/pkg/querier/api.go b/pkg/querier/api.go
index 7f7e028761..ae0e281015 100644
--- a/pkg/querier/api.go
+++ b/pkg/querier/api.go
@@ -140,7 +140,9 @@ func (handler *handler) QueryRawStream(rw http.ResponseWriter, req *http.Request
}
// create the v5 request param
queryRangeRequest := qbtypes.QueryRangeRequest{
- Start: start,
+ // Coerce the window to epoch milliseconds up front (End may be 0 for the
+ // open-ended stream, which ToMilliSecs leaves untouched).
+ Start: qbtypes.ToMilliSecs(start),
RequestType: qbtypes.RequestTypeRawStream,
CompositeQuery: qbtypes.CompositeQuery{
Queries: []qbtypes.QueryEnvelope{
diff --git a/pkg/querier/list_range.go b/pkg/querier/list_range.go
index 48a129327f..94f772b4e7 100644
--- a/pkg/querier/list_range.go
+++ b/pkg/querier/list_range.go
@@ -11,6 +11,12 @@ func makeBuckets(start, end uint64) []tsRange {
startNS := querybuilder.ToNanoSecs(start)
endNS := querybuilder.ToNanoSecs(end)
+ // A cursor that reaches the window boundary can leave end <= start; the
+ // subtractions below are unsigned and would wrap.
+ if endNS <= startNS {
+ return nil
+ }
+
if endNS-startNS <= uint64(hourNanos) {
return []tsRange{{fromNS: startNS, toNS: endNS}}
}
@@ -20,9 +26,9 @@ func makeBuckets(start, end uint64) []tsRange {
curEnd := endNS
for {
- curStart := curEnd - bucket
- if curStart < startNS {
- curStart = startNS
+ curStart := startNS
+ if curEnd-startNS > bucket {
+ curStart = curEnd - bucket
}
out = append(out, tsRange{fromNS: curStart, toNS: curEnd})
diff --git a/pkg/querier/querier.go b/pkg/querier/querier.go
index 9b2c8a5fed..3fa5b2c354 100644
--- a/pkg/querier/querier.go
+++ b/pkg/querier/querier.go
@@ -111,11 +111,10 @@ func New(
func (q *querier) QueryRange(ctx context.Context, orgID valuer.UUID, req *qbtypes.QueryRangeRequest) (*qbtypes.QueryRangeResponse, error) {
- // Coerce the window to epoch milliseconds up front so every downstream
- // consumer (TimeRange, narrowWindowByTraceID, step interval, etc.) can
- // safely assume ms regardless of the resolution the caller sent.
- req.Start = querybuilder.ToMilliSecs(req.Start)
- req.End = querybuilder.ToMilliSecs(req.End)
+ // Callers that build the request in-process never pass through
+ // UnmarshalJSON, so normalise here too.
+ req.Start = qbtypes.ToMilliSecs(req.Start)
+ req.End = qbtypes.ToMilliSecs(req.End)
event := &qbtypes.QBEvent{
Version: "v5",
@@ -534,10 +533,8 @@ func familyTemporality(temporalities map[string]metrictypes.Temporality, names [
func (q *querier) QueryRawStream(ctx context.Context, orgID valuer.UUID, req *qbtypes.QueryRangeRequest, client *qbtypes.RawStream) {
- // Coerce the window to epoch milliseconds up front (End may be 0 for the
- // open-ended stream, which ToMilliSecs leaves untouched).
- req.Start = querybuilder.ToMilliSecs(req.Start)
- req.End = querybuilder.ToMilliSecs(req.End)
+ req.Start = qbtypes.ToMilliSecs(req.Start)
+ req.End = qbtypes.ToMilliSecs(req.End)
event := &qbtypes.QBEvent{
Version: "v5",
diff --git a/pkg/querybuilder/time.go b/pkg/querybuilder/time.go
index 641095dcfa..f1a3b3b554 100644
--- a/pkg/querybuilder/time.go
+++ b/pkg/querybuilder/time.go
@@ -33,28 +33,6 @@ func ToNanoSecs(epoch uint64) uint64 {
return temp * uint64(math.Pow(10, float64(19-count)))
}
-// ToMilliSecs takes an epoch whose resolution is inferred from its magnitude
-// (s/ms/µs/ns) and returns it in milliseconds. A millisecond epoch for the
-// current era has 13 digits (e.g. ~1.7e12 in 2026), so the value is scaled so
-// its digit-width matches: smaller values (seconds) are scaled up, larger ones
-// (micro/nanoseconds) are scaled down. Zero is returned unchanged.
-func ToMilliSecs(epoch uint64) uint64 {
- if epoch == 0 {
- return 0
- }
- temp := epoch
- count := 0
- for epoch != 0 {
- epoch /= 10
- count++
- }
- const msDigits = 13
- if count < msDigits {
- return temp * uint64(math.Pow(10, float64(msDigits-count)))
- }
- return temp / uint64(math.Pow(10, float64(count-msDigits)))
-}
-
// TODO(srikanthccv): should these be rounded to nearest multiple of 60 instead of 5 if step > 60?
// That would make graph look nice but "nice" but should be less important than the usefulness.
func RecommendedStepInterval(start, end uint64) uint64 {
diff --git a/pkg/querybuilder/time_test.go b/pkg/querybuilder/time_test.go
index 551f0d0ff3..7617d93fa4 100644
--- a/pkg/querybuilder/time_test.go
+++ b/pkg/querybuilder/time_test.go
@@ -60,51 +60,3 @@ func TestToNanoSecs(t *testing.T) {
})
}
}
-
-func TestToMilliSecs(t *testing.T) {
- tests := []struct {
- name string
- epoch uint64
- expected uint64
- }{
- {
- name: "10-digit Unix timestamp (seconds) - 2023-01-01 00:00:00 UTC",
- epoch: 1672531200, // seconds
- expected: 1672531200000, // * 10^3
- },
- {
- name: "13-digit Unix timestamp (milliseconds) - already ms",
- epoch: 1672531200000,
- expected: 1672531200000, // unchanged
- },
- {
- name: "16-digit Unix timestamp (microseconds)",
- epoch: 1672531200000000, // microseconds
- expected: 1672531200000, // / 10^3
- },
- {
- name: "19-digit Unix timestamp (nanoseconds)",
- epoch: 1672531200000000000, // nanoseconds
- expected: 1672531200000, // / 10^6
- },
- {
- name: "Unix epoch start - zero is unchanged",
- epoch: 0,
- expected: 0,
- },
- {
- name: "Recent timestamp in seconds - 2024-05-25 12:00:00 UTC",
- epoch: 1716638400,
- expected: 1716638400000,
- },
- }
-
- for _, tt := range tests {
- t.Run(tt.name, func(t *testing.T) {
- result := ToMilliSecs(tt.epoch)
- if result != tt.expected {
- t.Errorf("ToMilliSecs(%d) = %d, want %d", tt.epoch, result, tt.expected)
- }
- })
- }
-}
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/req.go b/pkg/types/querybuildertypes/querybuildertypesv5/req.go
index bd2833658b..c0989fa85e 100644
--- a/pkg/types/querybuildertypes/querybuildertypesv5/req.go
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/req.go
@@ -706,6 +706,12 @@ func (r *QueryRangeRequest) UnmarshalJSON(data []byte) error {
// Copy the decoded values back to the original struct
*r = QueryRangeRequest(temp)
+ // Coerce the window to epoch milliseconds up front so every downstream
+ // consumer (TimeRange, narrowWindowByTraceID, step interval, etc.) can
+ // safely assume ms regardless of the resolution the caller sent.
+ r.Start = ToMilliSecs(r.Start)
+ r.End = ToMilliSecs(r.End)
+
return nil
}
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/time.go b/pkg/types/querybuildertypes/querybuildertypesv5/time.go
new file mode 100644
index 0000000000..a8fc8d69a0
--- /dev/null
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/time.go
@@ -0,0 +1,25 @@
+package querybuildertypesv5
+
+import "math"
+
+// ToMilliSecs takes an epoch whose resolution is inferred from its magnitude
+// (s/ms/µs/ns) and returns it in milliseconds. A millisecond epoch for the
+// current era has 13 digits (e.g. ~1.7e12 in 2026), so the value is scaled so
+// its digit-width matches: smaller values (seconds) are scaled up, larger ones
+// (micro/nanoseconds) are scaled down. Zero is returned unchanged.
+func ToMilliSecs(epoch uint64) uint64 {
+ if epoch == 0 {
+ return 0
+ }
+ temp := epoch
+ count := 0
+ for epoch != 0 {
+ epoch /= 10
+ count++
+ }
+ const msDigits = 13
+ if count < msDigits {
+ return temp * uint64(math.Pow(10, float64(msDigits-count)))
+ }
+ return temp / uint64(math.Pow(10, float64(count-msDigits)))
+}
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/time_test.go b/pkg/types/querybuildertypes/querybuildertypesv5/time_test.go
new file mode 100644
index 0000000000..69a09cb864
--- /dev/null
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/time_test.go
@@ -0,0 +1,52 @@
+package querybuildertypesv5
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestToMilliSecs(t *testing.T) {
+ testCases := []struct {
+ name string
+ epoch uint64
+ expected uint64
+ }{
+ {
+ name: "SecondsEpoch_TenDigits_ScaledUp",
+ epoch: 1672531200, // seconds, 2023-01-01 00:00:00 UTC
+ expected: 1672531200000, // * 10^3
+ },
+ {
+ name: "MilliSecondsEpoch_ThirteenDigits_Unchanged",
+ epoch: 1672531200000,
+ expected: 1672531200000, // unchanged
+ },
+ {
+ name: "MicroSecondsEpoch_SixteenDigits_ScaledDown",
+ epoch: 1672531200000000, // microseconds
+ expected: 1672531200000, // / 10^3
+ },
+ {
+ name: "NanoSecondsEpoch_NineteenDigits_ScaledDown",
+ epoch: 1672531200000000000, // nanoseconds
+ expected: 1672531200000, // / 10^6
+ },
+ {
+ name: "Zero_Unchanged",
+ epoch: 0,
+ expected: 0,
+ },
+ {
+ name: "SecondsEpoch_RecentTimestamp_ScaledUp",
+ epoch: 1716638400, // 2024-05-25 12:00:00 UTC
+ expected: 1716638400000,
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ assert.Equal(t, testCase.expected, ToMilliSecs(testCase.epoch))
+ })
+ }
+}
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go b/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
index 03f4e19782..8ea8783afa 100644
--- a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
@@ -5,12 +5,16 @@ import (
"math"
"slices"
"strings"
+ "time"
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/types/metrictypes"
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
)
+// maxEndTimeAhead tolerates client clock skew; anything further ahead is a malformed timestamp.
+const maxEndTimeAhead = 24 * time.Hour
+
// getQueryIdentifier returns a friendly identifier for a query based on its type and name/content.
func getQueryIdentifier(envelope QueryEnvelope, index int) string {
name := envelope.GetQueryName()
@@ -574,12 +578,8 @@ func (q *QueryBuilderQuery[T]) validateOrderByForAggregation() error {
// Validate validates the entire query range request.
func (r *QueryRangeRequest) Validate(opts ...ValidationOption) error {
- // Validate time range
- if r.RequestType != RequestTypeRawStream && r.Start >= r.End {
- return errors.NewInvalidInputf(
- errors.CodeInvalidInput,
- "start time must be before end time",
- )
+ if err := r.validateTimeRange(); err != nil {
+ return err
}
// Validate request type
@@ -631,8 +631,8 @@ func (r *QueryRangeRequest) Validate(opts ...ValidationOption) error {
// this so per-query errors can be attributed individually via QueryEnvelope.Validate
// instead of failing fast like Validate does.
func (r *QueryRangeRequest) ValidateRequestScope() ([]ValidationOption, error) {
- if r.RequestType != RequestTypeRawStream && r.Start >= r.End {
- return nil, errors.NewInvalidInputf(errors.CodeInvalidInput, "start time must be before end time")
+ if err := r.validateTimeRange(); err != nil {
+ return nil, err
}
var opts []ValidationOption
@@ -699,6 +699,22 @@ func (e QueryEnvelope) Validate(opts ...ValidationOption) error {
return validateQueryEnvelope(e, opts...)
}
+func (r *QueryRangeRequest) validateTimeRange() error {
+ if r.RequestType == RequestTypeRawStream {
+ return nil
+ }
+ if r.Start == 0 {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "start time is required")
+ }
+ if r.Start >= r.End {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "start time must be before end time")
+ }
+ if r.End > uint64(time.Now().Add(maxEndTimeAhead).UnixMilli()) {
+ return errors.NewInvalidInputf(errors.CodeInvalidInput, "end time %s is too far in the future", time.UnixMilli(int64(r.End)).UTC().Format(time.RFC3339))
+ }
+ return nil
+}
+
// validateAllQueriesNotDisabled validates that at least one query in the composite query is enabled.
func (r *QueryRangeRequest) validateAllQueriesNotDisabled() error {
for _, envelope := range r.CompositeQuery.Queries {
diff --git a/tests/integration/tests/queriercommon/09_time_range.py b/tests/integration/tests/queriercommon/09_time_range.py
new file mode 100644
index 0000000000..394a2f9401
--- /dev/null
+++ b/tests/integration/tests/queriercommon/09_time_range.py
@@ -0,0 +1,165 @@
+from collections.abc import Callable
+from datetime import UTC, datetime, timedelta
+from http import HTTPStatus
+
+import pytest
+
+from fixtures import types
+from fixtures.auth import USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD
+from fixtures.logs import Logs
+from fixtures.metrics import Metrics
+from fixtures.querier import (
+ Aggregation,
+ BuilderQuery,
+ MetricAggregation,
+ get_all_warnings,
+ get_error_message,
+ get_series_values,
+ make_query_request,
+)
+from fixtures.traces import TraceIdGenerator, Traces
+
+TWO_YEARS = timedelta(days=730)
+
+
+ "start_ms,end_ms,message",
+ [
+ pytest.param(0, 1_700_000_000_000, "start time is required", id="zero_start"),
+ pytest.param(1_700_000_000_000, 1_700_000_000_000, "start time must be before end time", id="start_equals_end"),
+ # 2592000000 is 30 days in ms; read as an epoch in seconds it is year 2052.
+ pytest.param(1_700_000_000_000, 2_592_000_000, "end time 2052-02-20T00:00:00Z is too far in the future", id="end_in_2052"),
+ ],
+)
+def test_time_range_rejected(
+ start_ms: int,
+ end_ms: int,
+ message: str,
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+) -> None:
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+
+ response = make_query_request(
+ signoz,
+ token,
+ start_ms=start_ms,
+ end_ms=end_ms,
+ queries=[BuilderQuery(signal="logs", aggregations=[Aggregation(expression="count()")], step_interval=60).to_dict()],
+ )
+
+ assert response.status_code == HTTPStatus.BAD_REQUEST
+ assert get_error_message(response.json()) == message
+
+
+def test_two_year_logs_timeseries_step_clamped(
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+ insert_logs: Callable[[list[Logs]], None],
+) -> None:
+ # A 1s step over 2 years is clamped to 42045s (at most ~1500 points); end is aligned to it so the bucket count is stable.
+ step_seconds = 42045
+ end = datetime.fromtimestamp(int(datetime.now(tz=UTC).timestamp()) // step_seconds * step_seconds, tz=UTC)
+ insert_logs([Logs(timestamp=end - timedelta(minutes=1), resources={"service.name": "time-range-logs"}, body="two year span")])
+
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+ response = make_query_request(
+ signoz,
+ token,
+ start_ms=int((end - TWO_YEARS).timestamp() * 1000),
+ end_ms=int(end.timestamp() * 1000),
+ queries=[
+ BuilderQuery(
+ signal="logs",
+ filter_expression="service.name = 'time-range-logs'",
+ aggregations=[Aggregation(expression="count()")],
+ step_interval=1,
+ ).to_dict()
+ ],
+ format_options={"formatTableResultForUI": False, "fillGaps": True},
+ )
+
+ assert response.status_code == HTTPStatus.OK
+ assert response.json()["data"]["meta"]["stepIntervals"] == {"A": step_seconds}
+ assert get_all_warnings(response.json()) == [
+ {"message": f"Query A is requesting aggregation interval 1 seconds, which is smaller than the minimum allowed interval of {step_seconds} seconds for selected time range. Using the minimum instead"},
+ ]
+ values = get_series_values(response.json(), "A")
+ assert len(values) == 1502
+ assert sum(v["value"] for v in values) == 1
+
+
+def test_two_year_traces_timeseries_step_clamped(
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+ insert_traces: Callable[[list[Traces]], None],
+) -> None:
+ # Same clamp as logs: 42045s; end is aligned to it so the bucket count is stable.
+ step_seconds = 42045
+ end = datetime.fromtimestamp(int(datetime.now(tz=UTC).timestamp()) // step_seconds * step_seconds, tz=UTC)
+ insert_traces([Traces(timestamp=end - timedelta(minutes=1), trace_id=TraceIdGenerator.trace_id(), span_id=TraceIdGenerator.span_id(), resources={"service.name": "time-range-traces"})])
+
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+ response = make_query_request(
+ signoz,
+ token,
+ start_ms=int((end - TWO_YEARS).timestamp() * 1000),
+ end_ms=int(end.timestamp() * 1000),
+ queries=[
+ BuilderQuery(
+ signal="traces",
+ filter_expression="service.name = 'time-range-traces'",
+ aggregations=[Aggregation(expression="count()")],
+ step_interval=1,
+ ).to_dict()
+ ],
+ format_options={"formatTableResultForUI": False, "fillGaps": True},
+ )
+
+ assert response.status_code == HTTPStatus.OK
+ assert response.json()["data"]["meta"]["stepIntervals"] == {"A": step_seconds}
+ assert get_all_warnings(response.json()) == [
+ {"message": f"Query A is requesting aggregation interval 1 seconds, which is smaller than the minimum allowed interval of {step_seconds} seconds for selected time range. Using the minimum instead"},
+ ]
+ values = get_series_values(response.json(), "A")
+ assert len(values) == 1502
+ assert sum(v["value"] for v in values) == 1
+
+
+def test_two_year_metrics_timeseries_step_clamped(
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+ insert_metrics: Callable[[list[Metrics]], None],
+) -> None:
+ # A 1s step over 2 years is clamped to 210600s (~300 points); end is aligned to it so the bucket count is stable.
+ step_seconds = 210600
+ metric_name = "time_range_two_year_gauge"
+ end = datetime.fromtimestamp(int(datetime.now(tz=UTC).timestamp()) // step_seconds * step_seconds, tz=UTC)
+ insert_metrics([Metrics(metric_name=metric_name, labels={"service": "time-range-metrics"}, timestamp=end - timedelta(minutes=1), value=1.0, temporality="Unspecified", type_="Gauge", is_monotonic=False)])
+
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+ response = make_query_request(
+ signoz,
+ token,
+ start_ms=int((end - TWO_YEARS).timestamp() * 1000),
+ end_ms=int(end.timestamp() * 1000),
+ queries=[
+ BuilderQuery(
+ signal="metrics",
+ aggregations=[MetricAggregation(metric_name=metric_name, time_aggregation="latest", space_aggregation="sum", temporality="unspecified")],
+ step_interval=1,
+ ).to_dict()
+ ],
+ format_options={"formatTableResultForUI": False, "fillGaps": True},
+ )
+
+ assert response.status_code == HTTPStatus.OK
+ assert response.json()["data"]["meta"]["stepIntervals"] == {"A": step_seconds}
+ assert get_all_warnings(response.json()) == [
+ {"message": f"Query A is requesting aggregation interval 1 seconds, which is smaller than the minimum allowed interval of {step_seconds} seconds for selected time range. Using the minimum instead"},
+ ]
+ assert len(get_series_values(response.json(), "A")) == 301