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