Skip to content

Commit 786bb9e

Browse files
committed
.
1 parent 1aeeb68 commit 786bb9e

File tree

1 file changed

+52
-43
lines changed

1 file changed

+52
-43
lines changed

tests/tracing/test_sampling.py

Lines changed: 52 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
import pytest
66

77
import sentry_sdk
8-
from sentry_sdk import start_span, start_transaction, capture_exception
8+
from sentry_sdk import start_span, capture_exception
99
from sentry_sdk.tracing import Transaction
1010
from sentry_sdk.utils import logger
1111

1212

1313
def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
1414
sentry_init(traces_sample_rate=0.5)
1515

16-
with start_transaction(name="hi") as transaction:
16+
with start_span(name="hi") as transaction:
1717
assert transaction.sampled is not None
1818

1919
with start_span() as span:
@@ -24,16 +24,14 @@ def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
2424

2525

2626
@pytest.mark.parametrize("sampled", [True, False])
27-
def test_nested_transaction_sampling_override(sentry_init, sampled):
27+
def test_nested_span_sampling_override(sentry_init, sampled):
2828
sentry_init(traces_sample_rate=1.0)
2929

30-
with start_transaction(name="outer", sampled=sampled) as outer_transaction:
31-
assert outer_transaction.sampled is sampled
32-
with start_transaction(
33-
name="inner", sampled=(not sampled)
34-
) as inner_transaction:
35-
assert inner_transaction.sampled is not sampled
36-
assert outer_transaction.sampled is sampled
30+
with start_span(name="outer", sampled=sampled) as outer_span:
31+
assert outer_span.sampled is sampled
32+
with start_span(name="inner", sampled=(not sampled)) as inner_span:
33+
assert inner_span.sampled is not sampled
34+
assert outer_span.sampled is sampled
3735

3836

3937
def test_no_double_sampling(sentry_init, capture_events):
@@ -42,19 +40,19 @@ def test_no_double_sampling(sentry_init, capture_events):
4240
sentry_init(traces_sample_rate=1.0, sample_rate=0.0)
4341
events = capture_events()
4442

45-
with start_transaction(name="/"):
43+
with start_span(name="/"):
4644
pass
4745

4846
assert len(events) == 1
4947

5048

