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: resolve aggregate column for exp histograms before samples table (#12640)

Naman Verma0bdc7bf6a190
diff --git a/pkg/querier/querier.go b/pkg/querier/querier.go
index 7ff61fd561..0fc4e1ef44 100644
--- a/pkg/querier/querier.go
+++ b/pkg/querier/querier.go
@@ -470,7 +470,7 @@ func (q *querier) resolveMetricMetadata(ctx context.Context, orgID valuer.UUID,
continue
}
// Type is resolved now; validate aggregation compatibility against it.
- if err := spec.Aggregations[i].ValidateForType(); err != nil {
+ if err := spec.Aggregations[i].ValidateForTypeAndTemporality(); err != nil {
return nil, nil, err
}
if reducedMetricsSet[spec.Aggregations[i].MetricName] {
diff --git a/pkg/statementbuilder/metricsstatementbuilder/statement_builder.go b/pkg/statementbuilder/metricsstatementbuilder/statement_builder.go
index faf08402a9..3804302a49 100644
--- a/pkg/statementbuilder/metricsstatementbuilder/statement_builder.go
+++ b/pkg/statementbuilder/metricsstatementbuilder/statement_builder.go
@@ -428,20 +428,24 @@ func (b *StatementBuilder) buildTemporalAggDeltaFastPath(
sb.SelectMore(fmt.Sprintf("`%s`", GroupByColumnAlias(i, g.Name)))
}
- aggCol, err := metricstelemetryschema.AggregationColumnForSamplesTable(
- samplesTable, query.Aggregations[0].Temporality, query.Aggregations[0].TimeAggregation,
- )
- if err != nil {
- return "", nil, err
- }
- if query.Aggregations[0].TimeAggregation == metrictypes.TimeAggregationRate {
- // TODO(srikanthccv): should it be step interval or use [start_time_unix_nano](https://github.com/open-telemetry/opentelemetry-proto/blob/d3fb76d70deb0874692bd0ebe03148580d85f3bb/opentelemetry/proto/metrics/v1/metrics.proto#L400C11-L400C31)?
- aggCol = fmt.Sprintf("%s/%d", aggCol, stepSec)
- }
-
+ var aggCol string
if query.Aggregations[0].SpaceAggregation.IsPercentile() &&
query.Aggregations[0].Type == metrictypes.ExpHistogramType {
+ // merging sketches already spans every series in the step, so neither a
+ // samples-table value column nor the rate divisor applies
aggCol = fmt.Sprintf("quantilesDDMerge(0.01, %f)(sketch)[1]", query.Aggregations[0].SpaceAggregation.Percentile())
+ } else {
+ col, err := metricstelemetryschema.AggregationColumnForSamplesTable(
+ samplesTable, query.Aggregations[0].Temporality, query.Aggregations[0].TimeAggregation,
+ )
+ if err != nil {
+ return "", nil, err
+ }
+ aggCol = col
+ if query.Aggregations[0].TimeAggregation == metrictypes.TimeAggregationRate {
+ // TODO(srikanthccv): should it be step interval or use [start_time_unix_nano](https://github.com/open-telemetry/opentelemetry-proto/blob/d3fb76d70deb0874692bd0ebe03148580d85f3bb/opentelemetry/proto/metrics/v1/metrics.proto#L400C11-L400C31)?
+ aggCol = fmt.Sprintf("%s/%d", aggCol, stepSec)
+ }
}
sb.SelectMore(fmt.Sprintf("%s AS value", aggCol))
diff --git a/pkg/statementbuilder/metricsstatementbuilder/stmt_builder_test.go b/pkg/statementbuilder/metricsstatementbuilder/stmt_builder_test.go
index b2d58dfbd3..c92f1c2e76 100644
--- a/pkg/statementbuilder/metricsstatementbuilder/stmt_builder_test.go
+++ b/pkg/statementbuilder/metricsstatementbuilder/stmt_builder_test.go
@@ -126,6 +126,64 @@ func TestStatementBuilder(t *testing.T) {
},
expectedErr: nil,
},
+ {
+ name: "test_exp_histogram_percentile_delta",
+ requestType: qbtypes.RequestTypeTimeSeries,
+ query: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{
+ Signal: telemetrytypes.SignalMetrics,
+ StepInterval: qbtypes.Step{Duration: 30 * time.Second},
+ Aggregations: []qbtypes.MetricAggregation{
+ {
+ MetricName: "signoz_latency",
+ Type: metrictypes.ExpHistogramType,
+ Temporality: metrictypes.Delta,
+ SpaceAggregation: metrictypes.SpaceAggregationPercentile95,
+ },
+ },
+ GroupBy: []qbtypes.GroupByKey{
+ {
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: "service.name",
+ },
+ },
+ },
+ },
+ expected: qbtypes.Statement{
+ Query: "WITH __spatial_aggregation_cte AS (SELECT toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), toIntervalSecond(30)) AS ts, `__GROUP_BY_KEY_0_service.name`, quantilesDDMerge(0.01, 0.950000)(sketch)[1] AS value FROM signoz_metrics.distributed_exp_hist AS points INNER JOIN (SELECT fingerprint, JSONExtractString(labels, 'service.name') AS `__GROUP_BY_KEY_0_service.name` FROM signoz_metrics.time_series_v4_6hrs WHERE metric_name IN (?) AND unix_milli >= ? AND unix_milli <= ? AND LOWER(temporality) LIKE LOWER(?) GROUP BY fingerprint, `__GROUP_BY_KEY_0_service.name`) AS filtered_time_series ON points.fingerprint = filtered_time_series.fingerprint WHERE metric_name IN (?) AND unix_milli >= ? AND unix_milli < ? GROUP BY ts, `__GROUP_BY_KEY_0_service.name`) SELECT * FROM __spatial_aggregation_cte ORDER BY `__GROUP_BY_KEY_0_service.name`, ts",
+ Args: []any{"signoz_latency", uint64(1747936800000), uint64(1747983420000), "delta", "signoz_latency", uint64(1747947390000), uint64(1747983420000)},
+ },
+ expectedErr: nil,
+ },
+ {
+ // the sketch merge spans the whole step, so `rate` must not add a /step divisor
+ name: "test_exp_histogram_percentile_delta_rate_time_aggregation",
+ requestType: qbtypes.RequestTypeTimeSeries,
+ query: qbtypes.QueryBuilderQuery[qbtypes.MetricAggregation]{
+ Signal: telemetrytypes.SignalMetrics,
+ StepInterval: qbtypes.Step{Duration: 30 * time.Second},
+ Aggregations: []qbtypes.MetricAggregation{
+ {
+ MetricName: "signoz_latency",
+ Type: metrictypes.ExpHistogramType,
+ Temporality: metrictypes.Delta,
+ TimeAggregation: metrictypes.TimeAggregationRate,
+ SpaceAggregation: metrictypes.SpaceAggregationPercentile95,
+ },
+ },
+ GroupBy: []qbtypes.GroupByKey{
+ {
+ TelemetryFieldKey: telemetrytypes.TelemetryFieldKey{
+ Name: "service.name",
+ },
+ },
+ },
+ },
+ expected: qbtypes.Statement{
+ Query: "WITH __spatial_aggregation_cte AS (SELECT toStartOfInterval(toDateTime(intDiv(unix_milli, 1000)), toIntervalSecond(30)) AS ts, `__GROUP_BY_KEY_0_service.name`, quantilesDDMerge(0.01, 0.950000)(sketch)[1] AS value FROM signoz_metrics.distributed_exp_hist AS points INNER JOIN (SELECT fingerprint, JSONExtractString(labels, 'service.name') AS `__GROUP_BY_KEY_0_service.name` FROM signoz_metrics.time_series_v4_6hrs WHERE metric_name IN (?) AND unix_milli >= ? AND unix_milli <= ? AND LOWER(temporality) LIKE LOWER(?) GROUP BY fingerprint, `__GROUP_BY_KEY_0_service.name`) AS filtered_time_series ON points.fingerprint = filtered_time_series.fingerprint WHERE metric_name IN (?) AND unix_milli >= ? AND unix_milli < ? GROUP BY ts, `__GROUP_BY_KEY_0_service.name`) SELECT * FROM __spatial_aggregation_cte ORDER BY `__GROUP_BY_KEY_0_service.name`, ts",
+ Args: []any{"signoz_latency", uint64(1747936800000), uint64(1747983420000), "delta", "signoz_latency", uint64(1747947390000), uint64(1747983420000)},
+ },
+ expectedErr: nil,
+ },
{
name: "test_histogram_percentile1",
requestType: qbtypes.RequestTypeTimeSeries,
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go b/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
index 55ee7e4864..0cb2267aa8 100644
--- a/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/validation.go
@@ -374,7 +374,7 @@ func (q *QueryBuilderQuery[T]) validateAggregations(cfg validationConfig) error
return nil
}
-func (m MetricAggregation) ValidateForType() error {
+func (m MetricAggregation) ValidateForTypeAndTemporality() error {
if m.SpaceAggregation.IsPercentile() && !m.Type.IsPercentileSpaceAggregationAllowed() {
return errors.Newf(
errors.TypeInvalidInput,
@@ -384,6 +384,17 @@ func (m MetricAggregation) ValidateForType() error {
m.Type.StringValue(),
)
}
+ // reading a step's distribution out of a cumulative sketch would mean
+ // subtracting the previous point's sketch, which ClickHouse cannot do
+ if m.Type == metrictypes.ExpHistogramType && m.Temporality != metrictypes.Delta {
+ return errors.Newf(
+ errors.TypeUnsupported,
+ errors.CodeUnsupported,
+ "metric `%s` is an exponential histogram recorded with `%s` temporality, which cannot be queried; only `delta` exponential histograms are supported",
+ m.MetricName,
+ m.Temporality.StringValue(),
+ )
+ }
return nil
}
diff --git a/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go b/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go
index 3831aac73c..a9a58243af 100644
--- a/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go
+++ b/pkg/types/querybuildertypes/querybuildertypesv5/validation_test.go
@@ -1517,10 +1517,11 @@ func TestNonAggregationFieldsSkipped(t *testing.T) {
})
}
-func TestMetricAggregationValidateForType(t *testing.T) {
+func TestMetricAggregationValidateForTypeAndTemporality(t *testing.T) {
cases := []struct {
name string
metricType metrictypes.Type
+ temporality metrictypes.Temporality
spaceAggregation metrictypes.SpaceAggregation
comparisonParam *metrictypes.ComparisonSpaceAggregationParam
wantErr bool
@@ -1532,11 +1533,33 @@ func TestMetricAggregationValidateForType(t *testing.T) {
wantErr: false,
},
{
- name: "percentile on exponential histogram is allowed",
+ name: "percentile on delta exponential histogram is allowed",
metricType: metrictypes.ExpHistogramType,
+ temporality: metrictypes.Delta,
spaceAggregation: metrictypes.SpaceAggregationPercentile99,
wantErr: false,
},
+ {
+ name: "cumulative exponential histogram is not allowed",
+ metricType: metrictypes.ExpHistogramType,
+ temporality: metrictypes.Cumulative,
+ spaceAggregation: metrictypes.SpaceAggregationPercentile99,
+ wantErr: true,
+ },
+ {
+ name: "exponential histogram with unresolved temporality is not allowed",
+ metricType: metrictypes.ExpHistogramType,
+ temporality: metrictypes.Unknown,
+ spaceAggregation: metrictypes.SpaceAggregationPercentile99,
+ wantErr: true,
+ },
+ {
+ name: "cumulative histogram is unaffected by the exponential histogram rule",
+ metricType: metrictypes.HistogramType,
+ temporality: metrictypes.Cumulative,
+ spaceAggregation: metrictypes.SpaceAggregationPercentile95,
+ wantErr: false,
+ },
{
name: "percentile on summary is not allowed",
metricType: metrictypes.SummaryType,
@@ -1562,10 +1585,11 @@ func TestMetricAggregationValidateForType(t *testing.T) {
agg := MetricAggregation{
MetricName: "test_metric",
Type: tc.metricType,
+ Temporality: tc.temporality,
SpaceAggregation: tc.spaceAggregation,
ComparisonSpaceAggregationParam: tc.comparisonParam,
}
- err := agg.ValidateForType()
+ err := agg.ValidateForTypeAndTemporality()
if tc.wantErr && err == nil {
t.Errorf("expected error, got nil")
}
diff --git a/tests/fixtures/metrics.py b/tests/fixtures/metrics.py
index 9e7833062f..177f09471b 100644
--- a/tests/fixtures/metrics.py
+++ b/tests/fixtures/metrics.py
@@ -132,7 +132,15 @@ class MetricsSample(ABC):
class MetricsExpHist(ABC):
- """Represents a row in the exp_hist table for exponential histograms."""
+ """Represents a row in the exp_hist table for exponential histograms.
+
+ `observations` must be non-empty; ClickHouse folds it into the `sketch`
+ AggregateFunction state on insert.
+
+ TODO: take an ExponentialHistogramDataPoint and compute the sketch bytes the
+ way the collector does, so the fixture exercises the real write path instead
+ of having ClickHouse build the state.
+ """
env: str
temporality: str
@@ -143,7 +151,7 @@ class MetricsExpHist(ABC):
sum: np.float64
min: np.float64
max: np.float64
- sketch: bytes
+ observations: list[int]
flags: np.uint32
def __init__(
@@ -151,11 +159,7 @@ class MetricsExpHist(ABC):
metric_name: str,
fingerprint: np.uint64,
timestamp: datetime.datetime,
- count: int,
- sum_value: float,
- min_value: float,
- max_value: float,
- sketch: bytes = b"",
+ observations: list[int],
temporality: str = "Unspecified",
env: str = "default",
flags: int = 0,
@@ -165,28 +169,13 @@ class MetricsExpHist(ABC):
self.metric_name = metric_name
self.fingerprint = fingerprint
self.unix_milli = np.int64(int(timestamp.timestamp() * 1e3))
- self.count = np.uint64(count)
- self.sum = np.float64(sum_value)
- self.min = np.float64(min_value)
- self.max = np.float64(max_value)
- self.sketch = sketch
+ self.observations = observations
+ self.count = np.uint64(len(observations))
+ self.sum = np.float64(sum(observations))
+ self.min = np.float64(min(observations))
+ self.max = np.float64(max(observations))
self.flags = np.uint32(flags)
- def to_row(self) -> list:
- return [
- self.env,
- self.temporality,
- self.metric_name,
- self.fingerprint,
- self.unix_milli,
- self.count,
- self.sum,
- self.min,
- self.max,
- self.sketch,
- self.flags,
- ]
-
class MetricsMetadata(ABC):
"""Represents a row in the metadata table for metric metadata."""
@@ -429,6 +418,73 @@ class Metrics(ABC):
return metrics
+class ExpHistogramMetrics(ABC):
+ """High-level exponential histogram representation. Produces both time series
+ and exp_hist entries."""
+
+ metric_name: str
+ labels: dict[str, str]
+ temporality: str
+ timestamp: datetime.datetime
+ observations: list[int]
+
+ @property
+ def time_series(self) -> MetricsTimeSeries:
+ return self._time_series
+
+ @property
+ def exp_hist(self) -> MetricsExpHist:
+ return self._exp_hist
+
+ def __init__(
+ self,
+ metric_name: str,
+ observations: list[int],
+ labels: dict[str, str] = {},
+ timestamp: datetime.datetime | None = None,
+ temporality: str = "Delta",
+ flags: int = 0,
+ description: str = "",
+ unit: str = "",
+ env: str = "default",
+ resource_attributes: dict[str, str] = {},
+ scope_attributes: dict[str, str] = {},
+ ) -> None:
+ if timestamp is None:
+ timestamp = datetime.datetime.now()
+ self.metric_name = metric_name
+ self.labels = labels
+ self.temporality = temporality
+ self.timestamp = timestamp
+ self.observations = observations
+
+ self._time_series = MetricsTimeSeries(
+ metric_name=metric_name,
+ labels=labels,
+ timestamp=timestamp,
+ temporality=temporality,
+ description=description,
+ unit=unit,
+ # the querier resolves the metric type from this column, and only an
+ # ExponentialHistogram here routes the query to the sketch read
+ type_="ExponentialHistogram",
+ is_monotonic=False,
+ env=env,
+ resource_attrs=resource_attributes,
+ scope_attrs=scope_attributes,
+ )
+
+ self._exp_hist = MetricsExpHist(
+ metric_name=metric_name,
+ fingerprint=self._time_series.fingerprint,
+ timestamp=timestamp,
+ observations=observations,
+ temporality=temporality,
+ env=env,
+ flags=flags,
+ )
+
+
class MetricsReducedTimeSeries(ABC):
"""Represents a row in the time_series_v4_reduced table i.e what
the time_series_v4_reduced_mv materializes for a metric under a
@@ -686,6 +742,47 @@ class MetricsBufferSample(ABC):
]
+def insert_time_series_to_clickhouse(conn, time_series: list[MetricsTimeSeries]) -> None:
+ """
+ Insert one distributed_time_series_v4 registration row per (series, hour
+ bucket), unix_milli floored to the hour — the exporter's exact shape.
+ Readers floor lookup windows to these buckets: skipping per-bucket
+ re-registration or keeping raw mid-hour timestamps hides series in ways
+ production never sees.
+ """
+ time_series_map: dict[tuple[int, int], MetricsTimeSeries] = {}
+ for ts in time_series:
+ fp = int(ts.fingerprint)
+ hour_bucket = int(ts.unix_milli) // 3_600_000
+ if (fp, hour_bucket) not in time_series_map:
+ ts.unix_milli = np.int64(hour_bucket * 3_600_000)
+ time_series_map[(fp, hour_bucket)] = ts
+
+ if len(time_series_map) == 0:
+ return
+
+ conn.insert(
+ database="signoz_metrics",
+ table="distributed_time_series_v4",
+ column_names=[
+ "env",
+ "temporality",
+ "metric_name",
+ "description",
+ "unit",
+ "type",
+ "is_monotonic",
+ "fingerprint",
+ "unix_milli",
+ "labels",
+ "attrs",
+ "scope_attrs",
+ "resource_attrs",
+ ],
+ data=[ts.to_row() for ts in time_series_map.values()],
+ )
+
+
def insert_metrics_to_clickhouse(conn, metrics: list[Metrics]) -> None:
"""
Insert metrics into ClickHouse tables.
@@ -697,39 +794,7 @@ def insert_metrics_to_clickhouse(conn, metrics: list[Metrics]) -> None:
Pure function so the seeder container can reuse the exact insert path
used by the pytest fixture. `conn` is a clickhouse-connect Client.
"""
- # One registration row per (series, hour bucket), unix_milli floored to
- # the hour — the exporter's exact shape. Readers floor lookup windows to
- # these buckets: skipping per-bucket re-registration or keeping raw
- # mid-hour timestamps hides series in ways production never sees.
- time_series_map: dict[tuple[int, int], MetricsTimeSeries] = {}
- for metric in metrics:
- fp = int(metric.time_series.fingerprint)
- hour_bucket = int(metric.time_series.unix_milli) // 3_600_000
- if (fp, hour_bucket) not in time_series_map:
- metric.time_series.unix_milli = np.int64(hour_bucket * 3_600_000)
- time_series_map[(fp, hour_bucket)] = metric.time_series
-
- if len(time_series_map) > 0:
- conn.insert(
- database="signoz_metrics",
- table="distributed_time_series_v4",
- column_names=[
- "env",
- "temporality",
- "metric_name",
- "description",
- "unit",
- "type",
- "is_monotonic",
- "fingerprint",
- "unix_milli",
- "labels",
- "attrs",
- "scope_attrs",
- "resource_attrs",
- ],
- data=[ts.to_row() for ts in time_series_map.values()],
- )
+ insert_time_series_to_clickhouse(conn, [metric.time_series for metric in metrics])
samples = [metric.sample for metric in metrics]
if len(samples) > 0:
@@ -748,6 +813,15 @@ def insert_metrics_to_clickhouse(conn, metrics: list[Metrics]) -> None:
data=[sample.to_row() for sample in samples],
)
+ insert_metrics_metadata_to_clickhouse(conn, metrics)
+
+
+def insert_metrics_metadata_to_clickhouse(conn, metrics: list) -> None:
+ """
+ Insert the distributed_metadata rows describing each metric's point, resource
+ and scope attributes. Accepts anything exposing `time_series`, `labels` and
+ `timestamp`.
+ """
# (metric_name, attr_type, attr_name, attr_value) -> MetricsMetadata
metadata_map: dict[tuple, MetricsMetadata] = {}
for metric in metrics:
@@ -853,6 +927,61 @@ def insert_metrics(
)
+def insert_exp_histogram_metrics_to_clickhouse(conn, metrics: list[ExpHistogramMetrics]) -> None:
+ """
+ Insert exponential histograms into ClickHouse tables.
+ Handles insertion into:
+ - distributed_time_series_v4 (time series metadata)
+ - distributed_exp_hist (per-point sketches)
+ - distributed_metadata (metric attribute metadata)
+ """
+ insert_time_series_to_clickhouse(conn, [metric.time_series for metric in metrics])
+
+ # `sketch` is AggregateFunction(quantilesDD(...), UInt64) — the state has to be
+ # folded server-side, it cannot be sent as a literal. The quantilesDDState
+ # parameters must match the column's exactly or the INSERT is rejected.
+ for metric in metrics:
+ hist = metric.exp_hist
+ conn.command(
+ "INSERT INTO signoz_metrics.distributed_exp_hist "
+ "(env, temporality, metric_name, fingerprint, unix_milli, count, sum, min, max, sketch, flags) "
+ "SELECT %(env)s, %(temporality)s, %(metric_name)s, %(fingerprint)s, %(unix_milli)s, "
+ "%(count)s, %(sum)s, %(min)s, %(max)s, "
+ "quantilesDDState(0.01, 0.5, 0.75, 0.9, 0.95, 0.99)(toUInt64(observation)), %(flags)s "
+ "FROM (SELECT arrayJoin(%(observations)s) AS observation)",
+ parameters={
+ "env": hist.env,
+ "temporality": hist.temporality,
+ "metric_name": hist.metric_name,
+ "fingerprint": int(hist.fingerprint),
+ "unix_milli": int(hist.unix_milli),
+ "count": int(hist.count),
+ "sum": float(hist.sum),
+ "min": float(hist.min),
+ "max": float(hist.max),
+ "observations": hist.observations,
+ "flags": int(hist.flags),
+ },
+ )
+
+ insert_metrics_metadata_to_clickhouse(conn, metrics)
+
+
[email protected](name="insert_exp_histogram_metrics", scope="function")
+def insert_exp_histogram_metrics(
+ clickhouse: types.TestContainerClickhouse,
+) -> Generator[Callable[[list[ExpHistogramMetrics]], None], Any]:
+ def _insert_exp_histogram_metrics(metrics: list[ExpHistogramMetrics]) -> None:
+ insert_exp_histogram_metrics_to_clickhouse(clickhouse.conn, metrics)
+
+ yield _insert_exp_histogram_metrics
+
+ truncate_metrics_tables(
+ clickhouse.conn,
+ clickhouse.env["SIGNOZ_TELEMETRYSTORE_CLICKHOUSE_CLUSTER"],
+ )
+
+
def insert_reduced_metrics_to_clickhouse(
conn,
time_series: list[MetricsReducedTimeSeries],
diff --git a/tests/integration/tests/queriermetrics/14_exp_histogram.py b/tests/integration/tests/queriermetrics/14_exp_histogram.py
new file mode 100644
index 0000000000..ed237cac43
--- /dev/null
+++ b/tests/integration/tests/queriermetrics/14_exp_histogram.py
@@ -0,0 +1,172 @@
+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.metrics import ExpHistogramMetrics
+from fixtures.querier import (
+ build_builder_query,
+ get_all_series,
+ get_all_warnings,
+ get_error_message,
+ get_series_values,
+ index_series_by_label,
+ make_query_request,
+)
+
+# quantilesDD carries 0.01 relative accuracy and the log-spaced observations put
+# neighbouring ranks ~1.25% apart, so a percentile can land a few percent off
+PERCENTILE_TOLERANCE = 0.05
+
+
+ "space_aggregation, frontend_first, frontend_last, backend_first, backend_last",
+ [
+ ("p50", 118, 153, 711, 921),
+ ("p95", 1108, 1435, 6651, 8613),
+ ("p99", 1352, 1751, 8113, 10507),
+ ],
+)
[email protected]("time_aggregation", ["", "rate"])
+def test_exp_histogram_percentile_delta_grouped(
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+ insert_exp_histogram_metrics: Callable[[list[ExpHistogramMetrics]], None],
+ time_aggregation: str,
+ space_aggregation: str,
+ frontend_first: float,
+ frontend_last: float,
+ backend_first: float,
+ backend_last: float,
+) -> None:
+ now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
+ start_ms = int((now - timedelta(minutes=65)).timestamp() * 1000)
+ end_ms = int(now.timestamp() * 1000)
+ metric_name = "test_exp_histogram_latency"
+
+ insert_exp_histogram_metrics(
+ [
+ ExpHistogramMetrics(
+ metric_name=metric_name,
+ # log-spaced latencies with a long tail, drifting ~30% higher across
+ # the hour so each point carries a distinct distribution
+ observations=[round(base * 1.0125**rank * (1 + minute / 200)) for rank in range(400)],
+ labels={"service.name": service},
+ timestamp=now - timedelta(minutes=60 - minute),
+ temporality="Delta",
+ )
+ for service, base in (("frontend", 10), ("backend", 60))
+ for minute in range(60)
+ ]
+ )
+
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+ query = build_builder_query(
+ "A",
+ metric_name,
+ time_aggregation,
+ space_aggregation,
+ temporality="delta",
+ group_by=["service.name"],
+ )
+
+ response = make_query_request(signoz, token, start_ms, end_ms, [query])
+ assert response.status_code == HTTPStatus.OK, response.text
+
+ data = response.json()
+ assert get_all_warnings(data) == [], f"unexpected warnings: {get_all_warnings(data)}"
+
+ series_by_service = index_series_by_label(get_all_series(data, "A"), "service.name")
+ assert set(series_by_service.keys()) == {"frontend", "backend"}, f"got series {set(series_by_service.keys())}"
+
+ for service, first, last in (
+ ("frontend", frontend_first, frontend_last),
+ ("backend", backend_first, backend_last),
+ ):
+ values = [point["value"] for point in sorted(series_by_service[service]["values"], key=lambda point: point["timestamp"])]
+ assert len(values) == 60, f"{service}: expected a point per minute, got {len(values)}"
+ assert values[0] == pytest.approx(first, rel=PERCENTILE_TOLERANCE), f"{service} {space_aggregation} at the oldest point: got {values[0]}, want ~{first}"
+ assert values[-1] == pytest.approx(last, rel=PERCENTILE_TOLERANCE), f"{service} {space_aggregation} at the newest point: got {values[-1]}, want ~{last}"
+ # every observation drifts up minute over minute, so the sketch must too
+ assert values == sorted(values), f"{service} {space_aggregation} is not non-decreasing: {values}"
+
+
+def test_exp_histogram_percentile_delta_merges_across_series(
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+ insert_exp_histogram_metrics: Callable[[list[ExpHistogramMetrics]], None],
+) -> None:
+ now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
+ start_ms = int((now - timedelta(minutes=65)).timestamp() * 1000)
+ end_ms = int(now.timestamp() * 1000)
+ metric_name = "test_exp_histogram_latency_merged"
+
+ insert_exp_histogram_metrics(
+ [
+ ExpHistogramMetrics(
+ metric_name=metric_name,
+ observations=[round(base * 1.0125**rank * (1 + minute / 200)) for rank in range(400)],
+ labels={"service.name": service},
+ timestamp=now - timedelta(minutes=60 - minute),
+ temporality="Delta",
+ )
+ for service, base in (("frontend", 10), ("backend", 60))
+ for minute in range(60)
+ ]
+ )
+
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+ query = build_builder_query("A", metric_name, "", "p95", temporality="delta")
+
+ response = make_query_request(signoz, token, start_ms, end_ms, [query])
+ assert response.status_code == HTTPStatus.OK, response.text
+
+ data = response.json()
+ assert get_all_warnings(data) == [], f"unexpected warnings: {get_all_warnings(data)}"
+
+ # both services' sketches merge into one, so p95 sits well above the frontend's
+ # own p95 (~1108) and below the backend's (~6651)
+ values = [point["value"] for point in sorted(get_series_values(data, "A"), key=lambda point: point["timestamp"])]
+ assert len(values) == 60, f"expected a point per minute, got {len(values)}"
+ assert values[0] == pytest.approx(5188, rel=PERCENTILE_TOLERANCE), f"oldest point: got {values[0]}, want ~5188"
+ assert values[-1] == pytest.approx(6718, rel=PERCENTILE_TOLERANCE), f"newest point: got {values[-1]}, want ~6718"
+
+
+def test_exp_histogram_percentile_cumulative_is_rejected(
+ signoz: types.SigNoz,
+ create_user_admin: None, # pylint: disable=unused-argument
+ get_token: Callable[[str, str], str],
+ insert_exp_histogram_metrics: Callable[[list[ExpHistogramMetrics]], None],
+) -> None:
+ now = datetime.now(tz=UTC).replace(second=0, microsecond=0)
+ start_ms = int((now - timedelta(minutes=65)).timestamp() * 1000)
+ end_ms = int(now.timestamp() * 1000)
+ metric_name = "test_exp_histogram_latency_cumulative"
+
+ insert_exp_histogram_metrics(
+ [
+ ExpHistogramMetrics(
+ metric_name=metric_name,
+ observations=[round(10 * 1.0125**rank * (1 + minute / 200)) for rank in range(400)],
+ labels={"service.name": "frontend"},
+ timestamp=now - timedelta(minutes=60 - minute),
+ temporality="Cumulative",
+ )
+ for minute in range(60)
+ ]
+ )
+
+ token = get_token(USER_ADMIN_EMAIL, USER_ADMIN_PASSWORD)
+ query = build_builder_query("A", metric_name, "", "p95", temporality="cumulative")
+
+ response = make_query_request(signoz, token, start_ms, end_ms, [query])
+
+ # the request is well formed; it is the stored metric that cannot be read, so
+ # this is reported as unsupported rather than as bad input
+ assert response.status_code == HTTPStatus.NOT_IMPLEMENTED, response.text
+ assert "only `delta` exponential histograms are supported" in get_error_message(response.json()), response.text