: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(logs): return per-value counts from the values endpoint (#64640)
Jon McCallum01357c0198e8
diff --git a/frontend/src/queries/schema.json b/frontend/src/queries/schema.jsonindex 6ae25b19a7f7..b83ab78f5830 100644--- a/frontend/src/queries/schema.json+++ b/frontend/src/queries/schema.json@@ -25951,6 +25951,10 @@"LogValueResult": {"additionalProperties": false,"properties": {+ "count": {+ "$ref": "#/definitions/integer",+ "description": "Number of log records with this attribute value, over the current date range, service, and resource filters."+ },"id": {"type": "string"},diff --git a/frontend/src/queries/schema/schema-general.ts b/frontend/src/queries/schema/schema-general.tsindex 1060bdba5339..c71d1ac2808f 100644--- a/frontend/src/queries/schema/schema-general.ts+++ b/frontend/src/queries/schema/schema-general.ts@@ -3182,6 +3182,8 @@ export interface LogAttributesQueryResponse extends AnalyticsQueryResponseBase {export interface LogValueResult {id: stringname: string+ /** Number of log records with this attribute value, over the current date range, service, and resource filters. */+ count?: integer}export interface LogValuesQueryResponse extends AnalyticsQueryResponseBase {diff --git a/posthog/schema.py b/posthog/schema.pyindex 4595f04c7c3b..22150984f34a 100644--- a/posthog/schema.py+++ b/posthog/schema.py@@ -1633,14 +1633,6 @@ class LogAttributeResult(BaseModel):propertyFilterType: str = Field(..., description="Either 'log_attribute' or 'log_resource_attribute'.")-class LogValueResult(BaseModel):- model_config = ConfigDict(- extra="forbid",- )- id: str- name: str--class LogsAlertStateChangeSignalExtra(BaseModel):model_config = ConfigDict(extra="forbid",@@ -5617,6 +5609,21 @@ class LogPropertyFilter(BaseModel):value: list[str | float | bool] | str | float | bool | None = None+class LogValueResult(BaseModel):+ model_config = ConfigDict(+ extra="forbid",+ )+ count: int | None = Field(+ default=None,+ description=(+ "Number of log records with this attribute value, over the current date"+ " range, service, and resource filters."+ ),+ )+ id: str+ name: str++class LogsAlertStateChangeSignalInput(BaseModel):model_config = ConfigDict(extra="forbid",diff --git a/products/logs/backend/log_values_query_runner.py b/products/logs/backend/log_values_query_runner.pyindex 8503498cd94f..a1ae27effaf4 100644--- a/products/logs/backend/log_values_query_runner.py+++ b/products/logs/backend/log_values_query_runner.py@@ -44,12 +44,12 @@ class LogValuesQueryRunner(AnalyticsQueryRunner[LogValuesQueryResponse], LogsQuequery = parse_select("""SELECT- groupArray({limit})(attribute_value) as values,+ groupArray({limit})((attribute_value, value_count)) as values,count() as total_countFROM (SELECTattribute_value,- sum(attribute_count)+ sum(attribute_count) AS value_countFROM log_attributesWHERE time_bucket >= {date_from_start_of_interval}AND time_bucket <= {date_to_start_of_interval} + {one_interval_period}@@ -107,8 +107,8 @@ class LogValuesQueryRunner(AnalyticsQueryRunner[LogValuesQueryResponse], LogsQueformatted_results: list[LogValueResult] = []if isinstance(response.results, list) and len(response.results) > 0 and len(response.results[0]) > 0:- for result in response.results[0][0]:- entry = LogValueResult(id=result, name=result)+ for value, count in response.results[0][0]:+ entry = LogValueResult(id=value, name=value, count=count)formatted_results.append(entry)return LogValuesQueryResponse(results=formatted_results)diff --git a/products/logs/backend/presentation/views/api.py b/products/logs/backend/presentation/views/api.pyindex e7da0faa767f..dc54f1330895 100644--- a/products/logs/backend/presentation/views/api.py+++ b/products/logs/backend/presentation/views/api.py@@ -592,6 +592,10 @@ class _LogsAttributesResponseSerializer(serializers.Serializer):class _LogAttributeValueSerializer(serializers.Serializer):id = serializers.CharField(help_text="Attribute value (used as the identifier).")name = serializers.CharField(help_text="Display name — currently identical to `id`.")+ count = serializers.IntegerField(+ required=False,+ help_text="Number of log records with this attribute value, scoped to the current date range, service, and resource filters.",+ )class _LogsValuesResponseSerializer(serializers.Serializer):diff --git a/products/logs/backend/test/test_log_values_attributes.py b/products/logs/backend/test/test_log_values_attributes.pyindex 60de971eb24d..153dcf0dabba 100644--- a/products/logs/backend/test/test_log_values_attributes.py+++ b/products/logs/backend/test/test_log_values_attributes.py@@ -3,6 +3,7 @@ import jsonfrom posthog.test.base import APIBaseTest, ClickhouseTestMixin+from parameterized import parameterizedfrom rest_framework import statusfrom posthog.clickhouse.client import sync_execute@@ -159,6 +160,34 @@ class TestLogValuesAttributesTimezones(ClickhouseTestMixin, APIBaseTest):self.assertEqual(all_names, empty_names, "Empty value filter should return same values as no filter")self.assertEqual(set(all_names), {"info", "DEBUG", "PING", "more", "error"})+ @parameterized.expand(+ [+ ("level", "log"),+ ("service.name", "resource"),+ ]+ )+ def test_log_values_query_returns_counts(self, key, attribute_type):+ """Each value carries a positive integer count, and results are ordered most-observed first."""++ query_params = {+ "dateRange": '{"date_from": "2025-12-16T09:00:00Z", "date_to": "2025-12-16T11:00:00Z"}',+ "key": key,+ "attribute_type": attribute_type,+ }++ response = self.client.get(f"/api/projects/{self.team.pk}/logs/values", query_params)+ self.assertEqual(response.status_code, status.HTTP_200_OK)++ results = response.json()["results"]+ self.assertGreater(len(results), 0, f"Expected at least one {key} value")++ for result in results:+ self.assertIsInstance(result["count"], int, f"count missing/non-int for {result['name']}")+ self.assertGreater(result["count"], 0, f"count should be positive for {result['name']}")++ counts = [r["count"] for r in results]+ self.assertEqual(counts, sorted(counts, reverse=True), "Values should be ordered by count descending")+def test_log_attributes_search_values_off_by_default(self):"""Searching with `search_values` unset should match attribute keys only."""diff --git a/products/logs/frontend/generated/api.schemas.ts b/products/logs/frontend/generated/api.schemas.tsindex b3a5b43316ea..480aaae2f77b 100644--- a/products/logs/frontend/generated/api.schemas.ts+++ b/products/logs/frontend/generated/api.schemas.ts@@ -1344,6 +1344,8 @@ export interface _LogAttributeValueApi {id: string/** Display name — currently identical to `id`. */name: string+ /** Number of log records with this attribute value, scoped to the current date range, service, and resource filters. */+ count?: number}export interface _LogsValuesResponseApi {diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.tsindex 6aaeb6e8f0b9..1049be51a04c 100644--- a/services/mcp/src/api/generated.ts+++ b/services/mcp/src/api/generated.ts@@ -25037,6 +25037,8 @@ export namespace Schemas {}export interface LogValueResult {+ /** Number of log records with this attribute value, over the current date range, service, and resource filters. */+ count?: number | null;id: string;name: string;}@@ -49815,6 +49817,8 @@ export namespace Schemas {id: string;/** Display name — currently identical to `id`. */name: string;+ /** Number of log records with this attribute value, scoped to the current date range, service, and resource filters. */+ count?: number;}/**