/
test_integration_tests.py
138 lines (106 loc) · 3.96 KB
/
test_integration_tests.py
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
import weakref
import gc
import pytest
from sentry_sdk import (
capture_message,
configure_scope,
Hub,
start_span,
start_transaction,
)
from sentry_sdk.tracing import Transaction
@pytest.mark.parametrize("sample_rate", [0.0, 1.0])
def test_basic(sentry_init, capture_events, sample_rate):
sentry_init(traces_sample_rate=sample_rate)
events = capture_events()
with start_transaction(name="hi") as transaction:
transaction.set_status("ok")
with pytest.raises(ZeroDivisionError):
with start_span(op="foo", description="foodesc"):
1 / 0
with start_span(op="bar", description="bardesc"):
pass
if sample_rate:
assert len(events) == 1
event = events[0]
span1, span2 = event["spans"]
parent_span = event
assert span1["tags"]["status"] == "internal_error"
assert span1["op"] == "foo"
assert span1["description"] == "foodesc"
assert "status" not in span2.get("tags", {})
assert span2["op"] == "bar"
assert span2["description"] == "bardesc"
assert parent_span["transaction"] == "hi"
assert "status" not in event["tags"]
assert event["contexts"]["trace"]["status"] == "ok"
else:
assert not events
@pytest.mark.parametrize("sampled", [True, False, None])
def test_continue_from_headers(sentry_init, capture_events, sampled):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
with start_transaction(name="hi"):
with start_span() as old_span:
old_span.sampled = sampled
headers = dict(Hub.current.iter_trace_propagation_headers())
header = headers["sentry-trace"]
if sampled is True:
assert header.endswith("-1")
if sampled is False:
assert header.endswith("-0")
if sampled is None:
assert header.endswith("-")
transaction = Transaction.continue_from_headers(headers, name="WRONG")
assert transaction is not None
assert transaction.sampled == sampled
assert transaction.trace_id == old_span.trace_id
assert transaction.same_process_as_parent is False
assert transaction.parent_span_id == old_span.span_id
assert transaction.span_id != old_span.span_id
with start_transaction(transaction):
with configure_scope() as scope:
scope.transaction = "ho"
capture_message("hello")
if sampled is False:
trace1, message = events
assert trace1["transaction"] == "hi"
else:
trace1, message, trace2 = events
assert trace1["transaction"] == "hi"
assert trace2["transaction"] == "ho"
assert (
trace1["contexts"]["trace"]["trace_id"]
== trace2["contexts"]["trace"]["trace_id"]
== transaction.trace_id
== message["contexts"]["trace"]["trace_id"]
)
assert message["message"] == "hello"
@pytest.mark.parametrize(
"args,expected_refcount",
[({"traces_sample_rate": 1.0}, 100), ({"traces_sample_rate": 0.0}, 0)],
)
def test_memory_usage(sentry_init, capture_events, args, expected_refcount):
sentry_init(**args)
references = weakref.WeakSet()
with start_transaction(name="hi"):
for i in range(100):
with start_span(op="helloworld", description="hi {}".format(i)) as span:
def foo():
pass
references.add(foo)
span.set_tag("foo", foo)
pass
del foo
del span
# required only for pypy (cpython frees immediately)
gc.collect()
assert len(references) == expected_refcount
def test_transactions_do_not_go_through_before_send(sentry_init, capture_events):
def before_send(event, hint):
raise RuntimeError("should not be called")
sentry_init(traces_sample_rate=1.0, before_send=before_send)
events = capture_events()
with start_transaction(name="/"):
pass
assert len(events) == 1