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: add check for percentile aggregation for non-histogram metrics (#11387)

Naman Verma4d3d1ef4236e
diff --git a/pkg/querier/querier.go b/pkg/querier/querier.go
index 4f39de0da8..5463bae7b3 100644
--- a/pkg/querier/querier.go
+++ b/pkg/querier/querier.go
@@ -361,6 +361,10 @@ func (q *querier) resolveMetricMetadata(ctx context.Context, queries []qbtypes.Q
missingMetrics = append(missingMetrics, spec.Aggregations[i].MetricName)
continue
}
+ // Type is resolved now; validate aggregation compatibility against it.
+ if err := spec.Aggregations[i].ValidateForType(); err != nil {
+ return nil, "", err
+ }
presentAggregations = append(presentAggregations, spec.Aggregations[i])
}
if len(presentAggregations) == 0 {
diff --git a/pkg/types/metrictypes/metrictypes.go b/pkg/types/metrictypes/metrictypes.go
index c8e17e5858..4f8605b7bd 100644
--- a/pkg/types/metrictypes/metrictypes.go
+++ b/pkg/types/metrictypes/metrictypes.go
@@ -137,7 +137,7 @@ func (t *Type) Scan(src interface{}) error {
}
func (t Type) IsPercentileSpaceAggregationAllowed() bool {
- return t == HistogramType || t == ExpHistogramType || t == SummaryType
+ return t == HistogramType || t == ExpHistogramType
}
var (
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go b/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
index 75c83e4e72..17367fbbf5 100644
--- a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
@@ -317,6 +317,19 @@ func (q *QueryBuilderQuery[T]) validateAggregations(cfg validationConfig) error
return nil
}
+func (m MetricAggregation) ValidateForType() error {
+ if m.SpaceAggregation.IsPercentile() && !m.Type.IsPercentileSpaceAggregationAllowed() {
+ return errors.Newf(
+ errors.TypeInvalidInput,
+ errors.CodeInvalidInput,
+ "invalid space aggregation `%s` for metric type `%s`, percentile space aggregations are only supported for `histogram`, `exponentialhistogram` metric types",
+ m.SpaceAggregation.StringValue(),
+ m.Type.StringValue(),
+ )
+ }
+ return nil
+}
+
func (q *QueryBuilderQuery[T]) validateLimitAndPagination(cfg validationConfig) error {
if cfg.skipLimitOffsetValidation {
return nil
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go b/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go
index 7dde0b8b43..628d2c1447 100644
--- a/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go
@@ -1421,3 +1421,62 @@ func TestNonAggregationFieldsSkipped(t *testing.T) {
}
})
}
+
+func TestMetricAggregationValidateForType(t *testing.T) {
+ cases := []struct {
+ name string
+ metricType metrictypes.Type
+ spaceAggregation metrictypes.SpaceAggregation
+ comparisonParam *metrictypes.ComparisonSpaceAggregationParam
+ wantErr bool
+ }{
+ {
+ name: "percentile on histogram is allowed",
+ metricType: metrictypes.HistogramType,
+ spaceAggregation: metrictypes.SpaceAggregationPercentile95,
+ wantErr: false,
+ },
+ {
+ name: "percentile on exponential histogram is allowed",
+ metricType: metrictypes.ExpHistogramType,
+ spaceAggregation: metrictypes.SpaceAggregationPercentile99,
+ wantErr: false,
+ },
+ {
+ name: "percentile on summary is not allowed",
+ metricType: metrictypes.SummaryType,
+ spaceAggregation: metrictypes.SpaceAggregationPercentile95,
+ wantErr: true,
+ },
+ {
+ name: "percentile on sum is not allowed",
+ metricType: metrictypes.SumType,
+ spaceAggregation: metrictypes.SpaceAggregationPercentile95,
+ wantErr: true,
+ },
+ {
+ name: "non-percentile space aggregation on sum is allowed",
+ metricType: metrictypes.SumType,
+ spaceAggregation: metrictypes.SpaceAggregationSum,
+ wantErr: false,
+ },
+ }
+
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ agg := MetricAggregation{
+ MetricName: "test_metric",
+ Type: tc.metricType,
+ SpaceAggregation: tc.spaceAggregation,
+ ComparisonSpaceAggregationParam: tc.comparisonParam,
+ }
+ err := agg.ValidateForType()
+ if tc.wantErr && err == nil {
+ t.Errorf("expected error, got nil")
+ }
+ if !tc.wantErr && err != nil {
+ t.Errorf("expected no error, got: %v", err)
+ }
+ })
+ }
+}