Skip to content

Commit 80427c6

Browse files
committed
dont do everything in this pr
1 parent 786bb9e commit 80427c6

File tree

1 file changed

+56
-52
lines changed

1 file changed

+56
-52
lines changed

tests/tracing/test_sampling.py

Lines changed: 56 additions & 52 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, capture_exception
8+
from sentry_sdk import start_span, start_transaction, 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_span(name="hi") as transaction:
16+
with start_transaction(name="hi") as transaction:
1717
assert transaction.sampled is not None
1818

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

2525

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

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
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
3537

3638

3739
def test_no_double_sampling(sentry_init, capture_events):
@@ -40,19 +42,19 @@ def test_no_double_sampling(sentry_init, capture_events):
4042
sentry_init(traces_sample_rate=1.0, sample_rate=0.0)
4143
events = capture_events()
4244

43-
with start_span(name="/"):
45+
with start_transaction(name="/"):
4446
pass
4547

4648
assert len(events) == 1
4749

4850

4951
@pytest.mark.parametrize("sampling_decision", [True, False])
50-
def test_get_span_from_scope_regardless_of_sampling_decision(
52+
def test_get_transaction_and_span_from_scope_regardless_of_sampling_decision(
5153
sentry_init, sampling_decision
5254
):
5355
sentry_init(traces_sample_rate=1.0)
5456

55-
with start_span(name="/", sampled=sampling_decision):
57+
with start_transaction(name="/", sampled=sampling_decision):
5658
with start_span(op="child-span"):
5759
with start_span(op="child-child-span"):
5860
scope = sentry_sdk.get_current_scope()
@@ -72,8 +74,8 @@ def test_uses_traces_sample_rate_correctly(
7274
sentry_init(traces_sample_rate=traces_sample_rate)
7375

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

7880

7981
@pytest.mark.parametrize(
@@ -88,8 +90,8 @@ def test_uses_traces_sampler_return_value_correctly(
8890
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
8991

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

9496

9597
@pytest.mark.parametrize("traces_sampler_return_value", [True, False])
@@ -98,19 +100,19 @@ def test_tolerates_traces_sampler_returning_a_boolean(
98100
):
99101
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
100102

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

104106

105107
@pytest.mark.parametrize("sampling_decision", [True, False])
106-
def test_only_captures_span_when_sampled_is_true(
108+
def test_only_captures_transaction_when_sampled_is_true(
107109
sentry_init, sampling_decision, capture_events
108110
):
109111
sentry_init(traces_sampler=mock.Mock(return_value=sampling_decision))
110112
events = capture_events()
111113

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

115117
assert len(events) == (1 if sampling_decision else 0)
116118

@@ -131,9 +133,9 @@ def test_prefers_traces_sampler_to_traces_sample_rate(
131133
traces_sampler=traces_sampler,
132134
)
133135

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

138140

139141
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
@@ -145,8 +147,10 @@ def test_ignores_inherited_sample_decision_when_traces_sampler_defined(
145147
traces_sampler = mock.Mock(return_value=not parent_sampling_decision)
146148
sentry_init(traces_sampler=traces_sampler)
147149

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

151155

152156
@pytest.mark.parametrize("explicit_decision", [True, False])
@@ -158,8 +162,8 @@ def test_traces_sampler_doesnt_overwrite_explicitly_passed_sampling_decision(
158162
traces_sampler = mock.Mock(return_value=not explicit_decision)
159163
sentry_init(traces_sampler=traces_sampler)
160164

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

164168

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

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

179185

180186
@pytest.mark.parametrize("parent_sampling_decision", [True, False])
@@ -189,13 +195,11 @@ def test_passes_parent_sampling_decision_in_sampling_context(
189195
)
190196
)
191197

192-
# XXX
193-
194198
transaction = Transaction.continue_from_headers(
195199
headers={"sentry-trace": sentry_trace_header}, name="dogpark"
196200
)
197201
spy = mock.Mock(wraps=transaction)
198-
start_span(span=spy)
202+
start_transaction(transaction=spy)
199203

200204
# there's only one call (so index at 0) and kwargs are always last in a call
201205
# tuple (so index at -1)
@@ -207,6 +211,19 @@ def test_passes_parent_sampling_decision_in_sampling_context(
207211
assert sampling_context["parent_sampled"]._mock_wraps is parent_sampling_decision
208212

209213

214+
def test_passes_attributes_from_start_span_to_traces_sampler(
215+
sentry_init, DictionaryContaining # noqa: N803
216+
):
217+
traces_sampler = mock.Mock()
218+
sentry_init(traces_sampler=traces_sampler)
219+
220+
start_transaction(attributes={"dogs": "yes", "cats": "maybe"})
221+
222+
traces_sampler.assert_any_call(
223+
DictionaryContaining({"dogs": "yes", "cats": "maybe"})
224+
)
225+
226+
210227
def test_sample_rate_affects_errors(sentry_init, capture_events):
211228
sentry_init(sample_rate=0)
212229
events = capture_events()
@@ -219,19 +236,6 @@ def test_sample_rate_affects_errors(sentry_init, capture_events):
219236
assert len(events) == 0
220237

221238

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-
235239
@pytest.mark.parametrize(
236240
"traces_sampler_return_value",
237241
[
@@ -252,9 +256,9 @@ def test_warns_and_sets_sampled_to_false_on_invalid_traces_sampler_return_value(
252256
sentry_init(traces_sampler=mock.Mock(return_value=traces_sampler_return_value))
253257

254258
with mock.patch.object(logger, "warning", mock.Mock()):
255-
span = start_span(name="dogpark")
259+
transaction = start_transaction(name="dogpark")
256260
logger.warning.assert_any_call(StringContaining("Given sample rate is invalid"))
257-
assert span.sampled is False
261+
assert transaction.sampled is False
258262

259263

260264
@pytest.mark.parametrize(
@@ -279,9 +283,9 @@ def test_records_lost_event_only_if_traces_sample_rate_enabled(
279283
sentry_init(traces_sample_rate=traces_sample_rate)
280284
record_lost_event_calls = capture_record_lost_event_calls()
281285

282-
span = start_span(name="dogpark")
283-
assert span.sampled is sampled_output
284-
span.finish()
286+
transaction = start_transaction(name="dogpark")
287+
assert transaction.sampled is sampled_output
288+
transaction.finish()
285289

286290
# Use Counter because order of calls does not matter
287291
assert Counter(record_lost_event_calls) == Counter(expected_record_lost_event_calls)
@@ -309,9 +313,9 @@ def test_records_lost_event_only_if_traces_sampler_enabled(
309313
sentry_init(traces_sampler=traces_sampler)
310314
record_lost_event_calls = capture_record_lost_event_calls()
311315

312-
span = start_span(name="dogpark")
313-
assert span.sampled is sampled_output
314-
span.finish()
316+
transaction = start_transaction(name="dogpark")
317+
assert transaction.sampled is sampled_output
318+
transaction.finish()
315319

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

0 commit comments

Comments
 (0)