Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(metrics): omit non-finite sparkline buckets (#112742)

Daniel Visca8617e85cafbd
diff --git a/products/metrics/backend/metric_names_query_runner.py b/products/metrics/backend/metric_names_query_runner.py
index d0d20bb07b65..2bc9b160d6fc 100644
--- a/products/metrics/backend/metric_names_query_runner.py
+++ b/products/metrics/backend/metric_names_query_runner.py
@@ -1,5 +1,6 @@
"""Distinct metric names for a team's picker UI."""
+import math
import datetime as dt
from collections.abc import Sequence
from hashlib import sha256
@@ -263,7 +264,9 @@ class MetricNamesQueryRunner:
sparklines: dict[str, list[float]] = {}
for name, _bucket_start, bucket_value in response.results:
- sparklines.setdefault(name, []).append(float(bucket_value))
+ value = float(bucket_value)
+ if math.isfinite(value):
+ sparklines.setdefault(name, []).append(value)
return sparklines
def _series_scope_subquery(self, names: Sequence[str]) -> ast.SelectQuery:
diff --git a/products/metrics/backend/tests/test_metric_names_query_runner.py b/products/metrics/backend/tests/test_metric_names_query_runner.py
index 2e3164eeb735..a310d0f55432 100644
--- a/products/metrics/backend/tests/test_metric_names_query_runner.py
+++ b/products/metrics/backend/tests/test_metric_names_query_runner.py
@@ -290,8 +290,12 @@ class TestMetricsValuesAPI(ClickhouseTestMixin, APIBaseTest):
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.json(), {"results": []})
- def test_values_returns_metric_names(self):
+ @parameterized.expand([("finite", 1.0), ("overflowing_average", 1e308)])
+ def test_values_returns_metric_names(self, _name: str, value: float):
anchor = timezone.now().replace(microsecond=0) - dt.timedelta(minutes=5)
+ earlier = anchor - dt.timedelta(minutes=20)
+ _seed_point(team_id=self.team.id, metric_name="m1", value=value, timestamp=earlier)
+ seed_metric(team_id=self.team.id, metric_name="m1", points=[(earlier, value)], labels={"shard": "other"})
_seed_point(team_id=self.team.id, metric_name="m1", value=1.0, timestamp=anchor)
_seed_point(team_id=self.team.id, metric_name="m2", value=2.0, timestamp=anchor, metric_type="gauge")
@@ -301,6 +305,8 @@ class TestMetricsValuesAPI(ClickhouseTestMixin, APIBaseTest):
names = {row["name"] for row in body["results"]}
self.assertEqual(names, {"m1", "m2"})
self.assertIn("sparkline", body["results"][0])
+ sparkline = next(row["sparkline"] for row in body["results"] if row["name"] == "m1")
+ self.assertEqual(sparkline, [1.0, 1.0] if value == 1.0 else [1.0])
def test_names_returns_picker_fields_only(self):
anchor = timezone.now().replace(microsecond=0) - dt.timedelta(minutes=5)