forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
115 lines (87 loc) · 3.59 KB
/
test_api.py
File metadata and controls
115 lines (87 loc) · 3.59 KB
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
from sentry_sdk import (
configure_scope,
continue_trace,
get_baggage,
get_current_span,
get_traceparent,
start_transaction,
)
from sentry_sdk.hub import Hub
try:
from unittest import mock # python 3.3 and above
except ImportError:
import mock # python < 3.3
def test_get_current_span():
fake_hub = mock.MagicMock()
fake_hub.scope = mock.MagicMock()
fake_hub.scope.span = mock.MagicMock()
assert get_current_span(fake_hub) == fake_hub.scope.span
fake_hub.scope.span = None
assert get_current_span(fake_hub) is None
def test_get_current_span_default_hub(sentry_init):
sentry_init()
assert get_current_span() is None
with configure_scope() as scope:
fake_span = mock.MagicMock()
scope.span = fake_span
assert get_current_span() == fake_span
def test_get_current_span_default_hub_with_transaction(sentry_init):
sentry_init()
assert get_current_span() is None
with start_transaction() as new_transaction:
assert get_current_span() == new_transaction
def test_traceparent_with_tracing_enabled(sentry_init):
sentry_init(traces_sample_rate=1.0)
with start_transaction() as transaction:
expected_traceparent = "%s-%s-1" % (
transaction.trace_id,
transaction.span_id,
)
assert get_traceparent() == expected_traceparent
def test_traceparent_with_tracing_disabled(sentry_init):
sentry_init()
propagation_context = Hub.current.scope._propagation_context
expected_traceparent = "%s-%s" % (
propagation_context["trace_id"],
propagation_context["span_id"],
)
assert get_traceparent() == expected_traceparent
def test_baggage_with_tracing_disabled(sentry_init):
sentry_init(release="1.0.0", environment="dev")
propagation_context = Hub.current.scope._propagation_context
expected_baggage = (
"sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0".format(
propagation_context["trace_id"]
)
)
# order not guaranteed in older python versions
assert sorted(get_baggage().split(",")) == sorted(expected_baggage.split(","))
def test_baggage_with_tracing_enabled(sentry_init):
sentry_init(traces_sample_rate=1.0, release="1.0.0", environment="dev")
with start_transaction() as transaction:
expected_baggage = "sentry-trace_id={},sentry-environment=dev,sentry-release=1.0.0,sentry-sample_rate=1.0,sentry-sampled={}".format(
transaction.trace_id, "true" if transaction.sampled else "false"
)
# order not guaranteed in older python versions
assert sorted(get_baggage().split(",")) == sorted(expected_baggage.split(","))
def test_continue_trace(sentry_init):
sentry_init()
trace_id = "471a43a4192642f0b136d5159a501701"
parent_span_id = "6e8f22c393e68f19"
parent_sampled = 1
transaction = continue_trace(
{
"sentry-trace": "{}-{}-{}".format(trace_id, parent_span_id, parent_sampled),
"baggage": "sentry-trace_id=566e3688a61d4bc888951642d6f14a19",
},
name="some name",
)
with start_transaction(transaction):
assert transaction.name == "some name"
propagation_context = Hub.current.scope._propagation_context
assert propagation_context["trace_id"] == transaction.trace_id == trace_id
assert propagation_context["parent_span_id"] == parent_span_id
assert propagation_context["parent_sampled"] == parent_sampled
assert propagation_context["dynamic_sampling_context"] == {
"trace_id": "566e3688a61d4bc888951642d6f14a19"
}