5149
@pytest.mark.parametrize("sampling_decision", [True, False])
52-
def test_get_transaction_and_span_from_scope_regardless_of_sampling_decision(
50+
def test_get_span_from_scope_regardless_of_sampling_decision(
5351
sentry_init, sampling_decision
5452
):
5553
sentry_init(traces_sample_rate=1.0)
5654

57-
with start_transaction(name="/", sampled=sampling_decision):
55+
with start_span(name="/", sampled=sampling_decision):
5856
with start_span(op="child-span"):
5957
with start_span(op="child-child-span"):
6058
scope = sentry_sdk.get_current_scope()
@@ -74,8 +72,8 @@ def test_uses_traces_sample_rate_correctly(
7472
sentry_init(traces_sample_rate=traces_sample_rate)
7573

7674
with mock.patch.object(random, "random", return_value=0.5):
77-
transaction = start_transaction(name="dogpark")
78-
assert transaction.sampled is expected_decision
75+
span = start_span(name="dogpark")
76+
assert span.sampled is expected_decision
7977

8078

8179
@pytest.mark.parametrize(
@@ -90,8 +88,8 @@ def test_uses_traces_sampler_return_value_correctly(
9088
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
9189

9290
with mock.patch.object(random, "random", return_value=0.5):
93-
transaction = start_transaction(name="dogpark")
94-
assert transaction.sampled is expected_decision
91+
span = start_span(name="dogpark")
92+
assert span.sampled is expected_decision
9593

9694

9795
@pytest.mark.parametrize("traces_sampler_return_value", [True, False])
@@ -100,19 +98,19 @@ def test_tolerates_traces_sampler_returning_a_boolean(
10098
):
10199
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
102100

103-
transaction = start_transaction(name="dogpark")
104-
assert transaction.sampled is traces_sampler_return_value
101+
span = start_span(name="dogpark")
102+
assert span.sampled is traces_sampler_return_value
105103

106104

107105
@pytest.mark.parametrize("sampling_decision", [True, False])
108-
def test_only_captures_transaction_when_sampled_is_true(
106+
def test_only_captures_span_when_sampled_is_true(
109107
sentry_init, sampling_decision, capture_events
110108
):
111109
sentry_init(traces_sampler=mock.Mock(return_value=sampling_decision))
112110
events = capture_events()
113111

114-
transaction = start_transaction(name="dogpark")
115-
transaction.finish()
112+
span = start_span(name="dogpark")
113+
span.finish()
116114

117115
assert len(events) == (1 if sampling_decision else 0)
118116

@@ -133,9 +131,9 @@ def test_prefers_traces_sampler_to_traces_sample_rate(
133131
traces_sampler=traces_sampler,
134132
)
135133

136-
transaction = start_transaction(name="dogpark")
134+
span = start_span(name="dogpark")
137135
assert traces_sampler.called is True
138-
assert transaction.sampled is traces_sampler_return_value
136+
assert span.sampled is traces_sampler_return_value
139137

140138

141139
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
@@ -147,10 +145,8 @@ def test_ignores_inherited_sample_decision_when_traces_sampler_defined(
147145
traces_sampler = mock.Mock(return_value=not parent_sampling_decision)
148146
sentry_init(traces_sampler=traces_sampler)
149147

150-
transaction = start_transaction(
151-
name="dogpark", parent_sampled=parent_sampling_decision
152-
)
153-
assert transaction.sampled is not parent_sampling_decision
148+
span = start_span(name="dogpark", parent_sampled=parent_sampling_decision)
149+
assert span.sampled is not parent_sampling_decision
154150

155151

156152
@pytest.mark.parametrize("explicit_decision", [True, False])
@@ -162,8 +158,8 @@ def test_traces_sampler_doesnt_overwrite_explicitly_passed_sampling_decision(
162158
traces_sampler = mock.Mock(return_value=not explicit_decision)
163159
sentry_init(traces_sampler=traces_sampler)
164160

165-
transaction = start_transaction(name="dogpark", sampled=explicit_decision)
166-
assert transaction.sampled is explicit_decision
161+
span = start_span(name="dogpark", sampled=explicit_decision)
162+
assert span.sampled is explicit_decision
167163

168164

169165
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
@@ -177,10 +173,8 @@ def test_inherits_parent_sampling_decision_when_traces_sampler_undefined(
177173
mock_random_value = 0.25 if parent_sampling_decision is False else 0.75
178174

179175
with mock.patch.object(random, "random", return_value=mock_random_value):
180-
transaction = start_transaction(
181-
name="dogpark", parent_sampled=parent_sampling_decision
182-
)
183-
assert transaction.sampled is parent_sampling_decision
176+
span = start_span(name="dogpark", parent_sampled=parent_sampling_decision)
177+
assert span.sampled is parent_sampling_decision
184178

185179

186180
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
@@ -195,11 +189,13 @@ def test_passes_parent_sampling_decision_in_sampling_context(
195189
)
196190
)
197191

192+
# XXX
193+
198194
transaction = Transaction.continue_from_headers(
199195
headers={"sentry-trace": sentry_trace_header}, name="dogpark"
200196
)
201197
spy = mock.Mock(wraps=transaction)
202-
start_transaction(transaction=spy)
198+
start_span(span=spy)
203199

204200
# there's only one call (so index at 0) and kwargs are always last in a call
205201
# tuple (so index at -1)
@@ -223,6 +219,19 @@ def test_sample_rate_affects_errors(sentry_init, capture_events):
223219
assert len(events) == 0
224220

225221

222+
def test_passes_custom_attributes_from_start_span_to_traces_sampler(
223+
sentry_init, DictionaryContaining # noqa: N803
224+
):
225+
traces_sampler = mock.Mock()
226+
sentry_init(traces_sampler=traces_sampler)
227+
228+
start_span(attributes={"dogs": "yes", "cats": "maybe"})
229+
230+
traces_sampler.assert_any_call(
231+
DictionaryContaining({"dogs": "yes", "cats": "maybe"})
232+
)
233+
234+
226235
@pytest.mark.parametrize(
227236
"traces_sampler_return_value",
228237
[
@@ -243,9 +252,9 @@ def test_warns_and_sets_sampled_to_false_on_invalid_traces_sampler_return_value(
243252
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
244253

245254
with mock.patch.object(logger, "warning", mock.Mock()):
246-
transaction = start_transaction(name="dogpark")
255+
span = start_span(name="dogpark")
247256
logger.warning.assert_any_call(StringContaining("Given sample rate is invalid"))
248-
assert transaction.sampled is False
257+
assert span.sampled is False
249258

250259

251260
@pytest.mark.parametrize(
@@ -270,9 +279,9 @@ def test_records_lost_event_only_if_traces_sample_rate_enabled(
270279
sentry_init(traces_sample_rate=traces_sample_rate)
271280
record_lost_event_calls = capture_record_lost_event_calls()
272281

273-
transaction = start_transaction(name="dogpark")
274-
assert transaction.sampled is sampled_output
275-
transaction.finish()
282+
span = start_span(name="dogpark")
283+
assert span.sampled is sampled_output
284+
span.finish()
276285

277286
# Use Counter because order of calls does not matter
278287
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)
@@ -300,9 +309,9 @@ def test_records_lost_event_only_if_traces_sampler_enabled(
300309
sentry_init(traces_sampler=traces_sampler)
301310
record_lost_event_calls = capture_record_lost_event_calls()
302311

303-
transaction = start_transaction(name="dogpark")
304-
assert transaction.sampled is sampled_output
305-
transaction.finish()
312+
span = start_span(name="dogpark")
313+
assert span.sampled is sampled_output
314+
span.finish()
306315

307316
# Use Counter because order of calls does not matter
308317
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)

0 commit comments

Comments
 (0)