: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): run the volume rollup once per bucket, not once per tick (#83676)
Jon McCallumea6b22a6881f
diff --git a/products/logs/backend/temporal/volume_tick/activities.py b/products/logs/backend/temporal/volume_tick/activities.pyindex 42e508b5b1b4..77ec9dcb92d2 100644--- a/products/logs/backend/temporal/volume_tick/activities.py+++ b/products/logs/backend/temporal/volume_tick/activities.py@@ -1,5 +1,6 @@import timeimport dataclasses+from collections.abc import Sequencefrom datetime import UTC, datetime, timedeltaimport structlog@@ -117,13 +118,27 @@ class TimedRollupPreview:duration_ms: int-async def _preview_due_bucket(due: DueBucket) -> TimedRollupPreview | None:- """Measure the due bucket's rollup for the allowlisted teams, or nothing if- the allowlist is empty. Once the commit protocol lands this becomes the write."""- if not TEAM_ALLOWLIST:+def teams_due_in_shard(team_ids: Sequence[int], minute_shard: int) -> list[int]:+ """The allowlisted teams this tick is responsible for.++ A bucket stays due for every minute of the next one, so a tick that ignored the+ shard would run the same scan BUCKET_MINUTES times over. The shard picks the one+ minute each team belongs to, matching the residue the discovery query counts.+ """+ return [team_id for team_id in team_ids if team_id % BUCKET_MINUTES == minute_shard]+++async def _preview_due_bucket(due: DueBucket, minute_shard: int) -> TimedRollupPreview | None:+ """Measure the due bucket's rollup for the allowlisted teams in this shard.++ Once the commit protocol lands this becomes the write, where an unsharded read+ would produce duplicate generations rather than merely wasted scans.+ """+ due_teams = teams_due_in_shard(TEAM_ALLOWLIST, minute_shard)+ if not due_teams:return Nonestarted = time.monotonic()- preview = await _preview_rollup_async(team_ids=TEAM_ALLOWLIST, start=due.start, end=due.end)+ preview = await _preview_rollup_async(team_ids=due_teams, start=due.start, end=due.end)return TimedRollupPreview(preview=preview, duration_ms=int((time.monotonic() - started) * 1000))@@ -137,18 +152,21 @@ async def volume_tick_heartbeat_activity(input: VolumeTickInput) -> VolumeTickOudue = due_bucket_bounds(ticked_at)# One team cohort per minute of the bucket: the every-minute schedule smears# teams across the bucket's minutes, so team_id % BUCKET_MINUTES is the shard.- # Observed, not enforced: no per-shard work happens yet.minute_shard = ticked_at.minute % BUCKET_MINUTESstarted = time.monotonic()try:counts = await _count_teams_with_logs_async(ticked_at - TEAMS_WITH_LOGS_WINDOW, ticked_at, minute_shard)- timed = await _preview_due_bucket(due)+ # Discovery's own duration, measured and recorded before the rollup runs.+ # The two queries differ by an order of magnitude, so one timer covering+ # both reports neither, and recording here keeps discovery latency+ # observable on the ticks whose rollup then fails.+ duration_ms = int((time.monotonic() - started) * 1000)+ record_clickhouse_duration(duration_ms)+ timed = await _preview_due_bucket(due, minute_shard)except Exception:increment_tick_runs("error")raise- duration_ms = int((time.monotonic() - started) * 1000)- record_clickhouse_duration(duration_ms)record_teams_with_logs(counts.total)if timed is not None:record_rollup_duration(timed.duration_ms)diff --git a/products/logs/backend/temporal/volume_tick/metrics.py b/products/logs/backend/temporal/volume_tick/metrics.pyindex 9ea135680a11..f2f077f18c37 100644--- a/products/logs/backend/temporal/volume_tick/metrics.py+++ b/products/logs/backend/temporal/volume_tick/metrics.py@@ -14,9 +14,10 @@ LOGS_VOLUME_TICK_LATENCY_HISTOGRAM_METRICS = ("logs_volume_tick_rollup_duration_ms",)-# Dense between 750ms and 3s because the discovery query lives there. Coarser-# steps put every sample in one bucket, so the high quantiles interpolate to a-# bucket edge and report a boundary rather than a latency.+# Dense between 750ms and 3s because the discovery query lives there, and again+# between 3s and 7.5s because the rollup query does. Coarser steps put every+# sample in one bucket, so the high quantiles interpolate to a bucket edge and+# report a boundary rather than a latency.LOGS_VOLUME_TICK_LATENCY_HISTOGRAM_BUCKETS = [50.0,100.0,@@ -28,7 +29,9 @@ LOGS_VOLUME_TICK_LATENCY_HISTOGRAM_BUCKETS = [2_000.0,2_500.0,3_000.0,+ 4_000.0,5_000.0,+ 7_500.0,10_000.0,30_000.0,60_000.0,diff --git a/products/logs/backend/test/test_volume_tick_workflow.py b/products/logs/backend/test/test_volume_tick_workflow.pyindex 6dd47fde4c9a..4032a7cec570 100644--- a/products/logs/backend/test/test_volume_tick_workflow.py+++ b/products/logs/backend/test/test_volume_tick_workflow.py@@ -22,6 +22,7 @@ from products.logs.backend.temporal.volume_tick.activities import (VolumeTickOutput,count_teams_with_logs,due_bucket_bounds,+ teams_due_in_shard,volume_tick_heartbeat_activity,)from products.logs.backend.temporal.volume_tick.constants import BUCKET_MINUTES, WORKFLOW_NAME@@ -143,10 +144,31 @@ class TestCountTeamsWithLogs(ClickhouseTestMixin, BaseTest):assert after.due_in_shard == before.due_in_shard + 1+class TestTeamsDueInShard(SimpleTestCase):+ @parameterized.expand(+ [+ ("team_in_shard", [2], 2, [2]),+ ("team_in_another_shard", [2], 0, []),+ ("one_of_three", [1, 2, 3], 2, [2]),+ ("same_residue_all_run", [2, 7, 12], 2, [2, 7, 12]),+ ("empty_allowlist", [], 2, []),+ ]+ )+ def test_only_teams_whose_residue_matches_the_minute_run(+ self, _name: str, team_ids: list[int], minute_shard: int, expected: list[int]+ ) -> None:+ # A bucket stays due for every minute of the next one. Without this filter+ # the tick would rerun the same scan once per minute, and the writer would+ # file a duplicate generation each time.+ assert teams_due_in_shard(team_ids, minute_shard) == expected++@pytest.mark.asyncio@pytest.mark.parametrize("allowlist,measures_the_bucket",- [((), False), ((1,), True)],+ # The second allowlist covers every residue mod BUCKET_MINUTES, so exactly one+ # team is due whatever minute the test runs in.+ [((), False), (tuple(range(1, BUCKET_MINUTES + 1)), True)],ids=["empty_allowlist_skips", "allowlisted_team_is_measured"],)async def test_the_tick_measures_the_due_bucket_only_for_allowlisted_teams(