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.

feat(usage-reports): add workflow start time to v2 pointer (#68116)

Marce Coll513a1620e1c4
diff --git a/posthog/temporal/tests/usage_report/conftest.py b/posthog/temporal/tests/usage_report/conftest.py
index d350e031abd7..2ad0e786fb72 100644
--- a/posthog/temporal/tests/usage_report/conftest.py
+++ b/posthog/temporal/tests/usage_report/conftest.py
@@ -45,6 +45,7 @@ def minio_workflow_ctx() -> Iterator[WorkflowContext]:
"""A `WorkflowContext` whose run-prefix gets nuked from MinIO at teardown."""
ctx = WorkflowContext(
run_id=f"test-{uuid.uuid4().hex[:12]}",
+ workflow_started_at=datetime(2026, 5, 5, 1, 45, 0, tzinfo=UTC),
period_start=datetime(2026, 5, 4, 0, 0, 0, tzinfo=UTC),
period_end=datetime(2026, 5, 4, 23, 59, 59, 999999, tzinfo=UTC),
date_str=f"test-{uuid.uuid4().hex[:8]}",
diff --git a/posthog/temporal/tests/usage_report/test_aggregator.py b/posthog/temporal/tests/usage_report/test_aggregator.py
index 5345224b4798..18ee97ab958a 100644
--- a/posthog/temporal/tests/usage_report/test_aggregator.py
+++ b/posthog/temporal/tests/usage_report/test_aggregator.py
@@ -30,6 +30,7 @@ from posthog.temporal.usage_report.types import Manifest, ReportCompleteness, Ru
def _ctx(run_id: str = "abc", report_completeness: ReportCompleteness = "partial") -> WorkflowContext:
return WorkflowContext(
run_id=run_id,
+ workflow_started_at=datetime(2026, 5, 5, 1, 45, 0, tzinfo=UTC),
period_start=datetime(2026, 5, 4, 0, 0, 0, tzinfo=UTC),
period_end=datetime(2026, 5, 4, 23, 59, 59, 999999, tzinfo=UTC),
date_str="2026-05-04",
diff --git a/posthog/temporal/tests/usage_report/test_parity.py b/posthog/temporal/tests/usage_report/test_parity.py
index ed857d7522df..2a66ebabc681 100644
--- a/posthog/temporal/tests/usage_report/test_parity.py
+++ b/posthog/temporal/tests/usage_report/test_parity.py
@@ -213,6 +213,7 @@ def test_end_to_end_parity_celery_task_vs_temporal_activity(
# ---- Temporal path ----
ctx = WorkflowContext(
run_id="e2e-parity-test",
+ workflow_started_at=period_start,
period_start=period_start,
period_end=period_end,
date_str=period_start.strftime("%Y-%m-%d"),
diff --git a/posthog/temporal/tests/usage_report/test_pointer_activity.py b/posthog/temporal/tests/usage_report/test_pointer_activity.py
index a59e070c2c58..cba400a91bc6 100644
--- a/posthog/temporal/tests/usage_report/test_pointer_activity.py
+++ b/posthog/temporal/tests/usage_report/test_pointer_activity.py
@@ -22,6 +22,7 @@ from posthog.temporal.usage_report.types import AggregateResult, EnqueuePointerI
def _ctx() -> WorkflowContext:
return WorkflowContext(
run_id="run-test",
+ workflow_started_at=datetime(2026, 5, 5, 1, 45, 0, tzinfo=UTC),
period_start=datetime(2026, 5, 4, 0, 0, 0, tzinfo=UTC),
period_end=datetime(2026, 5, 4, 23, 59, 59, 999999, tzinfo=UTC),
date_str="2026-05-04",
@@ -80,6 +81,7 @@ async def test_pointer_uses_v2_queue_and_correct_payload(activity_environment) -
assert body == {
"version": SQS_POINTER_VERSION,
"run_id": "run-test",
+ "workflow_started_at": "2026-05-05T01:45:00+00:00",
"date": "2026-05-04",
"period_start": "2026-05-04T00:00:00+00:00",
"period_end": "2026-05-04T23:59:59.999999+00:00",
@@ -102,6 +104,41 @@ async def test_pointer_uses_v2_queue_and_correct_payload(activity_environment) -
}
+async def test_pointer_omits_workflow_started_at_for_legacy_ctx(activity_environment) -> None:
+ """An activity input serialized before `workflow_started_at` existed (field
+ is None) must still send a valid pointer, just without the key. Guards the
+ backwards-compat rollout: an in-flight or retried old payload crossing a
+ deploy can't crash the enqueue, and billing skips the metric via `.get(...)`.
+ """
+ legacy_ctx = WorkflowContext(
+ run_id="run-legacy",
+ period_start=datetime(2026, 5, 4, 0, 0, 0, tzinfo=UTC),
+ period_end=datetime(2026, 5, 4, 23, 59, 59, 999999, tzinfo=UTC),
+ date_str="2026-05-04",
+ )
+ fake_producer = MagicMock()
+ fake_producer.send_message.return_value = {"MessageId": "abc"}
+
+ with (
+ patch("posthog.temporal.usage_report.activities.settings") as mock_settings,
+ patch("posthog.temporal.usage_report.activities.bucket", return_value="posthog"),
+ patch("ee.sqs.SQSProducer.get_sqs_producer", return_value=fake_producer),
+ patch("posthog.temporal.usage_report.activities.get_instance_region", return_value="US"),
+ ):
+ mock_settings.EE_AVAILABLE = True
+ mock_settings.SITE_URL = "https://us.posthog.com"
+
+ await activity_environment.run(
+ enqueue_pointer_message,
+ EnqueuePointerInputs(ctx=legacy_ctx, aggregate=_agg()),
+ )
+
+ fake_producer.send_message.assert_called_once()
+ body = json.loads(fake_producer.send_message.call_args.kwargs["message_body"])
+ assert "workflow_started_at" not in body
+
+
@pytest.mark.asyncio
async def test_pointer_skipped_when_ee_unavailable(activity_environment) -> None:
"""Self-hosted (no EE) → activity is a no-op, no SQS import or call."""
diff --git a/posthog/temporal/tests/usage_report/test_storage.py b/posthog/temporal/tests/usage_report/test_storage.py
index d50a706bc84e..5e76fc5446af 100644
--- a/posthog/temporal/tests/usage_report/test_storage.py
+++ b/posthog/temporal/tests/usage_report/test_storage.py
@@ -33,6 +33,7 @@ def _ctx(run_id: str = "abc-123", date_str: str = "2026-05-04") -> WorkflowConte
return WorkflowContext(
run_id=run_id,
+ workflow_started_at=datetime(2026, 5, 5, 1, 45, 0, tzinfo=UTC),
period_start=datetime(2026, 5, 4, 0, 0, 0, tzinfo=UTC),
period_end=datetime(2026, 5, 4, 23, 59, 59, 999999, tzinfo=UTC),
date_str=date_str,
diff --git a/posthog/temporal/usage_report/activities.py b/posthog/temporal/usage_report/activities.py
index 0cd17a7a7c6b..2b25b9dd4619 100644
--- a/posthog/temporal/usage_report/activities.py
+++ b/posthog/temporal/usage_report/activities.py
@@ -233,6 +233,10 @@ async def enqueue_pointer_message(inputs: EnqueuePointerInputs) -> None:
"total_orgs": inputs.aggregate.total_orgs,
"total_orgs_with_usage": inputs.aggregate.total_orgs_with_usage,
}
+ # Omit the key for legacy contexts (field absent pre-deploy); billing
+ # reads it with `.get(...)` and skips the flow-latency metric when None.
+ if inputs.ctx.workflow_started_at is not None:
+ pointer["workflow_started_at"] = inputs.ctx.workflow_started_at.isoformat()
@sync_to_async
def send() -> None:
diff --git a/posthog/temporal/usage_report/types.py b/posthog/temporal/usage_report/types.py
index af43ec75a5c9..fce4a1648180 100644
--- a/posthog/temporal/usage_report/types.py
+++ b/posthog/temporal/usage_report/types.py
@@ -32,6 +32,10 @@ class WorkflowContext(BaseModel):
"""Snapshot of the workflow run, threaded through every activity."""
run_id: str
+ # Optional so an activity input serialized before this field existed
+ # (in-flight or retried across a deploy) still decodes through the pydantic
+ # converter. New runs always populate it in build_context.
+ workflow_started_at: Optional[datetime] = None
period_start: datetime
period_end: datetime
date_str: str
diff --git a/posthog/temporal/usage_report/workflow.py b/posthog/temporal/usage_report/workflow.py
index ec7ce51881ea..a30747bf90ca 100644
--- a/posthog/temporal/usage_report/workflow.py
+++ b/posthog/temporal/usage_report/workflow.py
@@ -53,12 +53,16 @@ def build_context(inputs: RunUsageReportsInputs, run_id: str, now: datetime) ->
`now`. For `day_offset=0` (today) the tail of the window is in the
future and simply matches no events, so intraday and finalizer runs
share one code path.
+
+ `now` (the workflow start instant) also rides along on the context to
+ billing so it can measure end-to-end flow latency.
"""
report_day = (now.astimezone(UTC) - timedelta(days=inputs.day_offset)).date()
period_start = datetime.combine(report_day, time.min, tzinfo=UTC)
period_end = datetime.combine(report_day, time.max, tzinfo=UTC)
return WorkflowContext(
run_id=run_id,
+ workflow_started_at=now,
period_start=period_start,
period_end=period_end,
date_str=period_start.strftime("%Y-%m-%d"),
@@ -85,7 +89,7 @@ class RunUsageReportsWorkflow(PostHogWorkflow):
started_at = workflow.now()
status = "FAILED"
try:
- ctx = build_context(inputs, run_id=workflow.info().run_id, now=workflow.now())
+ ctx = build_context(inputs, run_id=workflow.info().run_id, now=started_at)
workflow.logger.info(
"Starting usage reports workflow",
extra={