: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(data-warehouse): add metrics to the warehouse CDP producer (#85680)
Tom Owers41da6de68880
diff --git a/products/warehouse_sources/backend/temporal/data_imports/cdp_producer_job.py b/products/warehouse_sources/backend/temporal/data_imports/cdp_producer_job.pyindex a74622741ee8..057cae226cb9 100644--- a/products/warehouse_sources/backend/temporal/data_imports/cdp_producer_job.py+++ b/products/warehouse_sources/backend/temporal/data_imports/cdp_producer_job.py@@ -1,6 +1,7 @@import jsonfrom datetime import timedelta+from prometheus_client import Counterfrom structlog.contextvars import bind_contextvarsfrom temporalio import activity, workflowfrom temporalio.common import RetryPolicy@@ -13,15 +14,26 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.core.cdpLOGGER = get_logger(__name__)+CDP_PRODUCER_RUNS_TOTAL = Counter(+ "warehouse_cdp_producer_runs_total",+ "CDP producer activity attempts by outcome",+ labelnames=["team_id", "outcome"],+)+@activity.defnasync def produce_to_cdp_kafka_activity(inputs: CDPProducerWorkflowInputs) -> None:bind_contextvars(team_id=inputs.team_id)logger = LOGGER.bind()- await CDPProducer(- team_id=inputs.team_id, schema_id=inputs.schema_id, job_id=inputs.job_id, logger=logger- ).produce_to_kafka_from_s3()+ try:+ await CDPProducer(+ team_id=inputs.team_id, schema_id=inputs.schema_id, job_id=inputs.job_id, logger=logger+ ).produce_to_kafka_from_s3()+ except Exception:+ CDP_PRODUCER_RUNS_TOTAL.labels(team_id=str(inputs.team_id), outcome="failed").inc()+ raise+ CDP_PRODUCER_RUNS_TOTAL.labels(team_id=str(inputs.team_id), outcome="completed").inc()@workflow.defn(name="dwh-cdp-producer-job")diff --git a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/cdp_producer.py b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/cdp_producer.pyindex 8914de0685e0..59610e7f69a4 100644--- a/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/cdp_producer.py+++ b/products/warehouse_sources/backend/temporal/data_imports/pipelines/core/cdp_producer.py@@ -10,6 +10,7 @@ import orjsonimport pyarrow as paimport pyarrow.fs as pa_fsimport pyarrow.parquet as pq+from prometheus_client import Counterfrom pyarrow.parquet import write_tablefrom structlog.types import FilteringBoundLogger@@ -27,6 +28,20 @@ from products.warehouse_sources.backend.temporal.data_imports.pipelines.helpersfrom products.warehouse_sources.backend.temporal.data_imports.util import PostHogInternalDatabaseErrorfrom products.workflows.backend.models.hog_flow.hog_flow import HogFlow+# Per-file exceptions are swallowed (the file is deleted and the run continues), so a failed file+# is silently dropped rows. The outcome label is what makes that visible to alerting.+CDP_PRODUCER_FILES_TOTAL = Counter(+ "warehouse_cdp_producer_files_total",+ "Staged CDP files read from S3 and produced to Kafka, by outcome",+ labelnames=["team_id", "outcome"],+)++CDP_PRODUCER_ROWS_TOTAL = Counter(+ "warehouse_cdp_producer_rows_total",+ "Warehouse rows produced to the CDP raw-table Kafka topic",+ labelnames=["team_id"],+)+class CDPProducer:team_id: int@@ -235,12 +250,16 @@ class CDPProducer:row_index += 1await kafka_producer.flush()+ CDP_PRODUCER_FILES_TOTAL.labels(team_id=str(self.team_id), outcome="produced").inc()await self.logger.adebug(f"Finished producing file {file_path} to Kafka")except Exception as e:+ CDP_PRODUCER_FILES_TOTAL.labels(team_id=str(self.team_id), outcome="failed").inc()capture_exception(e)await self.logger.adebug(f"Error producing file {file_path} to Kafka: {e}")finally:# TODO(Gilbert09): have better row tracking so we can retry from a particular row+ if row_index:+ CDP_PRODUCER_ROWS_TOTAL.labels(team_id=str(self.team_id)).inc(row_index)await self.logger.adebug(f"Produced {row_index} rows")await self.logger.adebug(f"Deleting file {file_path}")await asyncio.to_thread(fs.delete_file, file_path)