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): stop one bad row from aborting flag enrichment (#117418)

posthog[bot]5ff67bbe7842
diff --git a/posthog/test/test_feature_flag_analytics.py b/posthog/test/test_feature_flag_analytics.py
index fdddd563b427..261a326a0ec5 100644
--- a/posthog/test/test_feature_flag_analytics.py
+++ b/posthog/test/test_feature_flag_analytics.py
@@ -1105,6 +1105,19 @@ class TestEnrichedAnalytics(BaseTest):
flag.refresh_from_db()
self.assertEqual(flag.has_enriched_analytics, True)
+ @parameterized.expand([("empty_row", ()), ("non_sequence_row", 1), ("missing_team", (999999, "test_flag"))])
+ def test_bad_row_does_not_stop_later_rows(self, _name: str, bad_row: object) -> None:
+ flag = FeatureFlag.objects.create(team=self.team, key="test_flag", created_by=self.user)
+
+ with patch(
+ "products.feature_flags.backend.flag_analytics.sync_execute",
+ return_value=[bad_row, (self.team.id, "test_flag")],
+ ):
+ find_flags_with_enriched_analytics(datetime.datetime(2021, 1, 1), datetime.datetime(2021, 1, 2))
+
+ flag.refresh_from_db()
+ self.assertEqual(flag.has_enriched_analytics, True)
+
class TestFindFlagsWithEnrichedAnalyticsTask(SimpleTestCase):
@patch("products.feature_flags.backend.flag_analytics.find_flags_with_enriched_analytics")
diff --git a/products/feature_flags/backend/flag_analytics.py b/products/feature_flags/backend/flag_analytics.py
index ccf047af9550..f7fedf4ed752 100644
--- a/products/feature_flags/backend/flag_analytics.py
+++ b/products/feature_flags/backend/flag_analytics.py
@@ -8,6 +8,8 @@ from django.core.cache import cache
from django.db import transaction
from django.db.models import Q
+import structlog
+
from posthog.clickhouse.client import sync_execute
from posthog.clickhouse.materialized_columns import get_materialized_column_for_property
from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
@@ -25,6 +27,8 @@ from products.feature_flags.backend.models.feature_flag import FeatureFlag
if TYPE_CHECKING:
from posthoganalytics import Posthog
+logger = structlog.get_logger(__name__)
+
REDIS_LOCK_TOKEN = "posthog:decide_analytics:lock"
CACHE_BUCKET_SIZE = 60 * 2 # duration in seconds
@@ -269,11 +273,17 @@ def find_flags_with_enriched_analytics(begin: datetime, end: datetime):
result = sync_execute(_build_enriched_analytics_query(), {"begin": begin, "end": end})
for row in result:
- team_id = row[0]
- flag_key = row[1]
- team = Team.objects.only("project_id").get(id=team_id)
+ if not isinstance(row, tuple | list) or len(row) < 2:
+ logger.warning(
+ "find_flags_with_enriched_analytics_malformed_row",
+ row_type=type(row).__name__,
+ row_length=len(row) if isinstance(row, tuple | list) else None,
+ )
+ continue
+ team_id, flag_key = row[0], row[1]
try:
+ team = Team.objects.only("project_id").get(id=team_id)
flag = FeatureFlag.objects.get(team__project_id=team.project_id, key=flag_key)
if not flag.has_enriched_analytics:
# A failed enrichment rolls back the flag update, so the next run retries this
@@ -283,7 +293,7 @@ def find_flags_with_enriched_analytics(begin: datetime, end: datetime):
flag.save()
if flag.usage_dashboard and not flag.usage_dashboard_has_enriched_insights:
add_enriched_insights_to_feature_flag_dashboard(flag, flag.usage_dashboard)
- except FeatureFlag.DoesNotExist:
+ except (Team.DoesNotExist, FeatureFlag.DoesNotExist):
pass
except Exception as e:
capture_exception(e)