-
Notifications
You must be signed in to change notification settings - Fork 536
/
Copy pathtest_misc.py
507 lines (386 loc) · 16.9 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
import pytest
import gc
import uuid
import os
from unittest import mock
from unittest.mock import MagicMock
import sentry_sdk
from sentry_sdk import start_span, start_transaction, set_measurement
from sentry_sdk.consts import MATCH_ALL
from sentry_sdk.tracing import Span, Transaction
from sentry_sdk.tracing_utils import should_propagate_trace
from sentry_sdk.utils import Dsn
from tests.conftest import ApproxDict
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"
assert event["_meta"]["spans"][""]["len"] == 10
assert "_dropped_spans" not in event
assert "dropped_spans" not in event
def test_span_data_scrubbing_and_trimming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0, _experiments={"max_spans": 3})
events = capture_events()
with start_transaction(name="hi"):
with start_span(op="foo", name="bar") as span:
span.set_data("password", "secret")
span.set_data("datafoo", "databar")
for i in range(10):
with start_span(op="foo{}".format(i)):
pass
(event,) = events
assert event["spans"][0]["data"] == ApproxDict(
{"password": "[Filtered]", "datafoo": "databar"}
)
assert event["_meta"]["spans"] == {
"0": {"data": {"password": {"": {"rem": [["!config", "s"]]}}}},
"": {"len": 11},
}
def test_transaction_naming(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
# 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_transaction_data(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
with start_transaction(name="test-transaction"):
span_or_tx = sentry_sdk.get_current_span()
span_or_tx.set_data("foo", "bar")
with start_span(op="test-span") as span:
span.set_data("spanfoo", "spanbar")
assert len(events) == 1
transaction = events[0]
transaction_data = transaction["contexts"]["trace"]["data"]
assert "data" not in transaction.keys()
assert transaction_data.items() >= {"foo": "bar"}.items()
assert len(transaction["spans"]) == 1
span = transaction["spans"][0]
span_data = span["data"]
assert "contexts" not in span.keys()
assert span_data.items() >= {"spanfoo": "spanbar"}.items()
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 = sentry_sdk.get_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 = sentry_sdk.get_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 = sentry_sdk.get_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 = sentry_sdk.get_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 collector 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
def test_set_meaurement(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
transaction = start_transaction(name="measuring stuff")
with pytest.raises(TypeError):
transaction.set_measurement()
with pytest.raises(TypeError):
transaction.set_measurement("metric.foo")
transaction.set_measurement("metric.foo", 123)
transaction.set_measurement("metric.bar", 456, unit="second")
transaction.set_measurement("metric.baz", 420.69, unit="custom")
transaction.set_measurement("metric.foobar", 12, unit="percent")
transaction.set_measurement("metric.foobar", 17.99, unit="percent")
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"}
assert event["measurements"]["metric.foobar"] == {"value": 17.99, "unit": "percent"}
def test_set_meaurement_public_api(sentry_init, capture_events):
sentry_init(traces_sample_rate=1.0)
events = capture_events()
with start_transaction(name="measuring stuff"):
set_measurement("metric.foo", 123)
set_measurement("metric.bar", 456, unit="second")
(event,) = events
assert event["measurements"]["metric.foo"] == {"value": 123, "unit": ""}
assert event["measurements"]["metric.bar"] == {"value": 456, "unit": "second"}
def test_set_measurement_deprecated(sentry_init):
sentry_init(traces_sample_rate=1.0)
with start_transaction(name="measuring stuff") as trx:
with pytest.warns(DeprecationWarning):
set_measurement("metric.foo", 123)
with pytest.warns(DeprecationWarning):
trx.set_measurement("metric.bar", 456)
with start_span(op="measuring span") as span:
with pytest.warns(DeprecationWarning):
span.set_measurement("metric.baz", 420.69, unit="custom")
def test_set_meaurement_compared_to_set_data(sentry_init, capture_events):
"""
This is just a test to see if we can use set_data() instead of set_measurement() with
the same result in the backend.
"""
sentry_init(traces_sample_rate=1.0)
events = capture_events()
with start_transaction(name="measuring stuff") as transaction:
transaction.set_measurement("metric.foo", 123)
transaction.set_data("metric.bar", 456)
with start_span(op="measuring span") as span:
span.set_measurement("metric.baz", 420.69, unit="custom")
span.set_data("metric.qux", 789)
(event,) = events
from pprint import pprint
pprint(event)
raise AssertionError("This test is not finished yet.")
@pytest.mark.parametrize(
"trace_propagation_targets,url,expected_propagation_decision",
[
(None, "http://example.com", False),
([], "http://example.com", False),
([MATCH_ALL], "http://example.com", True),
(["localhost"], "localhost:8443/api/users", True),
(["localhost"], "http://localhost:8443/api/users", True),
(["localhost"], "mylocalhost:8080/api/users", True),
([r"^/api"], "/api/envelopes", True),
([r"^/api"], "/backend/api/envelopes", False),
([r"myApi.com/v[2-4]"], "myApi.com/v2/projects", True),
([r"myApi.com/v[2-4]"], "myApi.com/v1/projects", False),
([r"https:\/\/.*"], "https://example.com", True),
(
[r"https://.*"],
"https://example.com",
True,
), # to show escaping is not needed
([r"https://.*"], "http://example.com/insecure/", False),
],
)
def test_should_propagate_trace(
trace_propagation_targets, url, expected_propagation_decision
):
client = MagicMock()
# This test assumes the urls are not Sentry URLs. Use test_should_propagate_trace_to_sentry for sentry URLs.
client.is_sentry_url = lambda _: False
client.options = {"trace_propagation_targets": trace_propagation_targets}
client.transport = MagicMock()
client.transport.parsed_dsn = Dsn("https://[email protected]/12312012")
assert should_propagate_trace(client, url) == expected_propagation_decision
@pytest.mark.parametrize(
"dsn,url,expected_propagation_decision",
[
(
"https://[email protected]/12312012",
"http://example.com",
True,
),
(
"https://[email protected]/12312012",
"https://[email protected]/12312012",
False,
),
(
"https://[email protected]/12312012",
"http://squirrelchasers.ingest.sentry.io/12312012",
False,
),
(
"https://[email protected]/12312012",
"http://ingest.sentry.io/12312012",
True,
),
(
"https://[email protected]/12312012",
"http://localsentry.example.com",
False,
),
],
)
def test_should_propagate_trace_to_sentry(
sentry_init, dsn, url, expected_propagation_decision
):
sentry_init(
dsn=dsn,
traces_sample_rate=1.0,
)
client = sentry_sdk.get_client()
client.transport.parsed_dsn = Dsn(dsn)
assert should_propagate_trace(client, url) == expected_propagation_decision
def test_start_transaction_updates_scope_name_source(sentry_init):
sentry_init(traces_sample_rate=1.0)
scope = sentry_sdk.get_current_scope()
with start_transaction(name="foobar", source="route"):
assert scope._transaction == "foobar"
assert scope._transaction_info == {"source": "route"}
@pytest.mark.parametrize("sampled", (True, None))
def test_transaction_dropped_debug_not_started(sentry_init, sampled):
sentry_init(enable_tracing=True)
tx = Transaction(sampled=sampled)
with mock.patch("sentry_sdk.tracing.logger") as mock_logger:
with tx:
pass
mock_logger.debug.assert_any_call(
"Discarding transaction because it was not started with sentry_sdk.start_transaction"
)
with pytest.raises(AssertionError):
# We should NOT see the "sampled = False" message here
mock_logger.debug.assert_any_call(
"Discarding transaction because sampled = False"
)
def test_transaction_dropeed_sampled_false(sentry_init):
sentry_init(enable_tracing=True)
tx = Transaction(sampled=False)
with mock.patch("sentry_sdk.tracing.logger") as mock_logger:
with sentry_sdk.start_transaction(tx):
pass
mock_logger.debug.assert_any_call("Discarding transaction because sampled = False")
with pytest.raises(AssertionError):
# We should not see the "not started" message here
mock_logger.debug.assert_any_call(
"Discarding transaction because it was not started with sentry_sdk.start_transaction"
)
def test_transaction_not_started_warning(sentry_init):
sentry_init(enable_tracing=True)
tx = Transaction()
with mock.patch("sentry_sdk.tracing.logger") as mock_logger:
with tx:
pass
mock_logger.debug.assert_any_call(
"Transaction was entered without being started with sentry_sdk.start_transaction."
"The transaction will not be sent to Sentry. To fix, start the transaction by"
"passing it to sentry_sdk.start_transaction."
)