Skip to content

Commit 8ae99c0

Browse files
committed
fix tests
1 parent d9b7f71 commit 8ae99c0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

tests/tracing/test_sampling.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,28 @@ def test_prefers_traces_sampler_to_traces_sample_rate(
137137
assert transaction.sampled is traces_sampler_return_value
138138

139139

140+
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
141+
def test_ignores_inherited_sample_decision_when_traces_sampler_defined(
142+
sentry_init, parent_sampling_decision
143+
):
144+
# make traces_sampler pick the opposite of the inherited decision, to prove
145+
# that traces_sampler takes precedence
146+
traces_sampler = mock.Mock(return_value=not parent_sampling_decision)
147+
sentry_init(traces_sampler=traces_sampler)
148+
149+
sentry_trace_header = (
150+
"12312012123120121231201212312012-1121201211212012-{sampled}".format(
151+
sampled=int(parent_sampling_decision)
152+
)
153+
)
154+
155+
with sentry_sdk.continue_trace({"sentry-trace": sentry_trace_header}):
156+
with sentry_sdk.start_span(name="dogpark") as span:
157+
pass
158+
159+
assert span.sampled is not parent_sampling_decision
160+
161+
140162
@pytest.mark.parametrize("explicit_decision", [True, False])
141163
def test_traces_sampler_doesnt_overwrite_explicitly_passed_sampling_decision(
142164
sentry_init, explicit_decision
@@ -150,6 +172,24 @@ def test_traces_sampler_doesnt_overwrite_explicitly_passed_sampling_decision(
150172
assert transaction.sampled is explicit_decision
151173

152174

175+
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
176+
def test_inherits_parent_sampling_decision_when_traces_sampler_undefined(
177+
sentry_init, parent_sampling_decision
178+
):
179+
# make sure the parent sampling decision is the opposite of what
180+
# traces_sample_rate would produce, to prove the inheritance takes
181+
# precedence
182+
sentry_init(traces_sample_rate=0.5)
183+
mock_random_value = 0.25 if parent_sampling_decision is False else 0.75
184+
185+
with mock.patch.object(random, "random", return_value=mock_random_value):
186+
with start_span(name="catpark", sampled=parent_sampling_decision):
187+
with start_span(name="dogpark") as span:
188+
pass
189+
190+
assert span.sampled is parent_sampling_decision
191+
192+
153193
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
154194
def test_passes_parent_sampling_decision_in_sampling_context(
155195
sentry_init, parent_sampling_decision

0 commit comments

Comments
 (0)