Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 3.25.0 – 2025-04-15

1. Roll out new `/flags` endpoint to 100% of `/decide` traffic, excluding the top 10 customers.

## 3.24.3 – 2025-04-15

1. Fix hash inclusion/exclusion for flag rollout
Expand Down
2 changes: 1 addition & 1 deletion posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
MAX_DICT_SIZE = 50_000

# TODO: Get rid of these when you're done rolling out `/flags` to all customers
ROLLOUT_PERCENTAGE = 0.1
ROLLOUT_PERCENTAGE = 1
INCLUDED_HASHES = set({"bc94e67150c97dbcbf52549d50a7b80814841dbf"}) # this is PostHog's API key
# Explicitly excluding all the API tokens associated with the top 10 customers; we'll get to them soon, but don't want to rollout to them just yet
EXCLUDED_HASHES = set(
Expand Down
121 changes: 55 additions & 66 deletions posthog/test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,9 @@ def test_capture_exception_logs_when_enabled(self):
self.assertEqual(logs.output[0], "ERROR:posthog:test exception\nNoneType: None")
self.assertEqual(getattr(logs.records[0], "path"), "one/two/three")

@mock.patch("posthog.client.decide")
def test_basic_capture_with_feature_flags(self, patch_decide):
patch_decide.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
@mock.patch("posthog.client.flags")
def test_basic_capture_with_feature_flags(self, patch_flags):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}

client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, personal_api_key=FAKE_TEST_API_KEY)
success, msg = client.capture("distinct_id", "python test event", send_feature_flags=True)
Expand All @@ -290,11 +290,11 @@ def test_basic_capture_with_feature_flags(self, patch_decide):
self.assertEqual(msg["properties"]["$feature/beta-feature"], "random-variant")
self.assertEqual(msg["properties"]["$active_feature_flags"], ["beta-feature"])

self.assertEqual(patch_decide.call_count, 1)
self.assertEqual(patch_flags.call_count, 1)

@mock.patch("posthog.client.decide")
def test_basic_capture_with_locally_evaluated_feature_flags(self, patch_decide):
patch_decide.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
@mock.patch("posthog.client.flags")
def test_basic_capture_with_locally_evaluated_feature_flags(self, patch_flags):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, personal_api_key=FAKE_TEST_API_KEY)

multivariate_flag = {
Expand Down Expand Up @@ -380,7 +380,7 @@ def test_basic_capture_with_locally_evaluated_feature_flags(self, patch_decide):
self.assertEqual(msg["properties"]["$active_feature_flags"], ["beta-feature-local"])
assert "$feature/beta-feature" not in msg["properties"]

self.assertEqual(patch_decide.call_count, 0)
self.assertEqual(patch_flags.call_count, 0)

# test that flags are not evaluated without local evaluation
client.feature_flags = []
Expand Down Expand Up @@ -412,9 +412,9 @@ def test_load_feature_flags_quota_limited(self, patch_get):
self.assertEqual(client.cohorts, {})
self.assertIn("PostHog feature flags quota limited", logs.output[0])

@mock.patch("posthog.client.decide")
def test_dont_override_capture_with_local_flags(self, patch_decide):
patch_decide.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
@mock.patch("posthog.client.flags")
def test_dont_override_capture_with_local_flags(self, patch_flags):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, personal_api_key=FAKE_TEST_API_KEY)

