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(flags): classify person-resolution failures in flag test evaluation (#67573)

posthog[bot]6707cfd188ff
diff --git a/products/feature_flags/backend/api/feature_flag.py b/products/feature_flags/backend/api/feature_flag.py
index 1d66100dfd11..9b8d770cf7e6 100644
--- a/products/feature_flags/backend/api/feature_flag.py
+++ b/products/feature_flags/backend/api/feature_flag.py
@@ -15,6 +15,7 @@ from django.contrib.postgres.aggregates import ArrayAgg
from django.db import transaction
from django.db.models import Count, Prefetch, Q, QuerySet, deletion
+import grpc
import structlog
from drf_spectacular.types import OpenApiTypes
from drf_spectacular.utils import OpenApiExample, OpenApiParameter, OpenApiResponse, extend_schema_field
@@ -3813,6 +3814,7 @@ class FeatureFlagViewSet(
404: OpenApiResponse(response=ErrorResponseSerializer, description="Person not found"),
500: OpenApiResponse(response=ErrorResponseSerializer, description="Server error"),
502: OpenApiResponse(response=ErrorResponseSerializer, description="Flag evaluation service error"),
+ 503: OpenApiResponse(response=ErrorResponseSerializer, description="Person lookup service unavailable"),
},
)
@action(
@@ -3837,15 +3839,39 @@ class FeatureFlagViewSet(
timestamp = request.validated_data.get("timestamp")
groups = request.validated_data.get("groups") or {}
+ # The identifier we were asked to resolve. Logged on every failure path below so a
+ # personhog RPC outage, a genuinely missing person, and bad input can be told apart
+ # instead of collapsing into one opaque 500 (which left this endpoint's failures
+ # unclassifiable in tool-call telemetry).
+ identifier_type = "person_id" if person_id else "distinct_id"
+ identifier_value = person_id or distinct_id
+ log_context = {
+ "team_id": self.team_id,
+ "feature_flag_id": feature_flag.id,
+ "identifier_type": identifier_type,
+ "identifier_value": identifier_value,
+ }
+
# Resolve person and distinct_ids
try:
person, distinct_ids = get_person_and_distinct_ids_for_identifier(
team_id=self.team_id, distinct_id=distinct_id, person_id=person_id
)
except ValueError as e:
- capture_exception(e)
+ # Bad input shape (both identifiers, neither, or an empty value) — caller error, not a fault.
+ logger.warning("Invalid identifier for flag test evaluation: %s", e, extra=log_context)
return Response({"error": "Invalid parameters"}, status=status.HTTP_400_BAD_REQUEST)
+ except grpc.RpcError as e:
+ # personhog is unreachable/erroring. This is a transient dependency failure, not a
+ # missing person or bad input, so surface a distinct retryable 503 rather than a 500.
+ logger.exception("personhog RPC failed resolving person for flag test evaluation", extra=log_context)
+ capture_exception(e)
+ return Response(
+ {"error": "Person lookup service temporarily unavailable. Please retry."},
+ status=status.HTTP_503_SERVICE_UNAVAILABLE,
+ )
except Exception as e:
+ logger.exception("Unexpected error resolving person for flag test evaluation", extra=log_context)
capture_exception(e)
return Response({"error": "Failed to resolve person"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
@@ -4102,6 +4128,8 @@ class FeatureFlagViewSet(
timestamp,
e,
extra={
+ "team_id": self.team_id,
+ "feature_flag_id": feature_flag.id,
"flag_key": feature_flag.key,
"distinct_id": distinct_id,
"person_id": person_id,
diff --git a/products/feature_flags/backend/api/test/test_feature_flag.py b/products/feature_flags/backend/api/test/test_feature_flag.py
index bbd05c84b53f..98f47647cced 100644
--- a/products/feature_flags/backend/api/test/test_feature_flag.py
+++ b/products/feature_flags/backend/api/test/test_feature_flag.py
@@ -19,6 +19,7 @@ from django.core.cache import cache
from django.test import override_settings
from django.utils.timezone import now
+import grpc
import requests
from parameterized import parameterized
from prometheus_client import REGISTRY
@@ -12978,6 +12979,22 @@ class TestFeatureFlagTestEvaluation(APIBaseTest, ClickhouseTestMixin):
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.json()["detail"], f"Person not found for person_id: {missing_person_id}")
+ @patch("products.feature_flags.backend.api.feature_flag.get_person_and_distinct_ids_for_identifier")
+ def test_test_evaluation_personhog_rpc_failure_returns_503(self, mock_get_person):
+ # A personhog RPC outage must surface a distinct retryable 503, not the opaque 500 that
+ # used to swallow it alongside genuine "person not found" and bad-input cases.
+ flag = FeatureFlag.objects.create(team=self.team, key="test-flag")
+ mock_get_person.side_effect = grpc.RpcError("personhog unavailable")
+
+ response = self.client.post(
+ f"/api/projects/{self.team.pk}/feature_flags/{flag.id}/test_evaluation/",
+ {"distinct_id": "test-user"},
+ format="json",
+ )
+
+ self.assertEqual(response.status_code, status.HTTP_503_SERVICE_UNAVAILABLE)
+ self.assertEqual(response.json()["error"], "Person lookup service temporarily unavailable. Please retry.")
+
@patch("products.feature_flags.backend.api.feature_flag.get_flags_from_service")
@patch("products.feature_flags.backend.api.feature_flag.get_person_and_distinct_ids_for_identifier")
@override_settings(INTERNAL_REQUEST_TOKEN="test-token")