: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
fix(flags): measure backfill consumer lag from the Kafka message time (#111071)
Phil Haack42240903d0cd
diff --git a/posthog/dags/flag_evaluations_backfill.py b/posthog/dags/flag_evaluations_backfill.pyindex 9424bba65851..722cf5bdbb94 100644--- a/posthog/dags/flag_evaluations_backfill.py+++ b/posthog/dags/flag_evaluations_backfill.py@@ -59,11 +59,14 @@ _COPIED_COLUMNS = "uuid, event, properties, timestamp, team_id, distinct_id, cre# consumer's position. The lookback spans several days so that a partition that stops delivering# stays in the query and reports its real lag. A partition that has delivered nothing for the whole# lookback drops out of this query.+# Lag comes from _timestamp, the Kafka message time. A consumer that works through a backlog writes+# rows now, so the time a row is written does not show how far the consumer is behind.+# The lookback filters on inserted_at because only inserted_at has a skip index._KAFKA_LOOKBACK_DAYS = 7_KAFKA_POSITION_QUERY = f"""SELECT count(), max(lag_seconds)FROM (- SELECT _partition, dateDiff('second', max(inserted_at), now64(6)) AS lag_seconds+ SELECT _partition, dateDiff('second', max(_timestamp), now()) AS lag_secondsFROM {FLAG_EVALUATIONS_DATA_TABLE}WHERE inserted_at >= now() - INTERVAL {_KAFKA_LOOKBACK_DAYS} DAY AND inserted_at != timestampGROUP BY _partitiondiff --git a/posthog/dags/tests/test_flag_evaluations_backfill.py b/posthog/dags/tests/test_flag_evaluations_backfill.pyindex 511c5bea12af..269e3e70eb2f 100644--- a/posthog/dags/tests/test_flag_evaluations_backfill.py+++ b/posthog/dags/tests/test_flag_evaluations_backfill.py@@ -182,25 +182,30 @@ def seed_flag_evaluation(cluster: ClickhouseCluster, now: datetime, event: Sourcdef seed_kafka_path_row(- cluster: ClickhouseCluster, now: datetime, age: timedelta = timedelta(0), partition: int = 0+ cluster: ClickhouseCluster,+ now: datetime,+ age: timedelta = timedelta(0),+ partition: int = 0,+ consumer_delay: timedelta = timedelta(0),) -> None:# The lag check skips rows whose inserted_at equals their timestamp, because the backfill copies# rows that way. A Kafka row arrives after its event, so its inserted_at is later.- inserted_at = now - age+ kafka_time = now - agerow = (KAFKA_PATH_ROW.team_id,KAFKA_PATH_ROW.distinct_id,uuid5(NAMESPACE_URL, KAFKA_PATH_ROW.distinct_id),KAFKA_PATH_ROW.uuid,- inserted_at - timedelta(seconds=1),- inserted_at,+ kafka_time - timedelta(seconds=1),+ kafka_time + consumer_delay,+ kafka_time,partition,)def insert(client: Client) -> None:client.execute("""INSERT INTO writable_flag_evaluations- (team_id, distinct_id, person_id, uuid, timestamp, inserted_at, _partition)+ (team_id, distinct_id, person_id, uuid, timestamp, inserted_at, _timestamp, _partition)VALUES""",[row],)@@ -553,23 +558,37 @@ def test_backfill_stops_at_the_first_expired_day(start: datetime, step: str, ste@pytest.mark.django_db@pytest.mark.parametrize(- "overrides, kafka_path_row_ages",+ "overrides, kafka_path_row_ages, consumer_delay",[- pytest.param({"min_free_bytes": 1 << 60}, [timedelta(0)], id="free_space_below_the_floor"),+ pytest.param({"min_free_bytes": 1 << 60}, [timedelta(0)], timedelta(0), id="free_space_below_the_floor"),+ pytest.param(+ {"max_consumer_lag_seconds": 3600},+ [timedelta(hours=2)],+ timedelta(0),+ id="kafka_path_behind_by_more_than_the_limit",+ ),pytest.param(- {"max_consumer_lag_seconds": 3600}, [timedelta(hours=2)], id="kafka_path_behind_by_more_than_the_limit"+ {"max_consumer_lag_seconds": 3600},+ [timedelta(hours=2)],+ timedelta(hours=2),+ id="kafka_path_writing_a_backlog_older_than_the_limit",),- pytest.param({}, [timedelta(0), timedelta(days=2)], id="one_kafka_partition_silent_for_over_a_day"),- pytest.param({}, [timedelta(days=8)], id="kafka_path_silent_for_the_whole_lookback"),+ pytest.param(+ {}, [timedelta(0), timedelta(days=2)], timedelta(0), id="one_kafka_partition_silent_for_over_a_day"+ ),+ pytest.param({}, [timedelta(days=8)], timedelta(0), id="kafka_path_silent_for_the_whole_lookback"),],)def test_backfill_fails_without_copying_when_a_safety_check_fails(- cluster: ClickhouseCluster, overrides: dict[str, Any], kafka_path_row_ages: list[timedelta]+ cluster: ClickhouseCluster,+ overrides: dict[str, Any],+ kafka_path_row_ages: list[timedelta],+ consumer_delay: timedelta,) -> None:now = datetime.now(UTC)seed_source_events(cluster, now, [INSIDE_RECENT])for partition, age in enumerate(kafka_path_row_ages):- seed_kafka_path_row(cluster, now, age=age, partition=partition)+ seed_kafka_path_row(cluster, now, age=age, partition=partition, consumer_delay=consumer_delay)result = run_backfill(cluster, **overrides)