Skip to content

Commit a3cf4ad

Browse files
authored
Stop capturing all feature flags on $feature_flag_called event. (#181)
1 parent cec532f commit a3cf4ad

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

posthog/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def capture(
256256
except Exception as e:
257257
self.log.exception(f"[FEATURE FLAGS] Unable to get feature variants: {e}")
258258

259-
elif self.feature_flags:
259+
elif self.feature_flags and event != "$feature_flag_called":
260260
# Local evaluation is enabled, flags are loaded, so try and get all flags we can without going to the server
261261
feature_variants = self.get_all_flags(
262262
distinct_id, groups=(groups or {}), disable_geoip=disable_geoip, only_evaluate_locally=True

posthog/test/test_feature_flags.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,55 @@ def test_capture_is_called(self, patch_decide, patch_capture):
23392339
disable_geoip=None,
23402340
)
23412341

2342+
@mock.patch("posthog.client.decide")
2343+
def test_capture_is_called_but_does_not_add_all_flags(self, patch_decide):
2344+
patch_decide.return_value = {"featureFlags": {"decide-flag": "decide-value"}}
2345+
client = Client(FAKE_TEST_API_KEY, personal_api_key=FAKE_TEST_API_KEY)
2346+
client.feature_flags = [
2347+
{
2348+
"id": 1,
2349+
"name": "Beta Feature",
2350+
"key": "complex-flag",
2351+
"active": True,
2352+
"filters": {
2353+
"groups": [
2354+
{
2355+
"properties": [{"key": "region", "value": "USA"}],
2356+
"rollout_percentage": 100,
2357+
},
2358+
],
2359+
},
2360+
},
2361+
{
2362+
"id": 2,
2363+
"name": "Gamma Feature",
2364+
"key": "simple-flag",
2365+
"active": True,
2366+
"filters": {
2367+
"groups": [
2368+
{
2369+
"properties": [],
2370+
"rollout_percentage": 100,
2371+
},
2372+
],
2373+
},
2374+
},
2375+
]
2376+
2377+
self.assertTrue(
2378+
client.get_feature_flag("complex-flag", "some-distinct-id", person_properties={"region": "USA"})
2379+
)
2380+
2381+
# Grab the capture message that was just added to the queue
2382+
msg = client.queue.get(block=False)
2383+
assert msg["event"] == "$feature_flag_called"
2384+
assert msg["properties"]["$feature_flag"] == "complex-flag"
2385+
assert msg["properties"]["$feature_flag_response"] is True
2386+
assert msg["properties"]["locally_evaluated"] is True
2387+
assert msg["properties"]["$feature/complex-flag"] is True
2388+
assert "$feature/simple-flag" not in msg["properties"]
2389+
assert "$active_feature_flags" not in msg["properties"]
2390+
23422391
@mock.patch.object(Client, "capture")
23432392
@mock.patch("posthog.client.decide")
23442393
def test_capture_is_called_in_get_feature_flag_payload(self, patch_decide, patch_capture):

0 commit comments

Comments
 (0)