|
| 1 | +# coding=utf-8 |
| 2 | + |
| 3 | +from unittest import mock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from scout_apm.core.config import ScoutConfig |
| 8 | +from scout_apm.core.sampler import Sampler |
| 9 | + |
| 10 | + |
| 11 | +@pytest.fixture |
| 12 | +def config(): |
| 13 | + config = ScoutConfig() |
| 14 | + ScoutConfig.set( |
| 15 | + sample_rate=50, # 50% global sampling |
| 16 | + sample_endpoints={ |
| 17 | + "users/test": 0, # Never sample specific endpoint |
| 18 | + "users": 100, # Always sample |
| 19 | + "test": 20, # 20% sampling for test endpoints |
| 20 | + "health": 0, # Never sample health checks |
| 21 | + }, |
| 22 | + sample_jobs={ |
| 23 | + "critical-job": 100, # Always sample |
| 24 | + "batch": 30, # 30% sampling for batch jobs |
| 25 | + }, |
| 26 | + ignore_endpoints=["metrics", "ping"], |
| 27 | + ignore_jobs=["test-job"], |
| 28 | + endpoint_sample_rate=70, # 70% sampling for unspecified endpoints |
| 29 | + job_sample_rate=40, # 40% sampling for unspecified jobs |
| 30 | + ) |
| 31 | + yield config |
| 32 | + ScoutConfig.reset_all() |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def sampler(config): |
| 37 | + return Sampler(config) |
| 38 | + |
| 39 | + |
| 40 | +def test_should_sample_endpoint_always(sampler): |
| 41 | + assert sampler.should_sample("Controller/users", False) is True |
| 42 | + |
| 43 | + |
| 44 | +def test_should_sample_endpoint_never(sampler): |
| 45 | + assert sampler.should_sample("Controller/health/check", False) is False |
| 46 | + assert sampler.should_sample("Controller/users/test", False) is False |
| 47 | + |
| 48 | + |
| 49 | +def test_should_sample_endpoint_ignored(sampler): |
| 50 | + assert sampler.should_sample("Controller/metrics/some/more", False) is False |
| 51 | + |
| 52 | + |
| 53 | +def test_should_sample_endpoint_partial(sampler): |
| 54 | + with mock.patch("random.randint", return_value=10): |
| 55 | + assert sampler.should_sample("Controller/test/endpoint", False) is True |
| 56 | + with mock.patch("random.randint", return_value=30): |
| 57 | + assert sampler.should_sample("Controller/test/endpoint", False) is False |
| 58 | + |
| 59 | + |
| 60 | +def test_should_sample_job_always(sampler): |
| 61 | + assert sampler.should_sample("Job/critical-job", False) is True |
| 62 | + |
| 63 | + |
| 64 | +def test_should_sample_job_never(sampler): |
| 65 | + assert sampler.should_sample("Job/test-job", False) is False |
| 66 | + |
| 67 | + |
| 68 | +def test_should_sample_job_partial(sampler): |
| 69 | + with mock.patch("random.randint", return_value=10): |
| 70 | + assert sampler.should_sample("Job/batch-process", False) is True |
| 71 | + with mock.patch("random.randint", return_value=40): |
| 72 | + assert sampler.should_sample("Job/batch-process", False) is False |
| 73 | + |
| 74 | + |
| 75 | +def test_should_sample_unknown_operation(sampler): |
| 76 | + with mock.patch("random.randint", return_value=10): |
| 77 | + assert sampler.should_sample("Unknown/operation", False) is True |
| 78 | + with mock.patch("random.randint", return_value=60): |
| 79 | + assert sampler.should_sample("Unknown/operation", False) is False |
| 80 | + |
| 81 | + |
| 82 | +def test_should_sample_no_sampling_enabled(config): |
| 83 | + config.set( |
| 84 | + sample_rate=100, # Return config to defaults |
| 85 | + sample_endpoints={}, |
| 86 | + sample_jobs={}, |
| 87 | + ignore_endpoints=[], |
| 88 | + ignore_jobs=[], |
| 89 | + endpoint_sample_rate=None, |
| 90 | + job_sample_rate=None, |
| 91 | + ) |
| 92 | + sampler = Sampler(config) |
| 93 | + assert sampler.should_sample("Controller/any_endpoint", False) is True |
| 94 | + assert sampler.should_sample("Job/any_job", False) is True |
| 95 | + |
| 96 | + |
| 97 | +def test_should_sample_endpoint_default_rate(sampler): |
| 98 | + with mock.patch("random.randint", return_value=60): |
| 99 | + assert sampler.should_sample("Controller/unspecified", False) is True |
| 100 | + with mock.patch("random.randint", return_value=80): |
| 101 | + assert sampler.should_sample("Controller/unspecified", False) is False |
| 102 | + |
| 103 | + |
| 104 | +def test_should_sample_job_default_rate(sampler): |
| 105 | + with mock.patch("random.randint", return_value=30): |
| 106 | + assert sampler.should_sample("Job/unspecified-job", False) is True |
| 107 | + with mock.patch("random.randint", return_value=50): |
| 108 | + assert sampler.should_sample("Job/unspecified-job", False) is False |
| 109 | + |
| 110 | + |
| 111 | +def test_should_sample_endpoint_fallback_to_global_rate(config): |
| 112 | + config.set(endpoint_sample_rate=None) |
| 113 | + sampler = Sampler(config) |
| 114 | + with mock.patch("random.randint", return_value=40): |
| 115 | + assert sampler.should_sample("Controller/unspecified", False) is True |
| 116 | + with mock.patch("random.randint", return_value=60): |
| 117 | + assert sampler.should_sample("Controller/unspecified", False) is False |
| 118 | + |
| 119 | + |
| 120 | +def test_should_sample_job_fallback_to_global_rate(config): |
| 121 | + config.set(job_sample_rate=None) |
| 122 | + sampler = Sampler(config) |
| 123 | + with mock.patch("random.randint", return_value=40): |
| 124 | + assert sampler.should_sample("Job/unspecified-job", False) is True |
| 125 | + with mock.patch("random.randint", return_value=60): |
| 126 | + assert sampler.should_sample("Job/unspecified-job", False) is False |
| 127 | + |
| 128 | + |
| 129 | +def test_should_handle_legacy_ignore_with_specific_sampling(config): |
| 130 | + """Test that specific sampling rates override legacy ignore patterns.""" |
| 131 | + config.set( |
| 132 | + sample_endpoints={ |
| 133 | + "foo/bar": 50, # Should override the ignore pattern for specific endpoint |
| 134 | + "foo": 0, # Ignore all other foo endpoints |
| 135 | + }, |
| 136 | + ) |
| 137 | + sampler = Sampler(config) |
| 138 | + |
| 139 | + # foo/bar should be sampled at 50% |
| 140 | + with mock.patch("random.randint", return_value=40): |
| 141 | + assert sampler.should_sample("Controller/foo/bar", False) is True |
| 142 | + with mock.patch("random.randint", return_value=60): |
| 143 | + assert sampler.should_sample("Controller/foo/bar", False) is False |
| 144 | + |
| 145 | + # foo/other should be ignored (0% sampling) |
| 146 | + assert sampler.should_sample("Controller/foo/other", False) is False |
| 147 | + |
| 148 | + |
| 149 | +def test_prefix_matching_precedence(config): |
| 150 | + """Test that longer prefix matches take precedence.""" |
| 151 | + config.set( |
| 152 | + sample_endpoints={ |
| 153 | + "api/users/vip": 100, # Sample all VIP user endpoints |
| 154 | + "api/users": 50, # Sample 50% of user endpoints |
| 155 | + "api": 0, # Ignore all API endpoints by default |
| 156 | + } |
| 157 | + ) |
| 158 | + sampler = Sampler(config) |
| 159 | + |
| 160 | + # Regular API endpoint should be ignored |
| 161 | + assert sampler.should_sample("Controller/api/status", False) is False |
| 162 | + |
| 163 | + # Users API should be sampled at 50% |
| 164 | + with mock.patch("random.randint", return_value=40): |
| 165 | + assert sampler.should_sample("Controller/api/users/list", False) is True |
| 166 | + with mock.patch("random.randint", return_value=60): |
| 167 | + assert sampler.should_sample("Controller/api/users/list", False) is False |
| 168 | + |
| 169 | + # VIP users API should always be sampled |
| 170 | + assert sampler.should_sample("Controller/api/users/vip/list", False) is True |
0 commit comments