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(experiments): add recalc queue-wait metric and adjust concurrency (#69836)

Rodrigo Ilorod43e1fe38595
diff --git a/posthog/temporal/common/worker.py b/posthog/temporal/common/worker.py
index 52afa3e53e20..dd98b5769312 100644
--- a/posthog/temporal/common/worker.py
+++ b/posthog/temporal/common/worker.py
@@ -63,6 +63,8 @@ from products.experiments.backend.temporal.recalculation_metrics import (
EXPERIMENT_METRICS_RECALCULATION_ATTEMPT_HISTOGRAM_METRICS,
EXPERIMENT_METRICS_RECALCULATION_LATENCY_HISTOGRAM_BUCKETS,
EXPERIMENT_METRICS_RECALCULATION_LATENCY_HISTOGRAM_METRICS,
+ EXPERIMENT_METRICS_RECALCULATION_SCHEDULE_TO_START_HISTOGRAM_BUCKETS,
+ EXPERIMENT_METRICS_RECALCULATION_SCHEDULE_TO_START_HISTOGRAM_METRICS,
ExperimentsRecalculationMetricsInterceptor,
)
from products.logs.backend.facade.temporal import (
@@ -321,6 +323,12 @@ async def create_worker(
itertools.repeat(EXPERIMENT_METRICS_RECALCULATION_ATTEMPT_HISTOGRAM_BUCKETS),
)
)
+ | dict(
+ zip(
+ EXPERIMENT_METRICS_RECALCULATION_SCHEDULE_TO_START_HISTOGRAM_METRICS,
+ itertools.repeat(EXPERIMENT_METRICS_RECALCULATION_SCHEDULE_TO_START_HISTOGRAM_BUCKETS),
+ )
+ )
| {"batch_exports_activity_attempt": [1.0, 5.0, 10.0, 100.0]}
)
if task_queue == settings.DATA_MODELING_TASK_QUEUE:
diff --git a/products/experiments/backend/temporal/recalculation_metrics.py b/products/experiments/backend/temporal/recalculation_metrics.py
index 55094759f710..b5d1534ba68c 100644
--- a/products/experiments/backend/temporal/recalculation_metrics.py
+++ b/products/experiments/backend/temporal/recalculation_metrics.py
@@ -48,6 +48,25 @@ EXPERIMENT_METRICS_RECALCULATION_LATENCY_HISTOGRAM_BUCKETS = [
300_000.0, # 5m
]
+# Schedule-to-start latency is the queue-pressure signal: how long an activity sat in the task queue before
+# a worker picked it up. Buckets reach 30m (vs 5m for execution latency) because queue wait under backlog is
+# unbounded by the activity timeout; that is exactly the regime this histogram exists to observe.
+EXPERIMENT_METRICS_RECALCULATION_SCHEDULE_TO_START_HISTOGRAM_METRICS = (
+ "experiment_metrics_recalculation_activity_schedule_to_start_latency",
+)
+EXPERIMENT_METRICS_RECALCULATION_SCHEDULE_TO_START_HISTOGRAM_BUCKETS = [
+ 100.0, # 100ms
+ 500.0, # 500ms
+ 1_000.0, # 1s
+ 5_000.0, # 5s
+ 10_000.0, # 10s
+ 30_000.0, # 30s
+ 60_000.0, # 1m
+ 300_000.0, # 5m
+ 600_000.0, # 10m
+ 1_800_000.0, # 30m
+]
+
# Bucket boundaries for `_activity_success_attempts` (counts the attempt number at which success was reached).
# With `RetryPolicy(maximum_attempts=2)` today, only buckets [1, 2] are populated, but the wider range survives
# a future policy change without needing a new histogram.
@@ -85,6 +104,13 @@ class _ActivityInboundInterceptor(ActivityInboundInterceptor):
meter = activity.metric_meter().with_additional_attributes(
{"activity_type": activity_type, "workflow_type": _RECALCULATION_WORKFLOW_TYPE}
)
+ # Queue-pressure signal (see bucket comment above). Pattern mirrors mcp_analytics' intent_clustering.
+ if info.scheduled_time and info.started_time:
+ meter.create_histogram_timedelta(
+ name="experiment_metrics_recalculation_activity_schedule_to_start_latency",
+ description="Time between activity scheduling and start (task queue wait).",
+ unit="ms",
+ ).record(info.started_time - info.scheduled_time)
# Fires on every attempt (first run + each retry). Comparing this to `_activity_successes` reveals
# retry rate; without it a transient ClickHouse blip is indistinguishable from a healthy first-try
# success. Pattern mirrors batch_exports' `batch_exports_activity_attempts`.
diff --git a/products/experiments/backend/temporal/recalculation_workflow.py b/products/experiments/backend/temporal/recalculation_workflow.py
index f617feb97d15..d7e4faa7cdab 100644
--- a/products/experiments/backend/temporal/recalculation_workflow.py
+++ b/products/experiments/backend/temporal/recalculation_workflow.py
@@ -24,10 +24,11 @@ with temporalio.workflow.unsafe.imports_passed_through():
)
from products.experiments.backend.temporal.recalculation_metrics import increment_workflow_finished
-# Per-run metric fan-out: how many metric activities one run keeps in flight, sized so a typical experiment
-# recalculates in a single concurrent wave. Cross-run ClickHouse load is bounded separately by the dedicated
-# recalc worker's activity-slot cap (MAX_CONCURRENT_ACTIVITIES), not by this constant.
-MAX_CONCURRENT_METRICS = 14
+# Per-run metric fan-out: how many metric activities one run keeps in flight. Sized so two concurrent runs
+# from the same org fit exactly inside the per-org ClickHouse app-query limit (DEFAULT_APP_ORG_CONCURRENT_QUERIES
+# = 20 in posthog/clickhouse/client/limit.py); a third same-org run bounces off that limiter. Cross-run worker
+# load is bounded separately by the dedicated recalc worker's activity-slot cap (MAX_CONCURRENT_ACTIVITIES).
+MAX_CONCURRENT_METRICS = 10
@temporalio.workflow.defn(name="experiment-metrics-recalculation-workflow")