diff --git a/.sampo/changesets/release-id-on-minimized-flag-called.md b/.sampo/changesets/release-id-on-minimized-flag-called.md new file mode 100644 index 000000000..7dea6a275 --- /dev/null +++ b/.sampo/changesets/release-id-on-minimized-flag-called.md @@ -0,0 +1,5 @@ +--- +posthog: patch +--- + +A minimized `$feature_flag_called` event keeps `$release_id`. The release id the SDK reads once from `POSTHOG_RELEASE_ID` was stamped on the event and then stripped by the minimal-event allowlist, so flag calls could not be broken down by release. It is static, low-cardinality runtime identity, like the `$lib` and `$os` values the allowlist already keeps. diff --git a/posthog/client.py b/posthog/client.py index d71aa13d7..85a72c3a4 100644 --- a/posthog/client.py +++ b/posthog/client.py @@ -432,6 +432,8 @@ def wrapper(self, *args, **kwargs): "$os_distro", "$python_runtime", "$python_version", + # Deploy identity: one value per process, set once at construction. + "$release_id", } ) diff --git a/posthog/test/test_feature_flag_called_minimization.py b/posthog/test/test_feature_flag_called_minimization.py index 2106ae778..95c6c342c 100644 --- a/posthog/test/test_feature_flag_called_minimization.py +++ b/posthog/test/test_feature_flag_called_minimization.py @@ -8,6 +8,7 @@ to the full legacy shape. """ +import os import unittest from unittest import mock @@ -15,6 +16,7 @@ from posthog.capture_event import _to_v1_event from posthog.client import _MINIMAL_FLAG_CALLED_EVENT_PROPERTIES, Client +from posthog.release_id import RELEASE_ID_ENV_VAR from posthog.request import GetResponse from posthog.test.capture_helpers import record_sync_capture_sends from posthog.test.test_utils import FAKE_TEST_API_KEY @@ -139,6 +141,18 @@ def test_gated_non_experiment_flag_sends_exactly_the_allowlist(self, patch_flags self.assertEqual(properties["$feature_flag_request_id"], "req-1") self.assertEqual(properties["$feature_flag_evaluated_at"], 1640995200000) + @mock.patch("posthog.client.flags") + def test_gated_non_experiment_flag_keeps_the_release_id(self, patch_flags): + patch_flags.return_value = _flags_response(has_experiment=False, gate=True) + with mock.patch.dict(os.environ, {RELEASE_ID_ENV_VAR: "rel-abc123"}): + client, captured = self._make_client() + + client.get_feature_flag_result("person-flag", "some-distinct-id") + + properties = self._flag_called_properties(captured) + self.assertEqual(properties["$release_id"], "rel-abc123") + self.assertLessEqual(set(properties), _MINIMAL_FLAG_CALLED_EVENT_PROPERTIES) + @mock.patch("posthog.client.flags") def test_gated_non_experiment_flag_with_groups_keeps_group_context( self, patch_flags diff --git a/posthog/test/test_release_id.py b/posthog/test/test_release_id.py index 8d72a12bf..6609ee584 100644 --- a/posthog/test/test_release_id.py +++ b/posthog/test/test_release_id.py @@ -132,8 +132,25 @@ def test_release_id_is_read_once_at_client_init(self) -> None: client.capture("python test event", distinct_id="user-1") self.assertEqual(events[0]["properties"]["$release_id"], "at-init") - def test_minimal_flag_called_events_keep_their_strict_allowlist(self) -> None: + def test_minimal_flag_called_events_keep_the_release_id(self) -> None: client, events = self._client("0198c1a2-release") + client._enqueue( + { + "event": "$feature_flag_called", + "distinct_id": "user-1", + "timestamp": None, + "properties": {"$feature_flag": "my-flag", "dropped": "value"}, + }, + None, + property_allowlist=_MINIMAL_FLAG_CALLED_EVENT_PROPERTIES, + ) + properties = events[0]["properties"] + self.assertEqual(properties["$feature_flag"], "my-flag") + self.assertEqual(properties["$release_id"], "0198c1a2-release") + self.assertNotIn("dropped", properties) + + def test_minimal_flag_called_events_omit_an_unset_release_id(self) -> None: + client, events = self._client(None) client._enqueue( { "event": "$feature_flag_called", @@ -144,7 +161,6 @@ def test_minimal_flag_called_events_keep_their_strict_allowlist(self) -> None: None, property_allowlist=_MINIMAL_FLAG_CALLED_EVENT_PROPERTIES, ) - self.assertEqual(events[0]["properties"]["$feature_flag"], "my-flag") self.assertNotIn("$release_id", events[0]["properties"])