: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(session-replay): emit surfacing sweep backlog gauge (#64475)
Tue Haulundadf4525190bb
diff --git a/posthog/temporal/session_replay/surfacing_scoring_sweep/README.md b/posthog/temporal/session_replay/surfacing_scoring_sweep/README.mdindex e78f3864c483..9a06bbddc3b3 100644--- a/posthog/temporal/session_replay/surfacing_scoring_sweep/README.md+++ b/posthog/temporal/session_replay/surfacing_scoring_sweep/README.md@@ -283,6 +283,7 @@ Temporal SDK metrics (Prometheus on the worker metrics port):| `surfacing_scoring_total_fetched` | counter | end of each tick (`total_fetched > 0`) || `surfacing_scoring_total_scored` | counter | end of each tick (`total_scored > 0`) || `surfacing_scoring_chunks_failed` | counter | end of each tick (`chunks_failed > 0`) |+| `surfacing_scoring_estimated_backlog` | gauge | start of each tick (`list_chunks`) || `surfacing_scoring_score_chunk_activity_execution_latency` | histogram | each `score_chunk_activity` run |`total_fetched` is rows the feature SELECT returned from ClickHouse;diff --git a/posthog/temporal/session_replay/surfacing_scoring_sweep/activities.py b/posthog/temporal/session_replay/surfacing_scoring_sweep/activities.pyindex 643d32173a86..c6f96c778df5 100644--- a/posthog/temporal/session_replay/surfacing_scoring_sweep/activities.py+++ b/posthog/temporal/session_replay/surfacing_scoring_sweep/activities.py@@ -59,6 +59,7 @@ from posthog.temporal.session_replay.surfacing_scoring_sweep.features import (out_of_contract_row_mask,validate_features,)+from posthog.temporal.session_replay.surfacing_scoring_sweep.metrics import record_backlog_estimatefrom posthog.temporal.session_replay.surfacing_scoring_sweep.scorer import get_feature_names, predictfrom posthog.temporal.session_replay.surfacing_scoring_sweep.types import (ChunkResult,@@ -98,6 +99,7 @@ async def list_chunks_activity(_inputs: ScoreSessionsBatchInputs) -> ListChunksRsampled = await sync_to_async(_count_unscored_in_one_bucket, thread_sensitive=False)(lookback_days, of_chunks)estimated_total = sampled * of_chunks+ record_backlog_estimate(estimated_total)chunks = [ChunkSpec(diff --git a/posthog/temporal/session_replay/surfacing_scoring_sweep/metrics.py b/posthog/temporal/session_replay/surfacing_scoring_sweep/metrics.pyindex 9237b57696eb..9c720c4a2e07 100644--- a/posthog/temporal/session_replay/surfacing_scoring_sweep/metrics.py+++ b/posthog/temporal/session_replay/surfacing_scoring_sweep/metrics.py@@ -31,6 +31,16 @@ TOTAL_SCORED_DESCRIPTION = "Sessions scored in a surfacing scoring sweep tick"CHUNKS_FAILED_COUNTER = "surfacing_scoring_chunks_failed"CHUNKS_FAILED_DESCRIPTION = "Hash-partitioned chunks that failed in a surfacing scoring sweep tick"+ESTIMATED_BACKLOG_GAUGE = "surfacing_scoring_estimated_backlog"+ESTIMATED_BACKLOG_DESCRIPTION = (+ "Estimated eligible-but-unscored session backlog (one hash bucket sampled, extrapolated)"+)+++def record_backlog_estimate(estimated: int) -> None:+ # Gauge, not counter: it's a standing quantity, and 0 (caught up) is meaningful — emit unconditionally.+ get_metric_meter().create_gauge(ESTIMATED_BACKLOG_GAUGE, ESTIMATED_BACKLOG_DESCRIPTION).set(max(0, estimated))+def record_tick_summary(*, total_scored: int, total_fetched: int, chunks_failed: int) -> None:if total_scored <= 0 and total_fetched <= 0 and chunks_failed <= 0:diff --git a/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/test_activities.py b/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/test_activities.pyindex 2825c82af498..92d8300ac890 100644--- a/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/test_activities.py+++ b/posthog/temporal/tests/session_replay/surfacing_scoring_sweep/test_activities.py@@ -52,6 +52,16 @@ class TestListChunksActivity:assert result.estimated_unscored_sessions == 42 * DEFAULT_OF_CHUNKS+ @pytest.mark.asyncio+ async def test_emits_backlog_gauge_with_extrapolated_estimate(self) -> None:+ with (+ mock.patch(f"{ACTIVITIES_MODULE}._count_unscored_in_one_bucket", return_value=42),+ mock.patch(f"{ACTIVITIES_MODULE}.record_backlog_estimate") as record_backlog_mock,+ ):+ await ActivityEnvironment().run(list_chunks_activity, ScoreSessionsBatchInputs())++ record_backlog_mock.assert_called_once_with(42 * DEFAULT_OF_CHUNKS)+class TestBuildPartialRow:def test_naive_datetime_is_treated_as_utc(self) -> None: