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.

chore: disallow small step intervals for large durations (#4950)

Srikanth Chekuri27d12871af55
diff --git a/pkg/query-service/app/parser.go b/pkg/query-service/app/parser.go
index 670f5eff25..f7f622e250 100644
--- a/pkg/query-service/app/parser.go
+++ b/pkg/query-service/app/parser.go
@@ -20,6 +20,7 @@ import (
"go.signoz.io/signoz/pkg/query-service/app/metrics"
"go.signoz.io/signoz/pkg/query-service/app/queryBuilder"
"go.signoz.io/signoz/pkg/query-service/auth"
+ "go.signoz.io/signoz/pkg/query-service/common"
"go.signoz.io/signoz/pkg/query-service/constants"
"go.signoz.io/signoz/pkg/query-service/model"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
@@ -1038,6 +1039,10 @@ func ParseQueryRangeParams(r *http.Request) (*v3.QueryRangeParamsV3, *model.ApiE
}
}
+ if minStep := common.MinAllowedStepInterval(queryRangeParams.Start, queryRangeParams.End); query.StepInterval < minStep {
+ query.StepInterval = minStep
+ }
+
var timeShiftBy int64
if len(query.Functions) > 0 {
for idx := range query.Functions {
diff --git a/pkg/query-service/app/parser_test.go b/pkg/query-service/app/parser_test.go
index f2959616a2..be113d08b3 100644
--- a/pkg/query-service/app/parser_test.go
+++ b/pkg/query-service/app/parser_test.go
@@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
+ "go.signoz.io/signoz/pkg/query-service/common"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)
@@ -1174,3 +1175,105 @@ func TestQueryRangeFormula(t *testing.T) {
})
}
}
+
+func TestParseQueryRangeParamsStepIntervalAdjustment(t *testing.T) {
+ reqCases := []struct {
+ desc string
+ start int64
+ end int64
+ step int64
+ }{
+ {
+ desc: "30 minutes and 60 seconds step",
+ start: time.Now().Add(-30 * time.Minute).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 60, // no update
+ },
+ {
+ desc: "1 hour and 1 second step",
+ start: time.Now().Add(-time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 1, // gets updated
+ },
+ {
+ desc: "1 week and 1 minute step",
+ start: time.Now().Add(-7 * 24 * time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 60, // gets updated
+ },
+ {
+ desc: "1 day and 1 hour step",
+ start: time.Now().Add(-24 * time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 3600, // no update
+ },
+ {
+ desc: "1 day and 1 minute step",
+ start: time.Now().Add(-24 * time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 60, // gets updated
+ },
+ {
+ desc: "1 day and 2 minutes step",
+ start: time.Now().Add(-24 * time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 120, // gets updated
+ },
+ {
+ desc: "1 day and 5 minutes step",
+ start: time.Now().Add(-24 * time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 300, // no update
+ },
+ {
+ desc: "1 week and 10 minutes step",
+ start: time.Now().Add(-7 * 24 * time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 600, // get updated
+ },
+ {
+ desc: "1 week and 45 minutes step",
+ start: time.Now().Add(-7 * 24 * time.Hour).UnixMilli(),
+ end: time.Now().UnixMilli(),
+ step: 2700, // no update
+ },
+ }
+
+ for _, tc := range reqCases {
+ t.Run(tc.desc, func(t *testing.T) {
+
+ queryRangeParams := &v3.QueryRangeParamsV3{
+ Start: tc.start,
+ End: tc.end,
+ Step: tc.step,
+ CompositeQuery: &v3.CompositeQuery{
+ PanelType: v3.PanelTypeGraph,
+ QueryType: v3.QueryTypeBuilder,
+ BuilderQueries: map[string]*v3.BuilderQuery{
+ "A": {
+ QueryName: "A",
+ DataSource: v3.DataSourceMetrics,
+ AggregateOperator: v3.AggregateOperatorSum,
+ AggregateAttribute: v3.AttributeKey{Key: "signoz_calls_total"},
+ GroupBy: []v3.AttributeKey{{Key: "service_name"}, {Key: "operation_name"}},
+ Expression: "A",
+ StepInterval: tc.step,
+ },
+ },
+ },
+ Variables: map[string]interface{}{},
+ }
+
+ body := &bytes.Buffer{}
+ err := json.NewEncoder(body).Encode(queryRangeParams)
+ require.NoError(t, err)
+ req := httptest.NewRequest(http.MethodPost, "/api/v3/query_range", body)
+
+ p, apiErr := ParseQueryRangeParams(req)
+ if apiErr != nil && apiErr.Err != nil {
+ t.Fatalf("unexpected error %s", apiErr.Err)
+ }
+ require.True(t, p.CompositeQuery.BuilderQueries["A"].StepInterval >= common.MinAllowedStepInterval(p.Start, p.End))
+ })
+ }
+}
diff --git a/pkg/query-service/common/metrics.go b/pkg/query-service/common/metrics.go
index c6b8075991..3a14e06439 100644
--- a/pkg/query-service/common/metrics.go
+++ b/pkg/query-service/common/metrics.go
@@ -4,6 +4,7 @@ import (
"math"
"time"
+ "go.signoz.io/signoz/pkg/query-service/constants"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
)
@@ -23,3 +24,10 @@ func PastDayRoundOff() int64 {
now := time.Now().UnixMilli()
return int64(math.Floor(float64(now)/float64(time.Hour.Milliseconds()*24))) * time.Hour.Milliseconds() * 24
}
+
+// start and end are in milliseconds
+func MinAllowedStepInterval(start, end int64) int64 {
+ step := (end - start) / constants.MaxAllowedPointsInTimeSeries / 1000
+ // return the nearest lower multiple of 60
+ return step - step%60
+}
diff --git a/pkg/query-service/constants/constants.go b/pkg/query-service/constants/constants.go
index 54fc819478..fe2ad8c86c 100644
--- a/pkg/query-service/constants/constants.go
+++ b/pkg/query-service/constants/constants.go
@@ -25,6 +25,8 @@ var ConfigSignozIo = "https://config.signoz.io/api/v1"
var DEFAULT_TELEMETRY_ANONYMOUS = false
+const MaxAllowedPointsInTimeSeries = 300
+
func IsTelemetryEnabled() bool {
if testing.Testing() {
return false
diff --git a/pkg/query-service/rules/thresholdRule.go b/pkg/query-service/rules/thresholdRule.go
index 05fd526b79..feb16bda49 100644
--- a/pkg/query-service/rules/thresholdRule.go
+++ b/pkg/query-service/rules/thresholdRule.go
@@ -19,6 +19,7 @@ import (
"github.com/ClickHouse/clickhouse-go/v2"
"github.com/ClickHouse/clickhouse-go/v2/lib/driver"
+ "go.signoz.io/signoz/pkg/query-service/common"
"go.signoz.io/signoz/pkg/query-service/converter"
"go.signoz.io/signoz/pkg/query-service/app/queryBuilder"
@@ -469,7 +470,7 @@ func (r *ThresholdRule) prepareQueryRange(ts time.Time) *v3.QueryRangeParamsV3 {
if r.ruleCondition.CompositeQuery != nil && r.ruleCondition.CompositeQuery.BuilderQueries != nil {
for _, q := range r.ruleCondition.CompositeQuery.BuilderQueries {
- q.StepInterval = 60
+ q.StepInterval = int64(math.Max(float64(common.MinAllowedStepInterval(start, end)), 60))
}
}
@@ -501,13 +502,7 @@ func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, quer
}
columnTypes := rows.ColumnTypes()
- if err != nil {
- return nil, err
- }
columnNames := rows.Columns()
- if err != nil {
- return nil, err
- }
vars := make([]interface{}, len(columnTypes))
for i := range columnTypes {
@@ -648,7 +643,8 @@ func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, quer
resultMap[labelHash] = sample
}
case OnAverage:
- sample.Point.V = (existing.Point.V + sample.Point.V) / 2
+ sample.Point.Vs = append(existing.Point.Vs, sample.Point.V)
+ sample.Point.V = (existing.Point.V + sample.Point.V)
resultMap[labelHash] = sample
case InTotal:
sample.Point.V = (existing.Point.V + sample.Point.V)
@@ -678,6 +674,13 @@ func (r *ThresholdRule) runChQuery(ctx context.Context, db clickhouse.Conn, quer
}
+ if r.matchType() == OnAverage {
+ for hash, s := range resultMap {
+ s.Point.V = s.Point.V / float64(len(s.Point.Vs))
+ resultMap[hash] = s
+ }
+ }
+
for hash, s := range resultMap {
if r.matchType() == AllTheTimes && r.compareOp() == ValueIsEq {
for _, v := range s.Point.Vs {
diff --git a/pkg/query-service/rules/thresholdRule_test.go b/pkg/query-service/rules/thresholdRule_test.go
index b7d3cc5fee..3e51e652b9 100644
--- a/pkg/query-service/rules/thresholdRule_test.go
+++ b/pkg/query-service/rules/thresholdRule_test.go
@@ -266,6 +266,45 @@ func TestThresholdRuleCombinations(t *testing.T) {
matchType: "1", // Once
target: 0.0,
},
+ {
+ values: [][]interface{}{
+ {int32(2), "endpoint"},
+ {int32(3), "endpoint"},
+ {int32(2), "endpoint"},
+ {int32(4), "endpoint"},
+ {int32(2), "endpoint"},
+ },
+ expectAlert: true,
+ compareOp: "2", // Below
+ matchType: "3", // On Average
+ target: 3.0,
+ },
+ {
+ values: [][]interface{}{
+ {int32(4), "endpoint"},
+ {int32(7), "endpoint"},
+ {int32(5), "endpoint"},
+ {int32(2), "endpoint"},
+ {int32(9), "endpoint"},
+ },
+ expectAlert: false,
+ compareOp: "2", // Below
+ matchType: "3", // On Average
+ target: 3.0,
+ },
+ {
+ values: [][]interface{}{
+ {int32(4), "endpoint"},
+ {int32(7), "endpoint"},
+ {int32(5), "endpoint"},
+ {int32(2), "endpoint"},
+ {int32(9), "endpoint"},
+ },
+ expectAlert: true,
+ compareOp: "2", // Below
+ matchType: "3", // On Average
+ target: 6.0,
+ },
}
for idx, c := range cases {