: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(csp): stop truncating sub-1% sampling rates to zero (#68598)
Frank Hamand63e6893fc991
diff --git a/posthog/api/test/test_csp.py b/posthog/api/test/test_csp.pyindex 8d4ac4ecd969..83332af52c75 100644--- a/posthog/api/test/test_csp.py+++ b/posthog/api/test/test_csp.py@@ -631,14 +631,14 @@ class TestCSPModule(TestCase):"""# Test specific URL+time combinations with known expected outcomes- # These were determined by calculating hash(url + "-" + timestamp) % 100+ # These were determined by calculating hash(url + "-" + timestamp) % 10000deterministic_test_cases = [- # URL, time_string, expected_result_at_50_percent, hash_mod_100- ("https://example.com/other", "2023-01-01 12:00:00", True, 8), # 8 < 50 = True- ("https://example.com/other", "2023-01-01 12:01:00", False, 99), # 99 >= 50 = False- ("https://test.com/page", "2023-01-01 12:03:00", False, 52), # 52 >= 50 = False- ("https://test.com/page", "2023-01-01 12:04:00", True, 43), # 43 < 50 = True- ("https://test.com/page", "2023-01-01 12:09:00", False, 98), # 98 >= 50 = False+ # URL, time_string, expected_result_at_50_percent, hash_mod_10000+ ("https://example.com/other", "2023-01-01 12:00:00", True, 1608), # 1608 < 5000 = True+ ("https://example.com/other", "2023-01-01 12:08:00", False, 9936), # 9936 >= 5000 = False+ ("https://example.com/other", "2023-01-01 12:09:00", False, 9727), # 9727 >= 5000 = False+ ("https://test.com/page", "2023-01-01 12:03:00", True, 2352), # 2352 < 5000 = True+ ("https://test.com/page", "2023-01-01 12:04:00", True, 2143), # 2143 < 5000 = True]for url, time_string, expected_result, expected_hash_mod in deterministic_test_cases:@@ -646,15 +646,15 @@ class TestCSPModule(TestCase):properties = {"document_url": url, "effective_directive": "script-src"}result = sample_csp_report(properties, 0.5)assert result == expected_result, (- f"Expected {expected_result} for {url} at {time_string} (hash%100={expected_hash_mod}), got {result}"+ f"Expected {expected_result} for {url} at {time_string} (hash%10000={expected_hash_mod}), got {result}")# Demonstrate the key improvement: same URL gets different sampling decisions across time- url = "https://test.com/page"+ url = "https://example.com/other"time_sampling_pairs = [- ("2023-01-01 12:03:00", False), # hash%100=52 >= 50- ("2023-01-01 12:04:00", True), # hash%100=43 < 50- ("2023-01-01 12:09:00", False), # hash%100=98 >= 50+ ("2023-01-01 12:03:00", True), # hash%10000=981 < 5000+ ("2023-01-01 12:08:00", False), # hash%10000=9936 >= 5000+ ("2023-01-01 12:09:00", False), # hash%10000=9727 >= 5000]sampled_in_results = []diff --git a/posthog/sampling.py b/posthog/sampling.pyindex 7f2ce1bf98f4..f84ef5425387 100644--- a/posthog/sampling.py+++ b/posthog/sampling.py@@ -75,4 +75,5 @@ def sample_on_property(prop: str, percent: float) -> bool:return Truepercent = clamp_to_range(percent, 0, 1, "Sampling rate")- return simple_hash(prop) % 100 < int(percent * 100)+ # 4-decimal-place resolution so sub-1% rates don't truncate to zero+ return simple_hash(prop) % 10000 < round(percent * 10000)diff --git a/posthog/test/test_sampling.py b/posthog/test/test_sampling.pyindex 3d3155631f4d..d1f15e72743d 100644--- a/posthog/test/test_sampling.py+++ b/posthog/test/test_sampling.py@@ -28,6 +28,11 @@ class TestSampling(TestCase):# The same string should have the same sampling decision at the same rateassert sample_on_property(test_string, 0.1) is result_at_10_percent+ def test_sample_on_property_sub_one_percent_rate(self):+ # sub-1% rates used to truncate to 0% and sample nothing+ sampled = sum(1 for i in range(10_000) if sample_on_property(f"example.com:script-src-{i}", 0.005))+ assert 0 < sampled < 200+def test_clamp_to_range(self):assert clamp_to_range(5, 0, 10) == 5assert clamp_to_range(-5, 0, 10) == 0