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.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
fix: ensure timestamp is always in ms (#11483)
Nityananda Gohain2f102ee3f548
diff --git a/pkg/querier/querier.go b/pkg/querier/querier.goindex 980a81e08e..331c90b7b1 100644--- a/pkg/querier/querier.go+++ b/pkg/querier/querier.go@@ -86,6 +86,12 @@ 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)+tmplVars := req.Variablesif tmplVars == nil {tmplVars = make(map[string]qbtypes.VariableItem)@@ -408,6 +414,11 @@ func (q *querier) resolveMetricMetadata(ctx context.Context, queries []qbtypes.Qfunc (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)+event := &qbtypes.QBEvent{Version: "v5",NumberOfQueries: len(req.CompositeQuery.Queries),diff --git a/pkg/querybuilder/time.go b/pkg/querybuilder/time.goindex f1a3b3b554..641095dcfa 100644--- a/pkg/querybuilder/time.go+++ b/pkg/querybuilder/time.go@@ -33,6 +33,28 @@ 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.goindex 7617d93fa4..551f0d0ff3 100644--- a/pkg/querybuilder/time_test.go+++ b/pkg/querybuilder/time_test.go@@ -60,3 +60,51 @@ 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)+ }+ })+ }+}