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(experiments): emit experiment metric retry event from recalc orchestrator (#77603)

Rodrigo Iloro44762b204016
diff --git a/products/experiments/backend/temporal/recalculation_logic.py b/products/experiments/backend/temporal/recalculation_logic.py
index d6d845d57994..c6f926a25d6e 100644
--- a/products/experiments/backend/temporal/recalculation_logic.py
+++ b/products/experiments/backend/temporal/recalculation_logic.py
@@ -748,14 +748,31 @@ def _calculate_experiment_metric_for_recalculation_sync(
else:
# Exact, not estimated: this branch sets the delay explicitly via next_retry_delay.
error_type = classify_experiment_query_error(e)
+ safe_message = _safe_retry_message(e, error_type)
_record_retry(
recalculation_id,
metric_uuid,
attempt,
error_type,
- _safe_retry_message(e, error_type),
+ safe_message,
CONCURRENCY_LIMIT_RETRY_DELAY_SECONDS,
)
+ _capture_experiment_metric_event(
+ experiment,
+ metric_uuid,
+ metric_type,
+ metric_dict,
+ "experiment metric retry",
+ {
+ "duration_ms": round((time.perf_counter() - calc_started_at) * 1000),
+ "error_type": error_type,
+ "error_message": safe_message,
+ "attempt": attempt,
+ "max_attempts": MAX_METRIC_ATTEMPTS,
+ "next_retry_delay_seconds": CONCURRENCY_LIMIT_RETRY_DELAY_SECONDS,
+ },
+ trigger=state.trigger,
+ )
raise ApplicationError(
message,
type=type(e).__name__,
@@ -810,13 +827,31 @@ def _calculate_experiment_metric_for_recalculation_sync(
if is_final_attempt or is_permanent:
_clear_retry(recalculation_id, metric_uuid)
else:
+ retry_delay_seconds = _estimated_retry_delay_seconds(attempt)
+ safe_message = _safe_retry_message(e, error_type)
_record_retry(
recalculation_id,
metric_uuid,
attempt,
error_type,
- _safe_retry_message(e, error_type),
- _estimated_retry_delay_seconds(attempt),
+ safe_message,
+ retry_delay_seconds,
+ )
+ _capture_experiment_metric_event(
+ experiment,
+ metric_uuid,
+ metric_type,
+ metric_dict,
+ "experiment metric retry",
+ {
+ "duration_ms": round((time.perf_counter() - calc_started_at) * 1000),
+ "error_type": error_type,
+ "error_message": safe_message,
+ "attempt": attempt,
+ "max_attempts": MAX_METRIC_ATTEMPTS,
+ "next_retry_delay_seconds": retry_delay_seconds,
+ },
+ trigger=state.trigger,
)
if is_permanent:
raise ApplicationError(message, type=error_type, non_retryable=True) from e
diff --git a/products/experiments/backend/temporal/test_recalculation_activities.py b/products/experiments/backend/temporal/test_recalculation_activities.py
index 556802b7c19a..952cbdd97d0b 100644
--- a/products/experiments/backend/temporal/test_recalculation_activities.py
+++ b/products/experiments/backend/temporal/test_recalculation_activities.py
@@ -1095,21 +1095,62 @@ class TestRecalculationAnalytics(BaseTest):
assert captured[0]["properties"]["is_primary"] is False
- def test_non_final_failure_emits_no_per_metric_event(self):
- # A non-final attempt re-raises for Temporal to retry; emitting there would double-count a
- # failure that may still succeed on a later attempt. Only the terminal attempt emits.
- exp = self._experiment(flag_key="an-transient", metrics=[_mean_metric("m1")])
+ @parameterized.expand(
+ [
+ # (name, exc, expected_error_type, expected_delay_seconds, expected_message) — backpressure uses
+ # the explicit concurrency delay; a generic transient on attempt 2 follows the policy backoff
+ # (5 * 2^1). The generic exception carries a fake credential to lock in that raw exception text
+ # (which can embed the executed query, warehouse secrets included) never reaches analytics.
+ (
+ "backpressure",
+ ConcurrencyLimitExceeded("quota"),
+ "rate_limited",
+ 60,
+ "The query was deferred because the cluster is at capacity.",
+ ),
+ (
+ "generic_transient",
+ RuntimeError("blip aws_access_key_id=AKIAFAKESECRET"),
+ "server_error",
+ 10,
+ "The query failed with a server error.",
+ ),
+ ]
+ )
+ def test_non_final_failure_emits_retry_event(
+ self,
+ name: str,
+ exc: Exception,
+ expected_error_type: str,
+ expected_delay_seconds: int,
+ expected_message: str,
+ ):
+ # A non-final attempt re-raises for Temporal to retry. It emits 'experiment metric retry' (not
+ # 'experiment metric error', which is reserved for the terminal attempt) so retries that later
+ # succeed are still countable. Guards against dropping the retry emit, double-counting a
+ # non-terminal failure as an error, and leaking the raw exception (secrets included) into analytics.
+ exp = self._experiment(flag_key=f"an-retry-{name}", metrics=[_mean_metric("m1")])
recalc = self._recalc(exp, metric_uuids=["m1"])
with _record_captures() as captured:
with patch(
"products.experiments.backend.temporal.recalculation_logic.ExperimentQueryRunner"
) as mock_runner:
- mock_runner.return_value.run.side_effect = RuntimeError("kaboom")
- with pytest.raises(RuntimeError, match="kaboom"):
- _calculate(exp.id, "m1", str(recalc.id), _QUERY_TO, is_final_attempt=False)
+ mock_runner.return_value.run.side_effect = exc
+ with pytest.raises((type(exc), ApplicationError)):
+ _calculate(exp.id, "m1", str(recalc.id), _QUERY_TO, is_final_attempt=False, attempt=2)
- assert captured == []
+ assert len(captured) == 1
+ assert captured[0]["event"] == "experiment metric retry"
+ props = captured[0]["properties"]
+ assert props["error_type"] == expected_error_type
+ assert props["error_message"] == expected_message
+ assert "AKIAFAKESECRET" not in props["error_message"]
+ assert props["attempt"] == 2
+ assert props["max_attempts"] == MAX_METRIC_ATTEMPTS
+ assert props["next_retry_delay_seconds"] == expected_delay_seconds
+ assert props["execution_mode"] == "recalculation"
+ assert props["mechanism"] == "orchestrated"
@parameterized.expand(
[