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(error-tracking): stop weekly digest failing under clickhouse load (#91626)

Aleksander Błaszkiewicz453242a7bf66
diff --git a/products/error_tracking/backend/temporal/weekly_digest/activities.py b/products/error_tracking/backend/temporal/weekly_digest/activities.py
index 40a6eea25454..d5a166a1f26e 100644
--- a/products/error_tracking/backend/temporal/weekly_digest/activities.py
+++ b/products/error_tracking/backend/temporal/weekly_digest/activities.py
@@ -352,5 +352,15 @@ def _send_org_digest(inputs: SendOrgDigestInputs, attempt: int) -> SendOrgDigest
@activity.defn
def send_org_digest_activity(inputs: SendOrgDigestInputs) -> SendOrgDigestResult:
close_old_connections()
+ attempt = activity.info().attempt
with HeartbeaterSync(logger=logger):
- return _send_org_digest(inputs, attempt=activity.info().attempt)
+ try:
+ return _send_org_digest(inputs, attempt=attempt)
+ except Exception:
+ # Every other failure log sits inside the per-team build loop, so a failure before it
+ # (the org-wide exception count query, or any Postgres read) names no org anywhere.
+ # Temporal's own activity-failure log carries the activity id but not the inputs, which
+ # leaves those orgs unidentifiable after the fact. This one event names the org for
+ # every failure path: filter it on attempt == max_attempts for the orgs a run gave up on.
+ logger.exception("et_weekly_digest.org_failed", org_id=inputs.org_id, attempt=attempt)
+ raise
diff --git a/products/error_tracking/backend/temporal/weekly_digest/test/test_weekly_digest_temporal.py b/products/error_tracking/backend/temporal/weekly_digest/test/test_weekly_digest_temporal.py
index d80959e872f9..983f8002e5d6 100644
--- a/products/error_tracking/backend/temporal/weekly_digest/test/test_weekly_digest_temporal.py
+++ b/products/error_tracking/backend/temporal/weekly_digest/test/test_weekly_digest_temporal.py
@@ -137,6 +137,11 @@ class TestLoadPageOrgs(SimpleTestCase):
# send_digest_to_workflow refuses to send outside Cloud, so the send path needs cloud mode.
+# The fallback and final-attempt branches are defined relative to max_attempts, so the cases
+# below have to move with it rather than hardcode the attempt numbers of one particular budget.
+_MAX_ATTEMPTS = SendOrgDigestInputs(org_id="").max_attempts
+
+
@override_settings(CLOUD_DEPLOYMENT="US")
class TestSendOrgDigest(ClickhouseTestMixin, APIBaseTest):
@classmethod
@@ -404,7 +409,7 @@ class TestSendOrgDigest(ClickhouseTestMixin, APIBaseTest):
# Final attempt (attempt == max_attempts): fall back to delivering the healthy teams rather
# than starving the recipient of a digest entirely. The activity still raises for visibility.
with pytest.raises(ApplicationError, match="team builds") as exc_info:
- self._run(attempt=6)
+ self._run(attempt=_MAX_ATTEMPTS)
assert mock_post.call_count == 1
sections = mock_post.call_args.kwargs["json"]["digest"]["project_sections"]
@@ -414,9 +419,9 @@ class TestSendOrgDigest(ClickhouseTestMixin, APIBaseTest):
@parameterized.expand(
[
- ("before_last_two_attempts_no_fallback", 4, 0),
- ("second_to_last_attempt_falls_back", 5, 1),
- ("final_attempt_falls_back", 6, 1),
+ ("before_last_two_attempts_no_fallback", _MAX_ATTEMPTS - 2, 0),
+ ("second_to_last_attempt_falls_back", _MAX_ATTEMPTS - 1, 1),
+ ("final_attempt_falls_back", _MAX_ATTEMPTS, 1),
]
)
def test_broken_test_account_filter_falls_back_to_unfiltered_on_last_two_attempts(
diff --git a/products/error_tracking/backend/temporal/weekly_digest/types.py b/products/error_tracking/backend/temporal/weekly_digest/types.py
index f8bdbe1e5ab1..7a0dea7790b7 100644
--- a/products/error_tracking/backend/temporal/weekly_digest/types.py
+++ b/products/error_tracking/backend/temporal/weekly_digest/types.py
@@ -10,12 +10,15 @@ class WeeklyDigestInputs:
# targeted manual runs.
dry_run: bool = True
org_ids: list[str] | None = None
- # How many per-org activities run at once within each page (bounds ClickHouse load
- # and webhook rate per page).
- max_concurrent: int = 10
- # Total executions per org activity: initial run + 5 retries. The final attempt sends
+ # How many per-org activities run at once within each page. Every page runs at once, so
+ # this is the only cap on the run's ClickHouse demand: peak concurrency is this value
+ # times ceil(total_orgs / page_size). The offline cluster has a global concurrent-query
+ # limit shared with every other product, so the value has to leave room for them.
+ max_concurrent: int = 5
+ # Total executions per org activity: initial run + 7 retries. The final attempt sends
# partial digests instead of deferring recipients whose teams failed to build.
- max_attempts: int = 6
+ # See SEND_ORG_* in workflow.py for the retry pacing this count is chosen against.
+ max_attempts: int = 8
# Orgs handled per page child workflow. Bounds each page's history and the size of
# the org-id slice its load activity returns (~40KB per 1000 orgs).
page_size: int = 1000
@@ -55,8 +58,8 @@ class WeeklyDigestPageInputs:
page_number: int
page_size: int
dry_run: bool = True
- max_concurrent: int = 10
- max_attempts: int = 6
+ max_concurrent: int = 5
+ max_attempts: int = 8
@dataclasses.dataclass(frozen=True)
@@ -64,9 +67,9 @@ class SendOrgDigestInputs:
org_id: str
# Fail-safe default, matching WeeklyDigestInputs: the workflow always passes it explicitly.
dry_run: bool = True
- # Must equal the activity's RetryPolicy.maximum_attempts — an activity cannot read its own
- # retry policy, and final-attempt detection (attempt >= max_attempts) depends on it.
- max_attempts: int = 6
+ # Must equal the activity's RetryPolicy.maximum_attempts, because an activity cannot read
+ # its own retry policy and final-attempt detection (attempt >= max_attempts) depends on it.
+ max_attempts: int = 8
@dataclasses.dataclass(frozen=True)
diff --git a/products/error_tracking/backend/temporal/weekly_digest/workflow.py b/products/error_tracking/backend/temporal/weekly_digest/workflow.py
index 34142b9c17a0..f7a0c6500ed7 100644
--- a/products/error_tracking/backend/temporal/weekly_digest/workflow.py
+++ b/products/error_tracking/backend/temporal/weekly_digest/workflow.py
@@ -43,8 +43,13 @@ CLEANUP_TIMEOUT = timedelta(minutes=1)
SEND_ORG_START_TO_CLOSE_TIMEOUT = timedelta(minutes=30)
SEND_ORG_HEARTBEAT_TIMEOUT = timedelta(minutes=5)
-SEND_ORG_INITIAL_RETRY_INTERVAL = timedelta(seconds=1)
-SEND_ORG_MAXIMUM_RETRY_INTERVAL = timedelta(seconds=30)
+# The retry budget must outlast a ClickHouse capacity incident, because every attempt made during
+# one fails the same way and a budget that fits inside the incident guarantees the org fails.
+# These values give 5s, 20s, 80s, 320s, then 600s per retry, so the default 8 attempts spread
+# across about 37 minutes.
+SEND_ORG_INITIAL_RETRY_INTERVAL = timedelta(seconds=5)
+SEND_ORG_BACKOFF_COEFFICIENT = 4.0
+SEND_ORG_MAXIMUM_RETRY_INTERVAL = timedelta(minutes=10)
def _sent_from_error(error: BaseException) -> int:
@@ -81,11 +86,11 @@ async def _send_orgs(org_ids: list[str], dry_run: bool, max_concurrent: int, max
start_to_close_timeout=SEND_ORG_START_TO_CLOSE_TIMEOUT,
heartbeat_timeout=SEND_ORG_HEARTBEAT_TIMEOUT,
retry_policy=common.RetryPolicy(
- # Must match SendOrgDigestInputs.max_attempts — final-attempt detection
+ # Must match SendOrgDigestInputs.max_attempts, because final-attempt detection
# inside the activity depends on the two agreeing.
maximum_attempts=max_attempts,
initial_interval=SEND_ORG_INITIAL_RETRY_INTERVAL,
- backoff_coefficient=2.0,
+ backoff_coefficient=SEND_ORG_BACKOFF_COEFFICIENT,
maximum_interval=SEND_ORG_MAXIMUM_RETRY_INTERVAL,
),
)
@@ -139,8 +144,9 @@ class ErrorTrackingWeeklyDigestWorkflow(PostHogWorkflow):
a child workflow immediately.
Only a storage key and a count ride through workflow history, so history and payload
- sizes stay flat regardless of org count. All children start at once, so the worker
- fleet's activity-slot capacity is the intended global throttle.
+ sizes stay flat regardless of org count. All children start at once, so the run's peak
+ ClickHouse demand is ``max_concurrent`` per page times the page count, and it grows with
+ the org count. The worker fleet's activity-slot capacity is the outer bound.
"""
inputs_cls = WeeklyDigestInputs