: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
fix(logs): emit logs-count-ranges timestamps as UTC (Z-suffixed) (#64245)
Frank Hamandf18c4d6c82bd
diff --git a/products/logs/backend/count_ranges_query_runner.py b/products/logs/backend/count_ranges_query_runner.pyindex 1a3d03faa25d..9e3382e3e3c2 100644--- a/products/logs/backend/count_ranges_query_runner.py+++ b/products/logs/backend/count_ranges_query_runner.py@@ -17,6 +17,11 @@ DEFAULT_TARGET_BUCKETS = 10MAX_TARGET_BUCKETS = 100+def _utc_z(value: dt.datetime) -> str:+ """Format a naive UTC datetime as a Z-suffixed ISO 8601 string."""+ return value.replace(tzinfo=dt.UTC).isoformat().replace("+00:00", "Z")++class CountRangesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRunnerMixin):"""Returns adaptive-interval bucket counts for a filtered log stream.@@ -55,13 +60,16 @@ class CountRangesQueryRunner(AnalyticsQueryRunner[LogsQueryResponse], LogsQueryRranges: list[dict] = []for row in response.results:+ # ClickHouse returns naive datetimes holding UTC values. Emit explicit UTC+ # (Z-suffixed) ISO strings so a bucket's date_from/date_to can be fed straight+ # back into query-logs without timezone drift.bucket_start: dt.datetime = row[0]bucket_end: dt.datetime = row[1]count: int = row[2]ranges.append({- "date_from": bucket_start.isoformat(),- "date_to": bucket_end.isoformat(),+ "date_from": _utc_z(bucket_start),+ "date_to": _utc_z(bucket_end),"count": count,})diff --git a/products/logs/backend/test/test_count_ranges_api.py b/products/logs/backend/test/test_count_ranges_api.pyindex 86570fd1a983..d36c3bc9b5c4 100644--- a/products/logs/backend/test/test_count_ranges_api.py+++ b/products/logs/backend/test/test_count_ranges_api.py@@ -116,6 +116,14 @@ class TestCountRangesApi(ClickhouseTestMixin, APIBaseTest):dt_ = datetime.fromisoformat(bucket["date_to"])self.assertGreater(dt_, df)+ @freeze_time("2025-12-18T12:00:00Z")+ def test_bucket_timestamps_are_utc_z_suffixed(self):+ ranges = self._ranges({"dateRange": _DENSE_DAY, "targetBuckets": 24})["ranges"]+ self.assertGreater(len(ranges), 0)+ for bucket in ranges:+ self.assertTrue(bucket["date_from"].endswith("Z"), bucket["date_from"])+ self.assertTrue(bucket["date_to"].endswith("Z"), bucket["date_to"])+@freeze_time("2025-12-18T12:00:00Z")def test_recursion_happy_path(self):wide = self._ranges({"dateRange": _FIXTURE_WINDOW, "targetBuckets": 10})diff --git a/products/logs/mcp/prompts/logs-count-ranges.md b/products/logs/mcp/prompts/logs-count-ranges.mdindex c80326d9e412..f940143910cb 100644--- a/products/logs/mcp/prompts/logs-count-ranges.md+++ b/products/logs/mcp/prompts/logs-count-ranges.md@@ -47,8 +47,8 @@ Same shape as `query-logs`. Applied **before** bucketing.```json{"ranges": [- { "date_from": "2026-04-26T00:00:00", "date_to": "2026-04-26T02:24:00", "count": 1024 },- { "date_from": "2026-04-26T02:24:00", "date_to": "2026-04-26T04:48:00", "count": 47 }+ { "date_from": "2026-04-26T00:00:00Z", "date_to": "2026-04-26T02:24:00Z", "count": 1024 },+ { "date_from": "2026-04-26T02:24:00Z", "date_to": "2026-04-26T04:48:00Z", "count": 47 }],"interval": "2h"}@@ -74,14 +74,14 @@ Same shape as `query-logs`. Applied **before** bucketing.## Drill into the densest hour from a previous call-After picking the densest bucket from the response above (say `{date_from: "2026-04-26T15:00:00", date_to: "2026-04-26T16:00:00", count: 894}`):+After picking the densest bucket from the response above (say `{date_from: "2026-04-26T15:00:00Z", date_to: "2026-04-26T16:00:00Z", count: 894}`):```json{"query": {"dateRange": {- "date_from": "2026-04-26T15:00:00",- "date_to": "2026-04-26T16:00:00"+ "date_from": "2026-04-26T15:00:00Z",+ "date_to": "2026-04-26T16:00:00Z"},"targetBuckets": 12,"serviceNames": ["api-gateway"],diff --git a/services/mcp/schema/generated-tool-definitions.json b/services/mcp/schema/generated-tool-definitions.jsonindex c4108e8c2aef..fe4015d19a6c 100644--- a/services/mcp/schema/generated-tool-definitions.json+++ b/services/mcp/schema/generated-tool-definitions.json@@ -4732,7 +4732,7 @@}},"logs-count-ranges": {- "description": "Get adaptive-interval bucket counts for a filtered log stream. Returns a flat list of `{date_from, date_to, count}` buckets covering the requested window. Modeled on Elasticsearch's `auto_date_histogram` — caller specifies a target bucket count, the engine picks the interval.\n\nUse this to find **where the volume is concentrated** before pulling rows. Cheaper than `query-logs`, more agent-friendly than `logs-sparkline-query` (each bucket carries explicit `date_from`/`date_to` you can feed straight back as the next call's `dateRange` to drill in).\n\n# When to use this vs other tools\n\n- **`logs-count`** — total volume in a window. Scalar.\n- **`logs-count-ranges`** (this tool) — _when_ in the window the volume sits. Time-bucketed.\n- **`query-logs`** — pull individual log rows. Most expensive; only call after counts confirm the window is right-sized.\n\n# Recursion pattern (the main reason this tool exists)\n\nUse the response to narrow into a sub-range without reasoning about interval width:\n\n1. Call `logs-count-ranges` with the user's window (e.g. last 24h).\n2. Pick the bucket(s) of interest (densest, an obvious spike, an unexpectedly empty stretch).\n3. Call `logs-count-ranges` again with that bucket's `date_from` and `date_to` as the next `dateRange`.\n4. Repeat up to ~3–4 levels — stop when buckets are shorter than your precision goal (e.g. 1 minute).\n5. Once narrowed, call `query-logs` for the actual rows.\n\nThis is the same pattern Elasticsearch users follow with `auto_date_histogram`. Keep recursion shallow — every call is cheap individually but they multiply quickly.\n\n# Parameters\n\nAll parameters must be nested inside a `query` object.\n\n## query.dateRange\n\nWindow to bucket. Defaults to the last hour (`-1h`). Same format as `query-logs`.\n\n## query.targetBuckets\n\nApproximate bucket count. Defaults to **10**, max 100. The engine picks the interval adaptively from a fixed list (1/5/10s, 1/2/5/10/15/30/60/120/240/360/720/1440m) to land near this target — actual count may differ slightly. Empty buckets are dropped, so the response can have fewer rows than `targetBuckets`.\n\nPick a value based on what you're doing:\n\n- **10** (default) — overview, finding spikes, \"is this concentrated or spread out?\"\n- **20–30** — characterising a known busy window\n- **50+** — high-resolution drill-down, only when you know the window is small\n\n## query.severityLevels, query.serviceNames, query.searchTerm, query.filterGroup\n\nSame shape as `query-logs`. Applied **before** bucketing.\n\n# Response\n\n```json\n{\n \"ranges\": [\n { \"date_from\": \"2026-04-26T00:00:00\", \"date_to\": \"2026-04-26T02:24:00\", \"count\": 1024 },\n { \"date_from\": \"2026-04-26T02:24:00\", \"date_to\": \"2026-04-26T04:48:00\", \"count\": 47 }\n ],\n \"interval\": \"2h\"\n}\n```\n\n- `ranges` — buckets ordered by `date_from` ascending. **Empty buckets are omitted** — infer gaps by comparing each bucket's `date_to` to the next bucket's `date_from`.\n- `interval` — short-form duration of the chosen bucket width (`1s` / `5m` / `1h` / `1d`). Informational only — for follow-up queries, use the per-bucket `date_from`/`date_to`.\n\n# Examples\n\n## Find when errors spiked over the last day\n\n```json\n{\n \"query\": {\n \"dateRange\": { \"date_from\": \"-1d\" },\n \"targetBuckets\": 24,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n## Drill into the densest hour from a previous call\n\nAfter picking the densest bucket from the response above (say `{date_from: \"2026-04-26T15:00:00\", date_to: \"2026-04-26T16:00:00\", count: 894}`):\n\n```json\n{\n \"query\": {\n \"dateRange\": {\n \"date_from\": \"2026-04-26T15:00:00\",\n \"date_to\": \"2026-04-26T16:00:00\"\n },\n \"targetBuckets\": 12,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n# Reminders\n\n- Cap recursion at ~3–4 levels. If your bucket width drops below your precision goal (e.g. 1 minute), stop and call `query-logs`.\n- Empty windows return `{\"ranges\": [], \"interval\": \"...\"}` — that's not an error, it's \"I asked, nothing matched.\"\n- Always include `serviceNames` or a resource attribute filter, just like `query-logs`. Don't bucket the entire team's log stream.",+ "description": "Get adaptive-interval bucket counts for a filtered log stream. Returns a flat list of `{date_from, date_to, count}` buckets covering the requested window. Modeled on Elasticsearch's `auto_date_histogram` — caller specifies a target bucket count, the engine picks the interval.\n\nUse this to find **where the volume is concentrated** before pulling rows. Cheaper than `query-logs`, more agent-friendly than `logs-sparkline-query` (each bucket carries explicit `date_from`/`date_to` you can feed straight back as the next call's `dateRange` to drill in).\n\n# When to use this vs other tools\n\n- **`logs-count`** — total volume in a window. Scalar.\n- **`logs-count-ranges`** (this tool) — _when_ in the window the volume sits. Time-bucketed.\n- **`query-logs`** — pull individual log rows. Most expensive; only call after counts confirm the window is right-sized.\n\n# Recursion pattern (the main reason this tool exists)\n\nUse the response to narrow into a sub-range without reasoning about interval width:\n\n1. Call `logs-count-ranges` with the user's window (e.g. last 24h).\n2. Pick the bucket(s) of interest (densest, an obvious spike, an unexpectedly empty stretch).\n3. Call `logs-count-ranges` again with that bucket's `date_from` and `date_to` as the next `dateRange`.\n4. Repeat up to ~3–4 levels — stop when buckets are shorter than your precision goal (e.g. 1 minute).\n5. Once narrowed, call `query-logs` for the actual rows.\n\nThis is the same pattern Elasticsearch users follow with `auto_date_histogram`. Keep recursion shallow — every call is cheap individually but they multiply quickly.\n\n# Parameters\n\nAll parameters must be nested inside a `query` object.\n\n## query.dateRange\n\nWindow to bucket. Defaults to the last hour (`-1h`). Same format as `query-logs`.\n\n## query.targetBuckets\n\nApproximate bucket count. Defaults to **10**, max 100. The engine picks the interval adaptively from a fixed list (1/5/10s, 1/2/5/10/15/30/60/120/240/360/720/1440m) to land near this target — actual count may differ slightly. Empty buckets are dropped, so the response can have fewer rows than `targetBuckets`.\n\nPick a value based on what you're doing:\n\n- **10** (default) — overview, finding spikes, \"is this concentrated or spread out?\"\n- **20–30** — characterising a known busy window\n- **50+** — high-resolution drill-down, only when you know the window is small\n\n## query.severityLevels, query.serviceNames, query.searchTerm, query.filterGroup\n\nSame shape as `query-logs`. Applied **before** bucketing.\n\n# Response\n\n```json\n{\n \"ranges\": [\n { \"date_from\": \"2026-04-26T00:00:00Z\", \"date_to\": \"2026-04-26T02:24:00Z\", \"count\": 1024 },\n { \"date_from\": \"2026-04-26T02:24:00Z\", \"date_to\": \"2026-04-26T04:48:00Z\", \"count\": 47 }\n ],\n \"interval\": \"2h\"\n}\n```\n\n- `ranges` — buckets ordered by `date_from` ascending. **Empty buckets are omitted** — infer gaps by comparing each bucket's `date_to` to the next bucket's `date_from`.\n- `interval` — short-form duration of the chosen bucket width (`1s` / `5m` / `1h` / `1d`). Informational only — for follow-up queries, use the per-bucket `date_from`/`date_to`.\n\n# Examples\n\n## Find when errors spiked over the last day\n\n```json\n{\n \"query\": {\n \"dateRange\": { \"date_from\": \"-1d\" },\n \"targetBuckets\": 24,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n## Drill into the densest hour from a previous call\n\nAfter picking the densest bucket from the response above (say `{date_from: \"2026-04-26T15:00:00Z\", date_to: \"2026-04-26T16:00:00Z\", count: 894}`):\n\n```json\n{\n \"query\": {\n \"dateRange\": {\n \"date_from\": \"2026-04-26T15:00:00Z\",\n \"date_to\": \"2026-04-26T16:00:00Z\"\n },\n \"targetBuckets\": 12,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n# Reminders\n\n- Cap recursion at ~3–4 levels. If your bucket width drops below your precision goal (e.g. 1 minute), stop and call `query-logs`.\n- Empty windows return `{\"ranges\": [], \"interval\": \"...\"}` — that's not an error, it's \"I asked, nothing matched.\"\n- Always include `serviceNames` or a resource attribute filter, just like `query-logs`. Don't bucket the entire team's log stream.","category": "Logs","feature": "logs","summary": "Count logs by time bucket",diff --git a/services/mcp/schema/tool-definitions-all.json b/services/mcp/schema/tool-definitions-all.jsonindex f0f1d3c3be90..19240d9821fb 100644--- a/services/mcp/schema/tool-definitions-all.json+++ b/services/mcp/schema/tool-definitions-all.json@@ -5140,7 +5140,7 @@}},"logs-count-ranges": {- "description": "Get adaptive-interval bucket counts for a filtered log stream. Returns a flat list of `{date_from, date_to, count}` buckets covering the requested window. Modeled on Elasticsearch's `auto_date_histogram` — caller specifies a target bucket count, the engine picks the interval.\n\nUse this to find **where the volume is concentrated** before pulling rows. Cheaper than `query-logs`, more agent-friendly than `logs-sparkline-query` (each bucket carries explicit `date_from`/`date_to` you can feed straight back as the next call's `dateRange` to drill in).\n\n# When to use this vs other tools\n\n- **`logs-count`** — total volume in a window. Scalar.\n- **`logs-count-ranges`** (this tool) — _when_ in the window the volume sits. Time-bucketed.\n- **`query-logs`** — pull individual log rows. Most expensive; only call after counts confirm the window is right-sized.\n\n# Recursion pattern (the main reason this tool exists)\n\nUse the response to narrow into a sub-range without reasoning about interval width:\n\n1. Call `logs-count-ranges` with the user's window (e.g. last 24h).\n2. Pick the bucket(s) of interest (densest, an obvious spike, an unexpectedly empty stretch).\n3. Call `logs-count-ranges` again with that bucket's `date_from` and `date_to` as the next `dateRange`.\n4. Repeat up to ~3–4 levels — stop when buckets are shorter than your precision goal (e.g. 1 minute).\n5. Once narrowed, call `query-logs` for the actual rows.\n\nThis is the same pattern Elasticsearch users follow with `auto_date_histogram`. Keep recursion shallow — every call is cheap individually but they multiply quickly.\n\n# Parameters\n\nAll parameters must be nested inside a `query` object.\n\n## query.dateRange\n\nWindow to bucket. Defaults to the last hour (`-1h`). Same format as `query-logs`.\n\n## query.targetBuckets\n\nApproximate bucket count. Defaults to **10**, max 100. The engine picks the interval adaptively from a fixed list (1/5/10s, 1/2/5/10/15/30/60/120/240/360/720/1440m) to land near this target — actual count may differ slightly. Empty buckets are dropped, so the response can have fewer rows than `targetBuckets`.\n\nPick a value based on what you're doing:\n\n- **10** (default) — overview, finding spikes, \"is this concentrated or spread out?\"\n- **20–30** — characterising a known busy window\n- **50+** — high-resolution drill-down, only when you know the window is small\n\n## query.severityLevels, query.serviceNames, query.searchTerm, query.filterGroup\n\nSame shape as `query-logs`. Applied **before** bucketing.\n\n# Response\n\n```json\n{\n \"ranges\": [\n { \"date_from\": \"2026-04-26T00:00:00\", \"date_to\": \"2026-04-26T02:24:00\", \"count\": 1024 },\n { \"date_from\": \"2026-04-26T02:24:00\", \"date_to\": \"2026-04-26T04:48:00\", \"count\": 47 }\n ],\n \"interval\": \"2h\"\n}\n```\n\n- `ranges` — buckets ordered by `date_from` ascending. **Empty buckets are omitted** — infer gaps by comparing each bucket's `date_to` to the next bucket's `date_from`.\n- `interval` — short-form duration of the chosen bucket width (`1s` / `5m` / `1h` / `1d`). Informational only — for follow-up queries, use the per-bucket `date_from`/`date_to`.\n\n# Examples\n\n## Find when errors spiked over the last day\n\n```json\n{\n \"query\": {\n \"dateRange\": { \"date_from\": \"-1d\" },\n \"targetBuckets\": 24,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n## Drill into the densest hour from a previous call\n\nAfter picking the densest bucket from the response above (say `{date_from: \"2026-04-26T15:00:00\", date_to: \"2026-04-26T16:00:00\", count: 894}`):\n\n```json\n{\n \"query\": {\n \"dateRange\": {\n \"date_from\": \"2026-04-26T15:00:00\",\n \"date_to\": \"2026-04-26T16:00:00\"\n },\n \"targetBuckets\": 12,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n# Reminders\n\n- Cap recursion at ~3–4 levels. If your bucket width drops below your precision goal (e.g. 1 minute), stop and call `query-logs`.\n- Empty windows return `{\"ranges\": [], \"interval\": \"...\"}` — that's not an error, it's \"I asked, nothing matched.\"\n- Always include `serviceNames` or a resource attribute filter, just like `query-logs`. Don't bucket the entire team's log stream.",+ "description": "Get adaptive-interval bucket counts for a filtered log stream. Returns a flat list of `{date_from, date_to, count}` buckets covering the requested window. Modeled on Elasticsearch's `auto_date_histogram` — caller specifies a target bucket count, the engine picks the interval.\n\nUse this to find **where the volume is concentrated** before pulling rows. Cheaper than `query-logs`, more agent-friendly than `logs-sparkline-query` (each bucket carries explicit `date_from`/`date_to` you can feed straight back as the next call's `dateRange` to drill in).\n\n# When to use this vs other tools\n\n- **`logs-count`** — total volume in a window. Scalar.\n- **`logs-count-ranges`** (this tool) — _when_ in the window the volume sits. Time-bucketed.\n- **`query-logs`** — pull individual log rows. Most expensive; only call after counts confirm the window is right-sized.\n\n# Recursion pattern (the main reason this tool exists)\n\nUse the response to narrow into a sub-range without reasoning about interval width:\n\n1. Call `logs-count-ranges` with the user's window (e.g. last 24h).\n2. Pick the bucket(s) of interest (densest, an obvious spike, an unexpectedly empty stretch).\n3. Call `logs-count-ranges` again with that bucket's `date_from` and `date_to` as the next `dateRange`.\n4. Repeat up to ~3–4 levels — stop when buckets are shorter than your precision goal (e.g. 1 minute).\n5. Once narrowed, call `query-logs` for the actual rows.\n\nThis is the same pattern Elasticsearch users follow with `auto_date_histogram`. Keep recursion shallow — every call is cheap individually but they multiply quickly.\n\n# Parameters\n\nAll parameters must be nested inside a `query` object.\n\n## query.dateRange\n\nWindow to bucket. Defaults to the last hour (`-1h`). Same format as `query-logs`.\n\n## query.targetBuckets\n\nApproximate bucket count. Defaults to **10**, max 100. The engine picks the interval adaptively from a fixed list (1/5/10s, 1/2/5/10/15/30/60/120/240/360/720/1440m) to land near this target — actual count may differ slightly. Empty buckets are dropped, so the response can have fewer rows than `targetBuckets`.\n\nPick a value based on what you're doing:\n\n- **10** (default) — overview, finding spikes, \"is this concentrated or spread out?\"\n- **20–30** — characterising a known busy window\n- **50+** — high-resolution drill-down, only when you know the window is small\n\n## query.severityLevels, query.serviceNames, query.searchTerm, query.filterGroup\n\nSame shape as `query-logs`. Applied **before** bucketing.\n\n# Response\n\n```json\n{\n \"ranges\": [\n { \"date_from\": \"2026-04-26T00:00:00Z\", \"date_to\": \"2026-04-26T02:24:00Z\", \"count\": 1024 },\n { \"date_from\": \"2026-04-26T02:24:00Z\", \"date_to\": \"2026-04-26T04:48:00Z\", \"count\": 47 }\n ],\n \"interval\": \"2h\"\n}\n```\n\n- `ranges` — buckets ordered by `date_from` ascending. **Empty buckets are omitted** — infer gaps by comparing each bucket's `date_to` to the next bucket's `date_from`.\n- `interval` — short-form duration of the chosen bucket width (`1s` / `5m` / `1h` / `1d`). Informational only — for follow-up queries, use the per-bucket `date_from`/`date_to`.\n\n# Examples\n\n## Find when errors spiked over the last day\n\n```json\n{\n \"query\": {\n \"dateRange\": { \"date_from\": \"-1d\" },\n \"targetBuckets\": 24,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n## Drill into the densest hour from a previous call\n\nAfter picking the densest bucket from the response above (say `{date_from: \"2026-04-26T15:00:00Z\", date_to: \"2026-04-26T16:00:00Z\", count: 894}`):\n\n```json\n{\n \"query\": {\n \"dateRange\": {\n \"date_from\": \"2026-04-26T15:00:00Z\",\n \"date_to\": \"2026-04-26T16:00:00Z\"\n },\n \"targetBuckets\": 12,\n \"serviceNames\": [\"api-gateway\"],\n \"severityLevels\": [\"error\", \"fatal\"]\n }\n}\n```\n\n# Reminders\n\n- Cap recursion at ~3–4 levels. If your bucket width drops below your precision goal (e.g. 1 minute), stop and call `query-logs`.\n- Empty windows return `{\"ranges\": [], \"interval\": \"...\"}` — that's not an error, it's \"I asked, nothing matched.\"\n- Always include `serviceNames` or a resource attribute filter, just like `query-logs`. Don't bucket the entire team's log stream.","category": "Logs","feature": "logs","summary": "Count logs by time bucket",