Skip to content

Commit b36c548

Browse files
authored
ref(tests): Split up tracing tests (getsentry#857)
No behavior changes, just movin' stuff around.
1 parent d8c161f commit b36c548

File tree

4 files changed

+100
-106
lines changed

4 files changed

+100
-106
lines changed

tests/tracing/test_deprecated.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from sentry_sdk import start_span
2+
3+
from sentry_sdk.tracing import Span
4+
5+
6+
def test_start_span_to_start_transaction(sentry_init, capture_events):
7+
# XXX: this only exists for backwards compatibility with code before
8+
# Transaction / start_transaction were introduced.
9+
sentry_init(traces_sample_rate=1.0)
10+
events = capture_events()
11+
12+
with start_span(transaction="/1/"):
13+
pass
14+
15+
with start_span(Span(transaction="/2/")):
16+
pass
17+
18+
assert len(events) == 2
19+
assert events[0]["transaction"] == "/1/"
20+
assert events[1]["transaction"] == "/2/"

tests/test_tracing.py renamed to tests/tracing/test_integration_tests.py

+1-106
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
start_span,
1111
start_transaction,
1212
)
13-
from sentry_sdk.tracing import Span, Transaction
13+
from sentry_sdk.tracing import Transaction
1414

1515

1616
@pytest.mark.parametrize("sample_rate", [0.0, 1.0])
@@ -46,23 +46,6 @@ def test_basic(sentry_init, capture_events, sample_rate):
4646
assert not events
4747

4848

49-
def test_start_span_to_start_transaction(sentry_init, capture_events):
50-
# XXX: this only exists for backwards compatibility with code before
51-
# Transaction / start_transaction were introduced.
52-
sentry_init(traces_sample_rate=1.0)
53-
events = capture_events()
54-
55-
with start_span(transaction="/1/"):
56-
pass
57-
58-
with start_span(Span(transaction="/2/")):
59-
pass
60-
61-
assert len(events) == 2
62-
assert events[0]["transaction"] == "/1/"
63-
assert events[1]["transaction"] == "/2/"
64-
65-
6649
@pytest.mark.parametrize("sampled", [True, False, None])
6750
def test_continue_from_headers(sentry_init, capture_events, sampled):
6851
sentry_init(traces_sample_rate=1.0)
@@ -114,19 +97,6 @@ def test_continue_from_headers(sentry_init, capture_events, sampled):
11497
assert message["message"] == "hello"
11598

11699

117-
def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
118-
sentry_init(traces_sample_rate=0.5)
119-
120-
with start_transaction(name="hi") as transaction:
121-
assert transaction.sampled is not None
122-
123-
with start_span() as span:
124-
assert span.sampled == transaction.sampled
125-
126-
with start_span() as span:
127-
assert span.sampled is None
128-
129-
130100
@pytest.mark.parametrize(
131101
"args,expected_refcount",
132102
[({"traces_sample_rate": 1.0}, 100), ({"traces_sample_rate": 0.0}, 0)],
@@ -156,67 +126,6 @@ def foo():
156126
assert len(references) == expected_refcount
157127

158128

159-
def test_span_trimming(sentry_init, capture_events):
160-
sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3})
161-
events = capture_events()
162-
163-
with start_transaction(name="hi"):
164-
for i in range(10):
165-
with start_span(op="foo{}".format(i)):
166-
pass
167-
168-
(event,) = events
169-
span1, span2 = event["spans"]
170-
assert span1["op"] == "foo0"
171-
assert span2["op"] == "foo1"
172-
173-
174-
def test_nested_transaction_sampling_override():
175-
with start_transaction(name="outer", sampled=True) as outer_transaction:
176-
assert outer_transaction.sampled is True
177-
with start_transaction(name="inner", sampled=False) as inner_transaction:
178-
assert inner_transaction.sampled is False
179-
assert outer_transaction.sampled is True
180-
181-
182-
def test_transaction_method_signature(sentry_init, capture_events):
183-
sentry_init(traces_sample_rate=1.0)
184-
events = capture_events()
185-
186-
with pytest.raises(TypeError):
187-
start_span(name="foo")
188-
assert len(events) == 0
189-
190-
with start_transaction() as transaction:
191-
pass
192-
assert transaction.name == "<unlabeled transaction>"
193-
assert len(events) == 1
194-
195-
with start_transaction() as transaction:
196-
transaction.name = "name-known-after-transaction-started"
197-
assert len(events) == 2
198-
199-
with start_transaction(name="a"):
200-
pass
201-
assert len(events) == 3
202-
203-
with start_transaction(Transaction(name="c")):
204-
pass
205-
assert len(events) == 4
206-
207-
208-
def test_no_double_sampling(sentry_init, capture_events):
209-
# Transactions should not be subject to the global/error sample rate.
210-
# Only the traces_sample_rate should apply.
211-
sentry_init(traces_sample_rate=1.0, sample_rate=0.0)
212-
events = capture_events()
213-
214-
with start_transaction(name="/"):
215-
pass
216-
217-
assert len(events) == 1
218-
219-
220129
def test_transactions_do_not_go_through_before_send(sentry_init, capture_events):
221130
def before_send(event, hint):
222131
raise RuntimeError("should not be called")
@@ -228,17 +137,3 @@ def before_send(event, hint):
228137
pass
229138

