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): make execution metrics actionable for alerting (#63217)

Jovan Sakovic6ed8b4cf0374
diff --git a/products/endpoints/backend/api.py b/products/endpoints/backend/api.py
index 244960bf6ea0..68c3ba7915ab 100644
--- a/products/endpoints/backend/api.py
+++ b/products/endpoints/backend/api.py
@@ -113,7 +113,7 @@ from products.endpoints.backend.metrics import (
ENDPOINT_EXECUTION_TOTAL,
ENDPOINT_HOGQL_RESULT_ROWS,
ENDPOINT_MATERIALIZATION_EVENT_TOTAL,
- ENDPOINT_MATERIALIZED_AGE_SECONDS,
+ ENDPOINT_MATERIALIZED_FRESHNESS_RATIO,
ENDPOINT_VALIDATION_ERROR_TOTAL,
query_kind_label,
)
@@ -1899,9 +1899,11 @@ class EndpointViewSet(
trigger_saved_query_schedule(saved_query)
raise
- if saved_query.last_run_at:
+ # Freshness relative to the configured target: >1.0 means behind SLA.
+ # Absolute age is meaningless across endpoints with different frequencies.
+ if saved_query.last_run_at and version and version.data_freshness_seconds:
age_seconds = max((timezone.now() - saved_query.last_run_at).total_seconds(), 0.0)
- ENDPOINT_MATERIALIZED_AGE_SECONDS.observe(age_seconds)
+ ENDPOINT_MATERIALIZED_FRESHNESS_RATIO.observe(age_seconds / version.data_freshness_seconds)
return result
except Exception as e:
@@ -2441,7 +2443,7 @@ class EndpointViewSet(
)
execution_status = "success"
except (ExposedHogQLError, ExposedCHQueryError) as e:
- execution_status = "error"
+ execution_status = "user_error"
error_label = getattr(e, "code_name", None) or type(e).__name__
logger.exception(
"Endpoint execution failed",
@@ -2450,7 +2452,7 @@ class EndpointViewSet(
)
raise ValidationError("Query execution failed.", getattr(e, "code_name", None))
except HogVMException:
- execution_status = "error"
+ execution_status = "user_error"
error_label = "HogVMException"
logger.exception(
"Endpoint execution failed (HogVM)",
@@ -2458,7 +2460,7 @@ class EndpointViewSet(
)
raise ValidationError("Query execution failed: HogQL virtual machine error")
except ResolutionError:
- execution_status = "error"
+ execution_status = "user_error"
error_label = "ResolutionError"
logger.exception(
"Endpoint resolution failed",
@@ -2466,7 +2468,7 @@ class EndpointViewSet(
)
raise ValidationError("Query resolution failed: unable to resolve table or field references.")
except ConcurrencyLimitExceeded:
- ENDPOINT_CONCURRENCY_REJECTED_TOTAL.inc()
+ ENDPOINT_CONCURRENCY_REJECTED_TOTAL.labels(team_id=str(self.team_id)).inc()
raise Throttled(detail="Too many concurrent requests. Please try again later.")
except Exception as e:
execution_status = "error"
@@ -2481,7 +2483,7 @@ class EndpointViewSet(
ENDPOINT_EXECUTION_TOTAL.labels(
execution_type=execution_type, query_kind=query_kind_metric, status=execution_status
).inc()
- if execution_status == "error":
+ if execution_status in ("error", "user_error"):
log_endpoint_execution(
team_id=self.team_id,
endpoint_id=str(endpoint.id),
diff --git a/products/endpoints/backend/metrics.py b/products/endpoints/backend/metrics.py
index 3ffbb298e525..5464edfbd8cd 100644
--- a/products/endpoints/backend/metrics.py
+++ b/products/endpoints/backend/metrics.py
@@ -9,7 +9,8 @@ def query_kind_label(query: dict | None) -> str:
ENDPOINT_EXECUTION_TOTAL = Counter(
"posthog_endpoint_execution_total",
- "Endpoint executions that reached query execution (excludes concurrency rejections; see posthog_endpoint_concurrency_rejected_total)",
+ "Endpoint executions that reached query execution (excludes concurrency rejections; see posthog_endpoint_concurrency_rejected_total). "
+ "status is success, user_error (invalid query/variables in the user's endpoint, returned as 4xx), or error (unexpected system failure)",
labelnames=["execution_type", "query_kind", "status"],
)
@@ -40,6 +41,7 @@ ENDPOINT_RATE_LIMITED_TOTAL = Counter(
ENDPOINT_CONCURRENCY_REJECTED_TOTAL = Counter(
"posthog_endpoint_concurrency_rejected_total",
"Endpoint executions rejected because the concurrency limit was exceeded",
+ labelnames=["team_id"],
)
ENDPOINT_CACHE_RESULT_TOTAL = Counter(
@@ -61,8 +63,8 @@ ENDPOINT_VALIDATION_ERROR_TOTAL = Counter(
labelnames=["reason"],
)
-ENDPOINT_MATERIALIZED_AGE_SECONDS = Histogram(
- "posthog_endpoint_materialized_age_seconds",
- "Age of the materialized data served, observed when a materialized table is used",
- buckets=(60, 300, 1800, 3600, 21600, 43200, 86400, 172800, 259200, 604800, 2592000, float("inf")),
+ENDPOINT_MATERIALIZED_FRESHNESS_RATIO = Histogram(
+ "posthog_endpoint_materialized_freshness_ratio",
+ "Age of the served materialized data relative to its data_freshness_seconds target; >1.0 means behind SLA",
+ buckets=(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 1.0, 1.1, 1.25, 1.5, 2, 5, 10, float("inf")),
)