: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-alerting): shard alerts across cron fires to spread CH load (#57813)
Jon McCallumb7ea8cf46872
diff --git a/products/logs/backend/alert_utils.py b/products/logs/backend/alert_utils.pyindex 9f63d4bb6300..9c054e23f158 100644--- a/products/logs/backend/alert_utils.py+++ b/products/logs/backend/alert_utils.py@@ -1,12 +1,39 @@from __future__ import annotationsfrom datetime import datetime, timedelta+from uuid import UUID++# How often the Temporal schedule fires. Drives shard granularity in+# `compute_shard_offset_seconds` — schedule fires every N seconds, so a+# cadence of M seconds has `M // N` shard slots available.+SCHEDULE_INTERVAL_SECONDS = 60+++def compute_shard_offset_seconds(alert_id: UUID, check_interval_minutes: int) -> int:+ """Deterministic per-alert offset within the cadence period.++ Spreads alerts across `cadence / SCHEDULE_INTERVAL_SECONDS` slots so cron+ fires pick up roughly equal slices instead of the whole fleet at once. With+ `SCHEDULE_INTERVAL_SECONDS=60` and a 5-min cadence, alerts distribute over+ 5 buckets at offsets [0, 60, 120, 180, 240] seconds.++ `alert_id.int` is stable across pod restarts (process-local `hash()` is not).+ For UUIDv7 IDs, the low bits are the random portion — the modulus operation+ naturally lands on those bits, so distribution is uniform.+ Cadences ≤ schedule interval get 1 shard (no spread possible).+ """+ cadence_seconds = check_interval_minutes * 60+ shard_count = max(1, cadence_seconds // SCHEDULE_INTERVAL_SECONDS)+ shard_index = alert_id.int % shard_count+ return shard_index * SCHEDULE_INTERVAL_SECONDSdef advance_next_check_at(current_next_check_at: datetime | None,check_interval_minutes: int,now: datetime,+ *,+ shard_offset_seconds: int = 0,) -> datetime:"""Schedule-relative advancement, snapped to the canonical cadence grid.@@ -18,6 +45,11 @@ def advance_next_check_at(sharing a cadence onto the same canonical grid regardless of when it wascreated.+ `shard_offset_seconds` shifts the canonical grid per-alert to spread load+ across cron fires (see `compute_shard_offset_seconds`). With offset=120 and+ a 5-min cadence, the alert lands on :02/:07/:12/... instead of :00/:05/:10.+ Default 0 = no shard, alert sits on the canonical grid.+Any drifted `next_check_at` — sub-minute drift OR minute-level offset (e.g.legacy alerts on a per-creation-time grid) — self-heals to canonical on itsnext return. Existing alerts heal lazily, one transient short-gap each (the@@ -44,7 +76,7 @@ def advance_next_check_at(# Bump by full interval (not 1 cadence-grid slot upward) when the floor# lands at/before `now`, so the alert stays on its canonical cadence grid.- snapped = _floor_to_cadence_grid(next_at, check_interval_minutes)+ snapped = _floor_to_cadence_grid(next_at, check_interval_minutes) + timedelta(seconds=shard_offset_seconds)if snapped <= now:snapped += intervalreturn snappeddiff --git a/products/logs/backend/temporal/activities.py b/products/logs/backend/temporal/activities.pyindex 9ccbd502fcfa..52fe3a63f674 100644--- a/products/logs/backend/temporal/activities.py+++ b/products/logs/backend/temporal/activities.py@@ -44,7 +44,7 @@ from products.logs.backend.alert_state_machine import (apply_outcome,evaluate_alert_check,)-from products.logs.backend.alert_utils import advance_next_check_at+from products.logs.backend.alert_utils import advance_next_check_at, compute_shard_offset_secondsfrom products.logs.backend.logs_url_params import build_logs_url_paramsfrom products.logs.backend.models import LogsAlertConfiguration, LogsAlertEventfrom products.logs.backend.temporal.constants import (@@ -754,7 +754,12 @@ def _stage_alert_for_save(dispatched: _DispatchedAlert, now: datetime) -> tuple[# (the per-alert fallback's `alert.save()` would honour `auto_now`, but the# happy path is bulk_update).alert.updated_at = now- alert.next_check_at = advance_next_check_at(alert.next_check_at, alert.check_interval_minutes, now)+ alert.next_check_at = advance_next_check_at(+ alert.next_check_at,+ alert.check_interval_minutes,+ now,+ shard_offset_seconds=compute_shard_offset_seconds(alert.id, alert.check_interval_minutes),+ )update_fields.extend(["last_checked_at", "next_check_at", "updated_at"])if (diff --git a/products/logs/backend/test/test_alert_utils.py b/products/logs/backend/test/test_alert_utils.pyindex 0ade8e1da55f..e2a30f3443dc 100644--- a/products/logs/backend/test/test_alert_utils.py+++ b/products/logs/backend/test/test_alert_utils.py@@ -1,4 +1,5 @@from datetime import UTC, datetime, timedelta+from uuid import UUIDimport pytestfrom unittest import TestCase@@ -10,7 +11,11 @@ from hypothesis import ()from parameterized import parameterized-from products.logs.backend.alert_utils import advance_next_check_at+from products.logs.backend.alert_utils import (+ SCHEDULE_INTERVAL_SECONDS,+ advance_next_check_at,+ compute_shard_offset_seconds,+)# Strategies for property tests below.# Cadences span the realistic range: 1 minute (tightest), through hourly (60),@@ -312,3 +317,147 @@ class TestAdvanceNextCheckAtProperties(TestCase):result = advance_next_check_at(scheduled, 1, now)assert result > nowassert result <= now + timedelta(minutes=1)+++class TestComputeShardOffsetSeconds(TestCase):+ """Per-alert shard offset computation."""++ def test_offset_is_deterministic_for_same_id_and_cadence(self) -> None:+ alert_id = UUID("019dec00-0000-0000-0000-000000000001")+ a = compute_shard_offset_seconds(alert_id, 5)+ b = compute_shard_offset_seconds(alert_id, 5)+ assert a == b++ def test_cadence_at_or_below_schedule_interval_returns_zero(self) -> None:+ # 1-min cadence with 60s schedule = 1 shard slot, no spread possible.+ alert_id = UUID("019dec00-0000-0000-0000-000000000001")+ assert compute_shard_offset_seconds(alert_id, 1) == 0++ @parameterized.expand([(c,) for c in (1, 5, 10, 15, 30, 60)])+ def test_offset_is_minute_aligned_and_within_cadence(self, cadence: int) -> None:+ # Offset must be a multiple of SCHEDULE_INTERVAL_SECONDS and < cadence.+ alert_id = UUID("019dec00-0000-0000-0000-000000000042")+ offset = compute_shard_offset_seconds(alert_id, cadence)+ assert offset % SCHEDULE_INTERVAL_SECONDS == 0+ assert 0 <= offset < cadence * 60++ def test_distribution_across_many_alerts_is_roughly_uniform(self) -> None:+ # 1000 random UUIDs at 5-min cadence (5 shards). Each shard should+ # see ~200 alerts. Allow ±20% drift before flagging non-uniformity.+ from uuid import uuid4++ counts: dict[int, int] = {0: 0, 60: 0, 120: 0, 180: 0, 240: 0}+ for _ in range(1000):+ offset = compute_shard_offset_seconds(uuid4(), 5)+ counts[offset] += 1++ for offset, count in counts.items():+ assert 160 <= count <= 240, f"shard {offset}: count {count} outside ±20% of mean 200"++ def test_different_cadences_produce_different_shard_counts(self) -> None:+ # Same alert_id, different cadences, should map to different shard slots+ # because the modulus changes. (Not strictly required, but a healthy+ # smoke test that the cadence input is actually used.)+ alert_id = UUID("019dec00-0000-0000-0000-00000000abcd")+ offsets = {compute_shard_offset_seconds(alert_id, c) for c in (5, 10, 15, 30, 60)}+ # At least two distinct offsets across these cadences (very loose bound).+ assert len(offsets) >= 2+++class TestAdvanceNextCheckAtWithShard(TestCase):+ """`shard_offset_seconds` shifts the canonical grid per-alert."""++ @parameterized.expand(+ [+ # (name, current, cadence, now, shard_offset, expected)+ (+ # 5-min alert with 2-min shard offset → :02/:07/:12/... grid.+ "shard_120_lands_on_shifted_grid",+ datetime(2026, 3, 19, 12, 7, tzinfo=UTC),+ 5,+ datetime(2026, 3, 19, 12, 7, 30, tzinfo=UTC),+ 120,+ datetime(2026, 3, 19, 12, 12, tzinfo=UTC),+ ),+ (+ # Pre-shard NCA on canonical grid (12:05) self-heals to shard+ # grid (12:12) on next eval. One transient longer gap.+ "drifted_to_canonical_self_heals_to_shard",+ datetime(2026, 3, 19, 12, 5, tzinfo=UTC),+ 5,+ datetime(2026, 3, 19, 12, 6, tzinfo=UTC),+ 120,+ datetime(2026, 3, 19, 12, 12, tzinfo=UTC),+ ),+ (+ # shard_offset=0 (default behaviour) preserves canonical grid.+ "no_shard_keeps_canonical_grid",+ datetime(2026, 3, 19, 12, 0, tzinfo=UTC),+ 5,+ datetime(2026, 3, 19, 12, 3, tzinfo=UTC),+ 0,+ datetime(2026, 3, 19, 12, 5, tzinfo=UTC),+ ),+ (+ # First-run with shard offset lands on shifted grid.+ "first_run_shard_240_lands_at_shifted_first_slot",+ None,+ 5,+ datetime(2026, 3, 19, 12, 0, tzinfo=UTC),+ 240,+ datetime(2026, 3, 19, 12, 9, tzinfo=UTC),+ ),+ ]+ )+ def test_shard_offset_shifts_grid(+ self,+ _name: str,+ current: datetime | None,+ cadence: int,+ now: datetime,+ shard_offset: int,+ expected: datetime,+ ) -> None:+ result = advance_next_check_at(current, cadence, now, shard_offset_seconds=shard_offset)+ assert result == expected, f"got {result}"++ @given(+ # Non-divisors of 60 (7, 11) included to surface bugs that only appear+ # when the cadence grid doesn't tile cleanly within the hour.+ cadence=st.sampled_from([2, 3, 5, 7, 10, 11, 15, 30, 60]),+ shard_index=st.integers(min_value=0, max_value=10),+ )+ @settings(max_examples=200, deadline=None)+ def test_steady_state_gap_equals_cadence_with_shard(self, cadence: int, shard_index: int) -> None:+ # Same property as the non-sharded test: inter-eval gaps stay exactly+ # `cadence` minutes once on grid, regardless of shard offset.+ shard_count = max(1, (cadence * 60) // SCHEDULE_INTERVAL_SECONDS)+ offset = (shard_index % shard_count) * SCHEDULE_INTERVAL_SECONDS++ anchor = datetime(2026, 3, 19, 12, 0, tzinfo=UTC)+ # Heal once to land on shard grid.+ current = advance_next_check_at(anchor, cadence, anchor + timedelta(seconds=1), shard_offset_seconds=offset)+ for _ in range(10):+ next_check = advance_next_check_at(current, cadence, current, shard_offset_seconds=offset)+ assert next_check - current == timedelta(minutes=cadence)+ current = next_check++ def test_cadence_change_self_heals_to_new_shard_grid(self) -> None:+ # User edits an alert from 5-min cadence (shard offset 120s) to 10-min+ # cadence (shard offset, say, 300s). The alert was last evaluated under+ # the old config; its current_next_check_at sits on the OLD grid. The+ # next eval under the new config must land on the NEW grid within one+ # cadence period, with the new shard offset applied.+ old_nca = datetime(2026, 3, 19, 12, 7, tzinfo=UTC) # was on 5-min grid + 120s offset+ new_cadence = 10+ new_shard_offset = 300 # different shard slot under new cadence+ now = datetime(2026, 3, 19, 12, 7, 30, tzinfo=UTC)++ result = advance_next_check_at(old_nca, new_cadence, now, shard_offset_seconds=new_shard_offset)++ # Expected: next 10-min boundary after old_nca is 12:10; floor lands at+ # 12:10, plus shard offset 300s (5min) = 12:15.+ assert result == datetime(2026, 3, 19, 12, 15, tzinfo=UTC)+ # Next gap is exactly the new cadence.+ next_check = advance_next_check_at(result, new_cadence, result, shard_offset_seconds=new_shard_offset)+ assert next_check - result == timedelta(minutes=new_cadence)diff --git a/products/logs/backend/test/test_alerts_api.py b/products/logs/backend/test/test_alerts_api.pyindex c7fc3c1ecff5..c6e821009b99 100644--- a/products/logs/backend/test/test_alerts_api.py+++ b/products/logs/backend/test/test_alerts_api.py@@ -1,5 +1,7 @@import jsonfrom datetime import UTC, datetime, timedelta+from typing import Any+from uuid import uuid4from freezegun import freeze_timefrom posthog.test.base import APIBaseTest, ClickhouseTestMixin@@ -13,6 +15,7 @@ from posthog.clickhouse.client import sync_executefrom posthog.models.team.team import Teamfrom products.logs.backend.alert_check_query import AlertCheckQuery, BucketedCount+from products.logs.backend.alert_utils import compute_shard_offset_secondsfrom products.logs.backend.alerts_api import ALLOWED_WINDOW_MINUTES, MAX_ALERTS_PER_TEAMfrom products.logs.backend.models import LogsAlertConfiguration, LogsAlertEvent@@ -1677,7 +1680,7 @@ class TestSimulateEvaluatorLifecycleParity(ClickhouseTestMixin, APIBaseTest):def _make_alert(self, **overrides: object) -> LogsAlertConfiguration:from products.logs.backend.alert_state_machine import AlertState- defaults = {+ defaults: dict[str, Any] = {"team": self.team,"name": "Lifecycle test alert","filters": {"serviceNames": [self.service]},@@ -1692,6 +1695,17 @@ class TestSimulateEvaluatorLifecycleParity(ClickhouseTestMixin, APIBaseTest):"state": AlertState.NOT_FIRING.value,}defaults.update(overrides)+ # Pin the test alert to shard 0 so its evaluator NCAs align with the+ # simulator's canonical-grid bucket boundaries. Without this, the+ # evaluator advances NCAs onto the alert's shard offset (e.g. :02/:07+ # instead of :00/:05) and the simulator's :00/:05 bucket evaluations+ # disagree on event timing.+ cadence = int(defaults["check_interval_minutes"])+ while True:+ candidate = uuid4()+ if compute_shard_offset_seconds(candidate, cadence) == 0:+ defaults["id"] = candidate+ breakreturn LogsAlertConfiguration.objects.create(**defaults)def _drive_evaluator(