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.

chore(data-warehouse): measure pipeline run attempts to expose retry amplification (#106825)

Tom Owers32473c599230
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/metrics.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/metrics.py
index 3a5c03b6165e..acc6bc4442ab 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/metrics.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/metrics.py
@@ -1,5 +1,5 @@
from temporalio import activity
-from temporalio.common import MetricCounter, MetricHistogramFloat
+from temporalio.common import MetricCounter, MetricHistogram, MetricHistogramFloat
def get_rows_extracted_metric(team_id: str, schema_id: str, source_type: str) -> MetricCounter:
@@ -42,3 +42,29 @@ def get_pipeline_run_duration_metric(
)
.create_histogram_float("warehouse_pipeline_run_duration_seconds", "Duration of full pipeline runs", "s")
)
+
+
+def get_run_attempt_metric(source_type: str) -> MetricHistogram:
+ """Temporal activity attempt number, recorded once as each attempt starts extracting.
+
+ Every attempt of a run stages a fresh set of batches under its own ``run_uuid`` (see
+ ``pipeline.py``), and the idempotency key is keyed on that ``run_uuid`` — so a retried run
+ re-extracts and re-loads work its earlier attempts already did. Batches drain serially per
+ ``(team_id, schema_id)``, so a run that keeps retrying enqueues faster than its group can
+ drain and holds the queue head for hours.
+
+ Nothing else measures that: ``warehouse_pg_consumer_batch_retry_total`` counts *batch*
+ retries, not workflow attempts. Read this as a distribution — a p99 above a handful means
+ runs are being restarted repeatedly and paying for the same extraction each time.
+
+ Labelled by ``source_type`` only. Attempts are a per-run property, so team/schema labels would
+ add cardinality without telling you anything the distribution does not.
+ """
+ return (
+ activity.metric_meter()
+ .with_additional_attributes({"source_type": source_type})
+ .create_histogram(
+ "warehouse_pipeline_run_attempt",
+ "Temporal activity attempt number at the start of a pipeline run",
+ )
+ )
diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py
index 754e02be223b..520add8451f5 100644
--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py
+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/pipeline_v3/pipeline.py
@@ -68,6 +68,7 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline
get_batches_produced_metric,
get_pipeline_run_duration_metric,
get_rows_extracted_metric,
+ get_run_attempt_metric,
)
from products.warehouse_sources.backend.temporal.data_imports.pipelines.pipeline_v3.postgres_queue.producer import (
PostgresProducer,
@@ -343,6 +344,12 @@ class PipelineV3(Generic[ResumableData]):
source_type = self._source.source_type if self._source else "unknown"
sync_type = self._pg_producer.sync_type
+ # Recorded where extraction begins, so one observation is one attempt that actually did
+ # work. A rising distribution means runs are restarting and re-extracting what earlier
+ # attempts already staged.
+ if activity.in_activity():
+ get_run_attempt_metric(source_type).record(self._attempt)
+
start_time = time.perf_counter()
status = "success"