230139
assert len(events) == 1
231-
232-
233-
def test_get_transaction_from_scope(sentry_init, capture_events):
234-
sentry_init(traces_sample_rate=1.0)
235-
events = capture_events()
236-
237-
with start_transaction(name="/"):
238-
with start_span(op="child-span"):
239-
with start_span(op="child-child-span"):
240-
scope = Hub.current.scope
241-
assert scope.span.op == "child-child-span"
242-
assert scope.transaction.name == "/"
243-
244-
assert len(events) == 1

tests/tracing/test_misc.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
3+
from sentry_sdk import start_span, start_transaction
4+
from sentry_sdk.tracing import Transaction
5+
6+
7+
def test_span_trimming(sentry_init, capture_events):
8+
sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3})
9+
events = capture_events()
10+
11+
with start_transaction(name="hi"):
12+
for i in range(10):
13+
with start_span(op="foo{}".format(i)):
14+
pass
15+
16+
(event,) = events
17+
span1, span2 = event["spans"]
18+
assert span1["op"] == "foo0"
19+
assert span2["op"] == "foo1"
20+
21+
22+
def test_transaction_method_signature(sentry_init, capture_events):
23+
sentry_init(traces_sample_rate=1.0)
24+
events = capture_events()
25+
26+
with pytest.raises(TypeError):
27+
start_span(name="foo")
28+
assert len(events) == 0
29+
30+
with start_transaction() as transaction:
31+
pass
32+
assert transaction.name == "<unlabeled transaction>"
33+
assert len(events) == 1
34+
35+
with start_transaction() as transaction:
36+
transaction.name = "name-known-after-transaction-started"
37+
assert len(events) == 2
38+
39+
with start_transaction(name="a"):
40+
pass
41+
assert len(events) == 3
42+
43+
with start_transaction(Transaction(name="c")):
44+
pass
45+
assert len(events) == 4

tests/tracing/test_sampling.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from sentry_sdk import start_span, start_transaction
2+
3+
4+
def test_sampling_decided_only_for_transactions(sentry_init, capture_events):
5+
sentry_init(traces_sample_rate=0.5)
6+
7+
with start_transaction(name="hi") as transaction:
8+
assert transaction.sampled is not None
9+
10+
with start_span() as span:
11+
assert span.sampled == transaction.sampled
12+
13+
with start_span() as span:
14+
assert span.sampled is None
15+
16+
17+
def test_nested_transaction_sampling_override():
18+
with start_transaction(name="outer", sampled=True) as outer_transaction:
19+
assert outer_transaction.sampled is True
20+
with start_transaction(name="inner", sampled=False) as inner_transaction:
21+
assert inner_transaction.sampled is False
22+
assert outer_transaction.sampled is True
23+
24+
25+
def test_no_double_sampling(sentry_init, capture_events):
26+
# Transactions should not be subject to the global/error sample rate.
27+
# Only the traces_sample_rate should apply.
28+
sentry_init(traces_sample_rate=1.0, sample_rate=0.0)
29+
events = capture_events()
30+
31+
with start_transaction(name="/"):
32+
pass
33+
34+
assert len(events) == 1

0 commit comments

Comments
 (0)