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): list service.name once in attribute keys (#107345)

Daniel Viscafc02cd02c0a3
diff --git a/products/metrics/backend/metric_attributes_query_runner.py b/products/metrics/backend/metric_attributes_query_runner.py
index 190d97782a69..9e5bde1ec41b 100644
--- a/products/metrics/backend/metric_attributes_query_runner.py
+++ b/products/metrics/backend/metric_attributes_query_runner.py
@@ -57,7 +57,9 @@ def _validate_limit(limit: int) -> int:
class MetricAttributeKeysQueryRunner:
- """Attribute keys ordered by distinct value count from recent series."""
+ """Attribute keys ordered by distinct value count from recent series.
+ The synthetic `service_name` key only stands in for series without a `service.name`
+ resource attribute, so senders see the one spelling they emit."""
def __init__(
self,
@@ -126,7 +128,8 @@ class MetricAttributeKeysQueryRunner:
"""
SELECT
arrayJoin(arrayDistinct(arrayConcat(
- mapKeys(attributes), mapKeys(resource_attributes), ['service_name']
+ mapKeys(attributes), mapKeys(resource_attributes),
+ if(mapContains(resource_attributes, 'service.name'), [], ['service_name'])
))) AS attribute_key,
uniqCombined64(if(attribute_key IN ('service_name', 'service.name'), service_name,
if(arrayElement(resource_attributes, attribute_key) != '',
@@ -136,7 +139,8 @@ class MetricAttributeKeysQueryRunner:
WHERE last_seen >= {date_from}
AND {metric_name_filter}
AND (attribute_key ILIKE {search_pattern}
- OR (attribute_key = 'service_name' AND 'service.name' ILIKE {search_pattern}))
+ OR (attribute_key = 'service_name' AND 'service.name' ILIKE {search_pattern})
+ OR (attribute_key = 'service.name' AND 'service_name' ILIKE {search_pattern}))
GROUP BY attribute_key
ORDER BY value_count DESC, attribute_key ASC
LIMIT {limit}
diff --git a/products/metrics/backend/tests/test_metric_attributes.py b/products/metrics/backend/tests/test_metric_attributes.py
index 2b1402a9d757..4916be2c21b8 100644
--- a/products/metrics/backend/tests/test_metric_attributes.py
+++ b/products/metrics/backend/tests/test_metric_attributes.py
@@ -171,3 +171,30 @@ class TestMetricAttributeDistinctValuesAPI(ClickhouseTestMixin, APIBaseTest):
{"name": "env", "value_count": 1},
{"name": "service_name", "value_count": 1},
]
+
+ @parameterized.expand(
+ [
+ ("no_search", "", ["service.name", "env"]),
+ ("underscore_search", "service_name", ["service.name"]),
+ ("dotted_search", "service.name", ["service.name"]),
+ ]
+ )
+ def test_service_name_resource_attribute_is_listed_once(self, _name: str, search: str, expected: list[str]) -> None:
+ truncate_metrics_tables()
+ now = timezone.now().replace(second=0, microsecond=0)
+ for service in ("checkout", "billing"):
+ seed_metric(
+ team_id=self.team.id,
+ metric_name="job.duration",
+ service_name=service,
+ points=[(now - dt.timedelta(minutes=2), 1.0)],
+ labels={"env": "prod"},
+ resource_labels={"service.name": service},
+ )
+
+ response = self.client.get(
+ f"/api/projects/{self.team.id}/metrics/attributes/", {"metricName": "job.duration", "search": search}
+ )
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ assert [r["name"] for r in response.json()["results"]] == expected