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.

fix(endpoints): only count a fallback when inline recovers (#77949)

Thiago Salvatore70a951e69b75
diff --git a/products/endpoints/backend/logic/execution.py b/products/endpoints/backend/logic/execution.py
index 795975558a8b..34773fc90453 100644
--- a/products/endpoints/backend/logic/execution.py
+++ b/products/endpoints/backend/logic/execution.py
@@ -528,6 +528,7 @@ class EndpointExecutionService(PydanticModelMixin):
_ch_query_start = time.monotonic()
try:
result: Response | None = None
+ materialized_failed = False
if use_materialized:
try:
result = self._execute_materialized_endpoint(
@@ -542,10 +543,13 @@ class EndpointExecutionService(PydanticModelMixin):
except ConcurrencyLimitExceeded:
raise
except Exception:
- # Already logged/captured/signaled inside the materialized path. Serve the
- # request from the original query instead of failing — stale tables and
- # series drift self-heal on the next materialization run.
- execution_type = "materialized_fallback"
+ # Already logged/captured/signaled inside the materialized path. Re-run
+ # inline: only stamp materialized_fallback once inline succeeds, because
+ # only an inline success proves the materialized table was the sole thing
+ # broken. If inline also fails the request was never recoverable (a bad
+ # query fails on both paths) — that's an inline failure, not a fallback.
+ materialized_failed = True
+ execution_type = "inline"
result = None
if result is None:
@@ -558,6 +562,8 @@ class EndpointExecutionService(PydanticModelMixin):
limit=limit,
offset=offset,
)
+ if materialized_failed:
+ execution_type = "materialized_fallback"
# Query-only wall-clock, to compare fairly with the DuckLake shadow.
_ch_query_ms = (time.monotonic() - _ch_query_start) * 1000
execution_status = "success"
diff --git a/products/endpoints/backend/tests/test_endpoint_execution.py b/products/endpoints/backend/tests/test_endpoint_execution.py
index ccf5c817b921..edf319b4d4f1 100644
--- a/products/endpoints/backend/tests/test_endpoint_execution.py
+++ b/products/endpoints/backend/tests/test_endpoint_execution.py
@@ -2457,6 +2457,36 @@ class TestEndpointExecution(ClickhouseTestMixin, APIBaseTest):
mock_trigger.assert_called_once()
self.assertEqual(mock_exec.call_count, 2, "expected materialized attempt then inline fallback")
+ def test_unrecoverable_failure_not_labeled_materialized_fallback(self):
+ from prometheus_client import REGISTRY
+
+ endpoint = self._make_fresh_materialized_endpoint(
+ "mat-both-fail", {"kind": "HogQLQuery", "query": "SELECT count() FROM events"}
+ )
+ fallback_labels = {"execution_type": "materialized_fallback", "query_kind": "hogql", "status": "user_error"}
+ inline_labels = {"execution_type": "inline", "query_kind": "hogql", "status": "user_error"}
+ fallback_before = REGISTRY.get_sample_value("posthog_endpoint_execution_total", fallback_labels) or 0.0
+ inline_before = REGISTRY.get_sample_value("posthog_endpoint_execution_total", inline_labels) or 0.0
+
+ with mock.patch.object(
+ EndpointExecutionService,
+ "_execute_query_and_respond",
+ side_effect=[RuntimeError("materialized table exploded"), ExposedHogQLError("Unknown field: bad")],
+ ) as mock_exec:
+ response = self.client.post(
+ f"/api/environments/{self.team.id}/endpoints/{endpoint.name}/run/", {}, format="json"
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
+ self.assertEqual(mock_exec.call_count, 2, "expected materialized attempt then inline retry")
+
+ fallback_after = REGISTRY.get_sample_value("posthog_endpoint_execution_total", fallback_labels) or 0.0
+ inline_after = REGISTRY.get_sample_value("posthog_endpoint_execution_total", inline_labels) or 0.0
+ self.assertEqual(
+ fallback_after - fallback_before, 0.0, "an unrecoverable request must not count as materialized_fallback"
+ )
+ self.assertEqual(inline_after - inline_before, 1.0)
+
def test_emit_failure_signal_reaches_workflow_boundary(self):
"""The failure-signal plumbing must make it to the Temporal boundary when the
org/source gates allow it — anything raising before that is a plumbing bug."""