-
Notifications
You must be signed in to change notification settings - Fork 464
/
test_misc.py
265 lines (204 loc) · 8.92 KB
/
test_misc.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import pytest
import gc
import uuid
import os
import sentry_sdk
from sentry_sdk import Hub, start_span, start_transaction
from sentry_sdk.tracing import Span, Transaction
from sentry_sdk.tracing_utils import has_tracestate_enabled
try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3
def test_span_trimming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3})
events = capture_events()
with start_transaction(name="hi"):
for i in range(10):
with start_span(op="foo{}".format(i)):
pass
(event,) = events
assert len(event["spans"]) == 3
span1, span2, span3 = event["spans"]
assert span1["op"] == "foo0"
assert span2["op"] == "foo1"
assert span3["op"] == "foo2"
def test_transaction_naming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
# only transactions have names - spans don't
with pytest.raises(TypeError):
start_span(name="foo")
assert len(events) == 0
# default name in event if no name is passed
with start_transaction() as transaction:
pass
assert len(events) == 1
assert events[0]["transaction"] == "<unlabeled transaction>"
# the name can be set once the transaction's already started
with start_transaction() as transaction:
transaction.name = "name-known-after-transaction-started"
assert len(events) == 2
assert events[1]["transaction"] == "name-known-after-transaction-started"
# passing in a name works, too
with start_transaction(name="a"):
pass
assert len(events) == 3
assert events[2]["transaction"] == "a"
def test_start_transaction(sentry_init):
sentry_init(traces_sample_rate=1.0)
# you can have it start a transaction for you
result1 = start_transaction(
name="/interactions/other-dogs/new-dog", op="greeting.sniff"
)
assert isinstance(result1, Transaction)
assert result1.name == "/interactions/other-dogs/new-dog"
assert result1.op == "greeting.sniff"
# or you can pass it an already-created transaction
preexisting_transaction = Transaction(
name="/interactions/other-dogs/new-dog", op="greeting.sniff"
)
result2 = start_transaction(preexisting_transaction)
assert result2 is preexisting_transaction
def test_finds_transaction_on_scope(sentry_init):
sentry_init(traces_sample_rate=1.0)
transaction = start_transaction(name="dogpark")
scope = Hub.current.scope
# See note in Scope class re: getters and setters of the `transaction`
# property. For the moment, assigning to scope.transaction merely sets the
# transaction name, rather than putting the transaction on the scope, so we
# have to assign to _span directly.
scope._span = transaction
# Reading scope.property, however, does what you'd expect, and returns the
# transaction on the scope.
assert scope.transaction is not None
assert isinstance(scope.transaction, Transaction)
assert scope.transaction.name == "dogpark"
# If the transaction is also set as the span on the scope, it can be found
# by accessing _span, too.
assert scope._span is not None
assert isinstance(scope._span, Transaction)
assert scope._span.name == "dogpark"
def test_finds_transaction_when_descendent_span_is_on_scope(
sentry_init,
):
sentry_init(traces_sample_rate=1.0)
transaction = start_transaction(name="dogpark")
child_span = transaction.start_child(op="sniffing")
scope = Hub.current.scope
scope._span = child_span
# this is the same whether it's the transaction itself or one of its
# decedents directly attached to the scope
assert scope.transaction is not None
assert isinstance(scope.transaction, Transaction)
assert scope.transaction.name == "dogpark"
# here we see that it is in fact the span on the scope, rather than the
# transaction itself
assert scope._span is not None
assert isinstance(scope._span, Span)
assert scope._span.op == "sniffing"
def test_finds_orphan_span_on_scope(sentry_init):
# this is deprecated behavior which may be removed at some point (along with
# the start_span function)
sentry_init(traces_sample_rate=1.0)
span = start_span(op="sniffing")
scope = Hub.current.scope
scope._span = span
assert scope._span is not None
assert isinstance(scope._span, Span)
assert scope._span.op == "sniffing"
def test_finds_non_orphan_span_on_scope(sentry_init):
sentry_init(traces_sample_rate=1.0)
transaction = start_transaction(name="dogpark")
child_span = transaction.start_child(op="sniffing")
scope = Hub.current.scope
scope._span = child_span
assert scope._span is not None
assert isinstance(scope._span, Span)
assert scope._span.op == "sniffing"
def test_circular_references(monkeypatch, sentry_init, request):
# TODO: We discovered while writing this test about transaction/span
# reference cycles that there's actually also a circular reference in
# `serializer.py`, between the functions `_serialize_node` and
# `_serialize_node_impl`, both of which are defined inside of the main
# `serialize` function, and each of which calls the other one. For now, in
# order to avoid having those ref cycles give us a false positive here, we
# can mock out `serialize`. In the long run, though, we should probably fix
# that. (Whenever we do work on fixing it, it may be useful to add
#
# gc.set_debug(gc.DEBUG_LEAK)
# request.addfinalizer(lambda: gc.set_debug(~gc.DEBUG_LEAK))
#
# immediately after the initial collection below, so we can see what new
# objects the garbage collecter has to clean up once `transaction.finish` is
# called and the serializer runs.)
monkeypatch.setattr(
sentry_sdk.client,
"serialize",
mock.Mock(
return_value=None,
),
)
# In certain versions of python, in some environments (specifically, python
# 3.4 when run in GH Actions), we run into a `ctypes` bug which creates
# circular references when `uuid4()` is called, as happens when we're
# generating event ids. Mocking it with an implementation which doesn't use
# the `ctypes` function lets us avoid having false positives when garbage
# collecting. See https://bugs.python.org/issue20519.
monkeypatch.setattr(
uuid,
"uuid4",
mock.Mock(
return_value=uuid.UUID(bytes=os.urandom(16)),
),
)
gc.disable()
request.addfinalizer(gc.enable)
sentry_init(traces_sample_rate=1.0)
# Make sure that we're starting with a clean slate before we start creating
# transaction/span reference cycles
gc.collect()
dogpark_transaction = start_transaction(name="dogpark")
sniffing_span = dogpark_transaction.start_child(op="sniffing")
wagging_span = dogpark_transaction.start_child(op="wagging")
# At some point, you have to stop sniffing - there are balls to chase! - so finish
# this span while the dogpark transaction is still open
sniffing_span.finish()
# The wagging, however, continues long past the dogpark, so that span will
# NOT finish before the transaction ends. (Doing it in this order proves
# that both finished and unfinished spans get their cycles broken.)
dogpark_transaction.finish()
# Eventually you gotta sleep...
wagging_span.finish()
# assuming there are no cycles by this point, these should all be able to go
# out of scope and get their memory deallocated without the garbage
# collector having anything to do
del sniffing_span
del wagging_span
del dogpark_transaction
assert gc.collect() == 0
# TODO (kmclb) remove this test once tracestate is a real feature
@pytest.mark.parametrize("tracestate_enabled", [True, False, None])
def test_has_tracestate_enabled(sentry_init, tracestate_enabled):
experiments = (
{"propagate_tracestate": tracestate_enabled}
if tracestate_enabled is not None
else {}
)
sentry_init(_experiments=experiments)
if tracestate_enabled is True:
assert has_tracestate_enabled() is True
else:
assert has_tracestate_enabled() is False
def test_set_meaurement(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, _experiments={"custom_measurements": True})
events = capture_events()
transaction = start_transaction(name="measuring stuff")
transaction.set_measurement("metric.foo", 123)
transaction.set_measurement("metric.bar", 456, unit="second")
transaction.set_measurement("metric.baz", 420.69, unit="custom")
transaction.finish()
(event,) = events
assert event["measurements"]["metric.foo"] == {"value": 123, "unit": ""}
assert event["measurements"]["metric.bar"] == {"value": 456, "unit": "second"}
assert event["measurements"]["metric.baz"] == {"value": 420.69, "unit": "custom"}