multivariate_flag = {
Expand Down Expand Up @@ -487,11 +487,11 @@ def test_dont_override_capture_with_local_flags(self, patch_decide):
assert "$feature/beta-feature" not in msg["properties"]
assert "$feature/person-flag" not in msg["properties"]

self.assertEqual(patch_decide.call_count, 0)
self.assertEqual(patch_flags.call_count, 0)

@mock.patch("posthog.client.decide")
def test_basic_capture_with_feature_flags_returns_active_only(self, patch_decide):
patch_decide.return_value = {
@mock.patch("posthog.client.flags")
def test_basic_capture_with_feature_flags_returns_active_only(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False}
}

Expand All @@ -512,8 +512,8 @@ def test_basic_capture_with_feature_flags_returns_active_only(self, patch_decide
self.assertEqual(msg["properties"]["$feature/alpha-feature"], True)
self.assertEqual(msg["properties"]["$active_feature_flags"], ["beta-feature", "alpha-feature"])

self.assertEqual(patch_decide.call_count, 1)
patch_decide.assert_called_with(
self.assertEqual(patch_flags.call_count, 1)
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
timeout=3,
Expand All @@ -524,9 +524,9 @@ def test_basic_capture_with_feature_flags_returns_active_only(self, patch_decide
disable_geoip=True,
)

@mock.patch("posthog.client.decide")
def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(self, patch_decide):
patch_decide.return_value = {
@mock.patch("posthog.client.flags")
def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False}
}

Expand Down Expand Up @@ -554,8 +554,8 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
self.assertEqual(msg["properties"]["$feature/alpha-feature"], True)
self.assertEqual(msg["properties"]["$active_feature_flags"], ["beta-feature", "alpha-feature"])

self.assertEqual(patch_decide.call_count, 1)
patch_decide.assert_called_with(
self.assertEqual(patch_flags.call_count, 1)
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
timeout=12,
Expand All @@ -566,9 +566,9 @@ def test_basic_capture_with_feature_flags_and_disable_geoip_returns_correctly(se
disable_geoip=False,
)

@mock.patch("posthog.client.decide")
def test_basic_capture_with_feature_flags_switched_off_doesnt_send_them(self, patch_decide):
patch_decide.return_value = {"featureFlags": {"beta-feature": "random-variant"}}
@mock.patch("posthog.client.flags")
def test_basic_capture_with_feature_flags_switched_off_doesnt_send_them(self, patch_flags):
patch_flags.return_value = {"featureFlags": {"beta-feature": "random-variant"}}

client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, personal_api_key=FAKE_TEST_API_KEY)
success, msg = client.capture("distinct_id", "python test event", send_feature_flags=False)
Expand All @@ -585,7 +585,7 @@ def test_basic_capture_with_feature_flags_switched_off_doesnt_send_them(self, pa
self.assertTrue("$feature/beta-feature" not in msg["properties"])
self.assertTrue("$active_feature_flags" not in msg["properties"])

self.assertEqual(patch_decide.call_count, 0)
self.assertEqual(patch_flags.call_count, 0)

def test_stringifies_distinct_id(self):
# A large number that loses precision in node:
Expand Down Expand Up @@ -942,29 +942,29 @@ def test_disabled(self):

self.assertEqual(msg, "disabled")

@mock.patch("posthog.client.decide")
def test_disabled_with_feature_flags(self, patch_decide):
@mock.patch("posthog.client.flags")
def test_disabled_with_feature_flags(self, patch_flags):
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disabled=True)

response = client.get_feature_flag("beta-feature", "12345")
self.assertIsNone(response)
patch_decide.assert_not_called()
patch_flags.assert_not_called()

response = client.feature_enabled("beta-feature", "12345")
self.assertIsNone(response)
patch_decide.assert_not_called()
patch_flags.assert_not_called()

response = client.get_all_flags("12345")
self.assertIsNone(response)
patch_decide.assert_not_called()
patch_flags.assert_not_called()

response = client.get_feature_flag_payload("key", "12345")
self.assertIsNone(response)
patch_decide.assert_not_called()
patch_flags.assert_not_called()

response = client.get_all_flags_and_payloads("12345")
self.assertEqual(response, {"featureFlags": None, "featureFlagPayloads": None})
patch_decide.assert_not_called()
patch_flags.assert_not_called()

# no capture calls
self.assertTrue(client.queue.empty())
Expand Down Expand Up @@ -1012,14 +1012,14 @@ def test_disable_geoip_method_overrides_init_on_events(self):
client.flush()
self.assertTrue("$geoip_disable" not in msg["properties"])

@mock.patch("posthog.client.decide")
def test_disable_geoip_default_on_decide(self, patch_decide):
patch_decide.return_value = {
@mock.patch("posthog.client.flags")
def test_disable_geoip_default_on_decide(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False}
}
client = Client(FAKE_TEST_API_KEY, on_error=self.set_fail, disable_geoip=False)
client.get_feature_flag("random_key", "some_id", disable_geoip=True)
patch_decide.assert_called_with(
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
timeout=3,
Expand All @@ -1029,9 +1029,9 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
group_properties={},
disable_geoip=True,
)
patch_decide.reset_mock()
patch_flags.reset_mock()
client.feature_enabled("random_key", "feature_enabled_distinct_id", disable_geoip=True)
patch_decide.assert_called_with(
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
timeout=3,
Expand All @@ -1041,9 +1041,9 @@ def test_disable_geoip_default_on_decide(self, patch_decide):
group_properties={},
disable_geoip=True,
)
patch_decide.reset_mock()
patch_flags.reset_mock()
client.get_all_flags_and_payloads("all_flags_payloads_id")
patch_decide.assert_called_with(
patch_flags.assert_called_with(
"random_key",
"https://us.i.posthog.com",
timeout=3,
Expand All @@ -1066,9 +1066,9 @@ def raise_effect():

self.assertFalse(client.feature_enabled("example", "distinct_id"))

@mock.patch("posthog.client.decide")
def test_default_properties_get_added_properly(self, patch_decide):
patch_decide.return_value = {
@mock.patch("posthog.client.flags")
def test_default_properties_get_added_properly(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False}
}
client = Client(FAKE_TEST_API_KEY, host="http://app2.posthog.com", on_error=self.set_fail, disable_geoip=False)
Expand All @@ -1079,7 +1079,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
person_properties={"x1": "y1"},
group_properties={"company": {"x": "y"}},
)
patch_decide.assert_called_with(
patch_flags.assert_called_with(
"random_key",
"http://app2.posthog.com",
timeout=3,
Expand All @@ -1093,7 +1093,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
disable_geoip=False,
)

patch_decide.reset_mock()
patch_flags.reset_mock()
client.get_feature_flag(
"random_key",
"some_id",
Expand All @@ -1105,7 +1105,7 @@ def test_default_properties_get_added_properly(self, patch_decide):
}
},
)
patch_decide.assert_called_with(
patch_flags.assert_called_with(
"random_key",
"http://app2.posthog.com",
timeout=3,
Expand All @@ -1119,10 +1119,10 @@ def test_default_properties_get_added_properly(self, patch_decide):
disable_geoip=False,
)

patch_decide.reset_mock()
patch_flags.reset_mock()
# test nones
client.get_all_flags_and_payloads("some_id", groups={}, person_properties=None, group_properties=None)
patch_decide.assert_called_with(
patch_flags.assert_called_with(
"random_key",
"http://app2.posthog.com",
timeout=3,
Expand Down Expand Up @@ -1222,9 +1222,9 @@ def test_mock_system_context(

assert context == expected_context

@mock.patch("posthog.client.decide")
def test_get_decide_returns_normalized_decide_response(self, patch_decide):
patch_decide.return_value = {
@mock.patch("posthog.client.flags")
def test_get_decide_returns_normalized_decide_response(self, patch_flags):
patch_flags.return_value = {
"featureFlags": {"beta-feature": "random-variant", "alpha-feature": True, "off-feature": False},
"featureFlagPayloads": {"beta-feature": '{"some": "data"}'},
"errorsWhileComputingFlags": False,
Expand Down Expand Up @@ -1272,9 +1272,9 @@ def test_get_decide_returns_normalized_decide_response(self, patch_decide):
"requestId": "test-id",
}

@mock.patch("posthog.client.flags")
@mock.patch("posthog.client.decide")
def test_get_flags_decision_rollout(self, patch_decide, patch_flags):
@mock.patch("posthog.client.flags")
def test_get_flags_decision_rollout(self, patch_flags, patch_decide):
# Set up mock responses
decide_response = {
"featureFlags": {"flag1": True},
Expand All @@ -1291,22 +1291,11 @@ def test_get_flags_decision_rollout(self, patch_decide, patch_flags):

client = Client(FAKE_TEST_API_KEY)

# Test 0% rollout - should use decide
with mock.patch("posthog.client.is_token_in_rollout", return_value=False) as mock_rollout:
client.get_flags_decision("distinct_id")
mock_rollout.assert_called_with(
FAKE_TEST_API_KEY, 0.1, included_hashes=INCLUDED_HASHES, excluded_hashes=EXCLUDED_HASHES
)
patch_decide.assert_called_once()
patch_flags.assert_not_called()
patch_decide.reset_mock()
patch_flags.reset_mock()

# Test 100% rollout - should use flags
with mock.patch("posthog.client.is_token_in_rollout", return_value=True) as mock_rollout:
client.get_flags_decision("distinct_id")
mock_rollout.assert_called_with(
FAKE_TEST_API_KEY, 0.1, included_hashes=INCLUDED_HASHES, excluded_hashes=EXCLUDED_HASHES
FAKE_TEST_API_KEY, 1, included_hashes=INCLUDED_HASHES, excluded_hashes=EXCLUDED_HASHES
)
patch_flags.assert_called_once()
patch_decide.assert_not_called()
Expand Down
Loading