: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(flags): degrade group-type lookups when personhog client unset (#68471)
posthog[bot]08f53e36ca64
diff --git a/posthog/models/group_type_mapping.py b/posthog/models/group_type_mapping.pyindex c8bb24a4cb49..42cb1b36bf7c 100644--- a/posthog/models/group_type_mapping.py+++ b/posthog/models/group_type_mapping.py@@ -195,11 +195,16 @@ def get_group_types_for_project(project_id: int, *, caller_tag: str | None = Nonif cached is not None:return cached- try:+ def _fetch() -> list[dict[str, Any]]:+ # require_personhog_client() must run inside personhog_call so a missing client+ # (RuntimeError) is wrapped as DatabaseError and recovered like any fetch failure.client = require_personhog_client()+ return _fetch_group_types_via_personhog(client, project_id)++ try:result = personhog_call("get_group_types_for_project",- lambda: _fetch_group_types_via_personhog(client, project_id),+ _fetch,caller_tag=f"group_type_mapping/{caller_tag or 'get_group_types_for_project'}",reraise_as=DatabaseError,)@@ -234,11 +239,17 @@ def _fetch_group_types_for_team_via_personhog(client: PersonHogClient, team_id:def get_group_types_for_team(team_id: int, *, caller_tag: str | None = None) -> list[dict[str, Any]]:"""Fetch group types for a team via personhog."""- try:++ def _fetch() -> list[dict[str, Any]]:+ # require_personhog_client() must run inside personhog_call so a missing client+ # (RuntimeError) is wrapped as DatabaseError and recovered like any fetch failure.client = require_personhog_client()+ return _fetch_group_types_for_team_via_personhog(client, team_id)++ try:return personhog_call("get_group_types_for_team",- lambda: _fetch_group_types_for_team_via_personhog(client, team_id),+ _fetch,caller_tag=f"group_type_mapping/{caller_tag or 'get_group_types_for_team'}",reraise_as=DatabaseError,)@@ -440,7 +451,7 @@ def get_group_types_for_projects(mapping. Callers must handle that case."""- def _fn() -> dict[int, list[dict[str, Any]]]:+ def _fetch() -> dict[int, list[dict[str, Any]]]:client = require_personhog_client()result = _fetch_group_types_for_projects_via_personhog(client, project_ids)for pid in project_ids:@@ -450,7 +461,7 @@ def get_group_types_for_projects(try:result = personhog_call("get_group_types_for_projects",- _fn,+ _fetch,caller_tag=f"group_type_mapping/{caller_tag or 'get_group_types_for_projects'}",reraise_as=DatabaseError,)@@ -466,14 +477,19 @@ def count_group_type_mappings_per_team(*, caller_tag: str | None = None) -> list"""Count group type mappings per team via personhog."""from posthog.personhog_client.proto import CountGroupTypeMappingsRequest- try:+ def _fetch() -> list[dict[str, int]]:+ # require_personhog_client() must run inside personhog_call so a missing client+ # (RuntimeError) is wrapped as DatabaseError and recovered like any fetch failure.client = require_personhog_client()+ return [+ {"team_id": c.team_id, "total": c.count}+ for c in client.count_group_type_mappings(CountGroupTypeMappingsRequest()).counts+ ]++ try:return personhog_call("count_group_type_mappings_per_team",- lambda: [- {"team_id": c.team_id, "total": c.count}- for c in client.count_group_type_mappings(CountGroupTypeMappingsRequest()).counts- ],+ _fetch,caller_tag=f"group_type_mapping/{caller_tag or 'count_group_type_mappings_per_team'}",reraise_as=DatabaseError,)@@ -556,10 +572,11 @@ def _fetch_group_types_for_project_direct(from posthog.personhog_client.converters import proto_group_type_mapping_to_dictfrom posthog.personhog_client.proto import GetGroupTypeMappingsByProjectIdRequest- client = require_personhog_client()- return personhog_call(- "get_group_types_for_project_direct",- lambda: sorted(+ def _fetch() -> list[dict[str, Any]]:+ # require_personhog_client() must run inside personhog_call so a missing client+ # (RuntimeError) is wrapped as DatabaseError and recovered like any fetch failure.+ client = require_personhog_client()+ return sorted([proto_group_type_mapping_to_dict(m)for m in client.get_group_type_mappings_by_project_id(@@ -570,7 +587,11 @@ def _fetch_group_types_for_project_direct().mappings],key=lambda d: d["group_type_index"],- ),+ )++ return personhog_call(+ "get_group_types_for_project_direct",+ _fetch,caller_tag=f"group_type_mapping/{caller_tag or 'get_group_types_for_project_direct'}",reraise_as=DatabaseError,)diff --git a/posthog/models/test/test_group_type_mapping.py b/posthog/models/test/test_group_type_mapping.pyindex 3605fa7edb69..d9f2b0dd0f4c 100644--- a/posthog/models/test/test_group_type_mapping.py+++ b/posthog/models/test/test_group_type_mapping.py@@ -2,6 +2,8 @@ from unittest.mock import MagicMock, patchfrom django.test import SimpleTestCase, override_settings+from parameterized import parameterized+from posthog.models.group_type_mapping import (GROUP_TYPES_CACHE_KEY_PREFIX,GROUP_TYPES_CONFIRMED_EMPTY_CACHE_KEY_PREFIX,@@ -849,6 +851,36 @@ class TestProjectHasGroupTypesAuthoritatively(SimpleTestCase):assert get_safe_cache(marker_key) is None+# Missing client (PERSONHOG_ADDR unset) raises RuntimeError; read paths must recover+# like a DatabaseError instead of letting it escape and 500 the caller.+class TestUnconfiguredClientDegradesGracefully(SimpleTestCase):+ def setUp(self):+ self.project_id = 314159+ _clear_cache(self.project_id)+ safe_cache_delete(f"{GROUP_TYPES_CONFIRMED_EMPTY_CACHE_KEY_PREFIX}{self.project_id}")+ self._client_patcher = patch(_CLIENT_PATCH, return_value=None)+ self._client_patcher.start()++ def tearDown(self):+ self._client_patcher.stop()+ _clear_cache(self.project_id)+ safe_cache_delete(f"{GROUP_TYPES_CONFIRMED_EMPTY_CACHE_KEY_PREFIX}{self.project_id}")++ @parameterized.expand(+ [+ ("project", lambda self: get_group_types_for_project(self.project_id)),+ ("team", lambda self: get_group_types_for_team(42)),+ ("count", lambda self: count_group_type_mappings_per_team()),+ ]+ )+ def test_read_path_returns_empty(self, _name, call):+ assert call(self) == []++ def test_project_has_group_types_fails_closed(self):+ # Unconfirmable state must not be treated as "safe to empty" — fail closed to True.+ assert project_has_group_types_authoritatively(self.project_id) is True++class TestDictToGroupTypeMappingModel(SimpleTestCase):def test_builds_model_from_full_dict(self):row = {