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.

refactor(experiments): add role to recalculation temporal payloads (#118595)

Anderscdd5f1216ad7
diff --git a/products/experiments/backend/temporal/models.py b/products/experiments/backend/temporal/models.py
index d8af658559e4..1f1b860362f2 100644
--- a/products/experiments/backend/temporal/models.py
+++ b/products/experiments/backend/temporal/models.py
@@ -99,6 +99,13 @@ class ExperimentMetricsRecalculationWorkflowInputs:
fairness_key: str | None = None
+def _role_from_either_field(role: str, metric_type: str) -> str:
+ resolved = role or metric_type
+ if not resolved:
+ raise ValueError("role is required")
+ return resolved
+
+
@dataclasses.dataclass(frozen=False)
class ExperimentMetricToRecalculate:
"""A single metric to recalculate.
@@ -109,9 +116,15 @@ class ExperimentMetricToRecalculate:
experiment_id: int
metric_uuid: str
- # The metric's role ("primary" or "secondary"), not its kind. Recorded workflow histories carry this
- # field name, so renaming it breaks their replay.
- metric_type: str
+ # "primary" or "secondary": the metric's role in the experiment, not its kind.
+ role: str = ""
+ # The old name of `role`. Workers and runs that started before `role` existed write and read only this
+ # field, so __post_init__ keeps the two fields equal. Remove this field, and the default of `role`, when no
+ # such worker or run is left.
+ metric_type: str = ""
+
+ def __post_init__(self) -> None:
+ self.role = self.metric_type = _role_from_either_field(self.role, self.metric_type)
@dataclasses.dataclass
@@ -136,7 +149,14 @@ class MetricOutOfTimeFailure:
recalculation_id: str
experiment_id: int
metric_uuid: str
- metric_type: str # "primary" or "secondary"
+ role: str = "" # "primary" or "secondary"
+ # The old name of `role`, kept for the same reason as ExperimentMetricToRecalculate.metric_type.
+ metric_type: str = ""
+
+ def __post_init__(self) -> None:
+ role = _role_from_either_field(self.role, self.metric_type)
+ object.__setattr__(self, "role", role)
+ object.__setattr__(self, "metric_type", role)
@dataclasses.dataclass
diff --git a/products/experiments/backend/temporal/recalculation_activities.py b/products/experiments/backend/temporal/recalculation_activities.py
index d2438252d6ce..372f759ba3b4 100644
--- a/products/experiments/backend/temporal/recalculation_activities.py
+++ b/products/experiments/backend/temporal/recalculation_activities.py
@@ -110,7 +110,7 @@ async def calculate_experiment_metric_for_recalculation(
"""Calculate one metric, write its recalc-fingerprinted result to ExperimentMetricResult, and fold the
progress update (counter + error) into the same job atomically. query_to is the run's shared data-window end.
- role ("primary" or "secondary") is carried from discovery in ExperimentMetricToRecalculate.metric_type; it's
+ role ("primary" or "secondary") is carried from discovery in ExperimentMetricToRecalculate.role; it's
threaded into the per-metric PostHog event so the capture path doesn't have to re-query the saved-metric M2M
to resolve it. Defaults to "primary" so existing call sites and tests that don't pass it remain valid. The
workflow passes these arguments by position, so recorded histories depend on their order, not their names.
diff --git a/products/experiments/backend/temporal/recalculation_logic.py b/products/experiments/backend/temporal/recalculation_logic.py
index 929ab1c2ee4e..dbeb98a87d8c 100644
--- a/products/experiments/backend/temporal/recalculation_logic.py
+++ b/products/experiments/backend/temporal/recalculation_logic.py
@@ -128,10 +128,9 @@ def _discover_experiment_metrics_sync(recalculation_id: str) -> list[ExperimentM
experiment = recalculation.experiment
# request_recalculation sets total_metrics from the same resolver, so the run's progress count
- # matches the metrics this activity schedules. The payload keeps the role in metric_type because
- # recorded workflow histories carry that field name.
+ # matches the metrics this activity schedules.
metrics_to_recalculate = [
- ExperimentMetricToRecalculate(experiment_id=experiment.id, metric_uuid=metric.uuid, metric_type=metric.role)
+ ExperimentMetricToRecalculate(experiment_id=experiment.id, metric_uuid=metric.uuid, role=metric.role)
for metric in get_metrics_for_calculation(experiment)
]
@@ -524,7 +523,7 @@ def _capture_experiment_metric_event(
"""Emit a per-metric product analytics event. Telemetry must never fail the activity, so any error
is swallowed. Attributed to the experiment creator, falling back to a team-scoped distinct_id.
- `role` ("primary" or "secondary") is carried from discovery in `ExperimentMetricToRecalculate.metric_type`,
+ `role` ("primary" or "secondary") is carried from discovery in `ExperimentMetricToRecalculate.role`,
threaded through the workflow + activity args so the capture path doesn't have to re-query the M2M to
resolve it.
@@ -986,7 +985,7 @@ def _record_metric_out_of_time_failure_sync(failure: MetricOutOfTimeFailure) ->
_capture_experiment_metric_event(
experiment,
failure.metric_uuid,
- failure.metric_type,
+ failure.role,
calculation_config.definition,
"experiment metric error",
{"error_type": "timeout", "error_message": _OUT_OF_TIME_MESSAGE},
diff --git a/products/experiments/backend/temporal/recalculation_workflow.py b/products/experiments/backend/temporal/recalculation_workflow.py
index 172feed4dbfb..3d845b6580a8 100644
--- a/products/experiments/backend/temporal/recalculation_workflow.py
+++ b/products/experiments/backend/temporal/recalculation_workflow.py
@@ -166,7 +166,7 @@ class ExperimentMetricsRecalculationWorkflow(PostHogWorkflow):
metric.metric_uuid,
recalculation_id,
query_to,
- metric.metric_type,
+ metric.role,
],
start_to_close_timeout=timedelta(seconds=METRIC_CALC_ACTIVITY_TIMEOUT_SECONDS),
schedule_to_close_timeout=timedelta(seconds=METRIC_CALC_SCHEDULE_TO_CLOSE_TIMEOUT_SECONDS),
@@ -237,7 +237,7 @@ class ExperimentMetricsRecalculationWorkflow(PostHogWorkflow):
recalculation_id=recalculation_id,
experiment_id=metric.experiment_id,
metric_uuid=metric.metric_uuid,
- metric_type=metric.metric_type,
+ role=metric.role,
),
start_to_close_timeout=timedelta(seconds=METRIC_FAILURE_BACKSTOP_ACTIVITY_TIMEOUT_SECONDS),
schedule_to_close_timeout=timedelta(
diff --git a/products/experiments/backend/temporal/test_recalculation_activities.py b/products/experiments/backend/temporal/test_recalculation_activities.py
index b9b84d2ea0dc..0facc1710038 100644
--- a/products/experiments/backend/temporal/test_recalculation_activities.py
+++ b/products/experiments/backend/temporal/test_recalculation_activities.py
@@ -192,7 +192,7 @@ class TestRecalculationActivities(BaseTest):
recalc.refresh_from_db()
assert set(recalc.metric_uuids) == expected_uuids
assert {m.metric_uuid for m in metrics} == expected_uuids
- assert {m.metric_type for m in metrics} == expected_roles
+ assert {m.role for m in metrics} == expected_roles
@parameterized.expand(
[
@@ -912,7 +912,7 @@ class TestCalculateActivity(BaseTest):
with _record_captures() as captured:
_record_out_of_time(
MetricOutOfTimeFailure(
- recalculation_id=str(recalc.id), experiment_id=exp.id, metric_uuid="m1", metric_type="primary"
+ recalculation_id=str(recalc.id), experiment_id=exp.id, metric_uuid="m1", role="primary"
)
)
@@ -940,7 +940,7 @@ class TestCalculateActivity(BaseTest):
recalc = self._recalc(exp, metric_uuids=["m1"])
_record_out_of_time(
MetricOutOfTimeFailure(
- recalculation_id=str(recalc.id), experiment_id=exp.id, metric_uuid="m1", metric_type="primary"
+ recalculation_id=str(recalc.id), experiment_id=exp.id, metric_uuid="m1", role="primary"
)
)
@@ -1665,7 +1665,7 @@ class TestRecalculationAnalytics(BaseTest):
assert props["execution_mode"] == "recalculation"
def test_secondary_metric_is_not_marked_primary(self):
- # The workflow passes the role from discovery (ExperimentMetricToRecalculate.metric_type), and the
+ # The workflow passes the role from discovery (ExperimentMetricToRecalculate.role), and the
# calculation does not look it up again, so the test passes it explicitly to exercise the same path.
exp = self._experiment(flag_key="an-secondary", secondary=[_mean_metric("s1")])
recalc = self._recalc(exp, metric_uuids=["s1"])
diff --git a/products/experiments/backend/temporal/test_recalculation_payloads.py b/products/experiments/backend/temporal/test_recalculation_payloads.py
new file mode 100644
index 000000000000..70d50fa54e5a
--- /dev/null
+++ b/products/experiments/backend/temporal/test_recalculation_payloads.py
@@ -0,0 +1,44 @@
+from typing import Any
+
+import pytest
+
+import temporalio.converter
+from temporalio.contrib.pydantic import pydantic_data_converter
+
+from products.experiments.backend.temporal.models import ExperimentMetricToRecalculate, MetricOutOfTimeFailure
+
+CONVERTERS = {
+ "default": temporalio.converter.default().payload_converter,
+ "pydantic": pydantic_data_converter.payload_converter,
+}
+
+PAYLOADS_WITHOUT_ROLE: list[tuple[type, dict[str, Any]]] = [
+ (ExperimentMetricToRecalculate, {"experiment_id": 1, "metric_uuid": "m1", "metric_type": "secondary"}),
+ (
+ MetricOutOfTimeFailure,
+ {"recalculation_id": "r1", "experiment_id": 1, "metric_uuid": "m1", "metric_type": "secondary"},
+ ),
+]
+
+
[email protected]("converter_name", CONVERTERS)
[email protected]("payload_class,fields", PAYLOADS_WITHOUT_ROLE)
+def test_payload_without_role_decodes_with_role(converter_name: str, payload_class: type, fields: dict) -> None:
+ converter = CONVERTERS[converter_name]
+
+ decoded = converter.from_payloads(converter.to_payloads([fields]), [payload_class])[0]
+
+ assert decoded.role == "secondary"
+
+
[email protected]("converter_name", CONVERTERS)
[email protected]("payload_class,fields", PAYLOADS_WITHOUT_ROLE)
+def test_payload_with_role_still_carries_metric_type(converter_name: str, payload_class: type, fields: dict) -> None:
+ converter = CONVERTERS[converter_name]
+ fields_without_metric_type = {name: value for name, value in fields.items() if name != "metric_type"}
+ written = payload_class(**fields_without_metric_type, role="secondary")
+
+ encoded = converter.from_payloads(converter.to_payloads([written]), [dict])[0]
+
+ assert encoded["metric_type"] == "secondary"
+ assert encoded["role"] == "secondary"
diff --git a/products/experiments/backend/temporal/test_recalculation_workflow.py b/products/experiments/backend/temporal/test_recalculation_workflow.py
index 13fc386f3bc1..3cda8c2b7686 100644
--- a/products/experiments/backend/temporal/test_recalculation_workflow.py
+++ b/products/experiments/backend/temporal/test_recalculation_workflow.py
@@ -38,7 +38,7 @@ _ACTIVITIES_MODULE = "products.experiments.backend.temporal.recalculation_activi
def _metric(metric_uuid: str) -> ExperimentMetricToRecalculate:
- return ExperimentMetricToRecalculate(experiment_id=1, metric_uuid=metric_uuid, metric_type="primary")
+ return ExperimentMetricToRecalculate(experiment_id=1, metric_uuid=metric_uuid, role="primary")
def _make_mock_activities(
@@ -252,7 +252,7 @@ class TestExperimentMetricsRecalculationWorkflow:
metric_uuid: str,
recalculation_id: str,
query_to: str,
- metric_type: str = "primary",
+ role: str = "primary",
) -> MetricRecalculationResult:
info = activity.info()
timeouts.append((info.schedule_to_close_timeout, info.heartbeat_timeout))
@@ -292,7 +292,7 @@ class TestExperimentMetricsRecalculationWorkflow:
metric_uuid: str,
recalculation_id: str,
query_to: str,
- metric_type: str = "primary",
+ role: str = "primary",
) -> MetricRecalculationResult:
nonlocal attempts
if metric_uuid == "m2":
@@ -309,7 +309,7 @@ class TestExperimentMetricsRecalculationWorkflow:
assert result == {"total": 2, "succeeded": 1, "failed": 1}
assert attempts < MAX_METRIC_ATTEMPTS
- assert [(f.experiment_id, f.metric_uuid, f.metric_type) for f in backstop_calls] == [(1, "m1", "primary")]
+ assert [(f.experiment_id, f.metric_uuid, f.role) for f in backstop_calls] == [(1, "m1", "primary")]
async def test_workflow_cancel_reaches_the_running_calc_and_kills_its_query(self):
activities, _progress, _calls = _make_mock_activities(metrics=[_metric("m1")])