forked from getsentry/sentry-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_attributes.py
More file actions
178 lines (120 loc) · 5.57 KB
/
Copy pathtest_attributes.py
File metadata and controls
178 lines (120 loc) · 5.57 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
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
import sentry_sdk
from tests.test_metrics import envelopes_to_metrics
def test_top_level_api(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()
sentry_sdk.set_attribute("set", "value")
sentry_sdk.set_attribute("removed", "value")
sentry_sdk.remove_attribute("removed")
# Attempting to remove a nonexistent attribute should not raise
sentry_sdk.remove_attribute("nonexistent")
sentry_sdk.metrics.count("test", 1)
sentry_sdk.get_client().flush()
metrics = envelopes_to_metrics(envelopes)
(metric,) = metrics
assert metric["attributes"]["set"] == "value"
assert "removed" not in metric["attributes"]
def test_scope_precedence(sentry_init, capture_envelopes):
# Order of precedence, from most important to least:
# 1. telemetry attributes (directly supplying attributes on creation or using set_attribute)
# 2. current scope attributes
# 3. isolation scope attributes
# 4. global scope attributes
sentry_init()
envelopes = capture_envelopes()
global_scope = sentry_sdk.get_global_scope()
global_scope.set_attribute("global.attribute", "global")
global_scope.set_attribute("overwritten.attribute", "global")
isolation_scope = sentry_sdk.get_isolation_scope()
isolation_scope.set_attribute("isolation.attribute", "isolation")
isolation_scope.set_attribute("overwritten.attribute", "isolation")
current_scope = sentry_sdk.get_current_scope()
current_scope.set_attribute("current.attribute", "current")
current_scope.set_attribute("overwritten.attribute", "current")
sentry_sdk.metrics.count("test", 1)
sentry_sdk.get_client().flush()
metrics = envelopes_to_metrics(envelopes)
(metric,) = metrics
assert metric["attributes"]["global.attribute"] == "global"
assert metric["attributes"]["isolation.attribute"] == "isolation"
assert metric["attributes"]["current.attribute"] == "current"
assert metric["attributes"]["overwritten.attribute"] == "current"
def test_telemetry_precedence(sentry_init, capture_envelopes):
# Order of precedence, from most important to least:
# 1. telemetry attributes (directly supplying attributes on creation or using set_attribute)
# 2. current scope attributes
# 3. isolation scope attributes
# 4. global scope attributes
sentry_init()
envelopes = capture_envelopes()
global_scope = sentry_sdk.get_global_scope()
global_scope.set_attribute("global.attribute", "global")
global_scope.set_attribute("overwritten.attribute", "global")
isolation_scope = sentry_sdk.get_isolation_scope()
isolation_scope.set_attribute("isolation.attribute", "isolation")
isolation_scope.set_attribute("overwritten.attribute", "isolation")
current_scope = sentry_sdk.get_current_scope()
current_scope.set_attribute("current.attribute", "current")
current_scope.set_attribute("overwritten.attribute", "current")
sentry_sdk.metrics.count(
"test",
1,
attributes={
"telemetry.attribute": "telemetry",
"overwritten.attribute": "telemetry",
},
)
sentry_sdk.get_client().flush()
metrics = envelopes_to_metrics(envelopes)
(metric,) = metrics
assert metric["attributes"]["global.attribute"] == "global"
assert metric["attributes"]["isolation.attribute"] == "isolation"
assert metric["attributes"]["current.attribute"] == "current"
assert metric["attributes"]["telemetry.attribute"] == "telemetry"
assert metric["attributes"]["overwritten.attribute"] == "telemetry"
def test_attribute_out_of_scope(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()
with sentry_sdk.new_scope() as scope:
scope.set_attribute("outofscope.attribute", "out of scope")
sentry_sdk.metrics.count("test", 1)
sentry_sdk.get_client().flush()
metrics = envelopes_to_metrics(envelopes)
(metric,) = metrics
assert "outofscope.attribute" not in metric["attributes"]
def test_remove_attribute(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()
with sentry_sdk.new_scope() as scope:
scope.set_attribute("some.attribute", 123)
scope.remove_attribute("some.attribute")
sentry_sdk.metrics.count("test", 1)
sentry_sdk.get_client().flush()
metrics = envelopes_to_metrics(envelopes)
(metric,) = metrics
assert "some.attribute" not in metric["attributes"]
def test_scope_attributes_preserialized(sentry_init, capture_envelopes):
def before_send_metric(metric, _):
# Scope attrs show up serialized in before_send
assert isinstance(metric["attributes"]["instance"], str)
assert isinstance(metric["attributes"]["dictionary"], str)
return metric
sentry_init(before_send_metric=before_send_metric)
envelopes = capture_envelopes()
class Cat:
pass
instance = Cat()
dictionary = {"color": "tortoiseshell"}
with sentry_sdk.new_scope() as scope:
scope.set_attribute("instance", instance)
scope.set_attribute("dictionary", dictionary)
# Scope attrs are stored preserialized
assert isinstance(scope._attributes["instance"], str)
assert isinstance(scope._attributes["dictionary"], str)
sentry_sdk.metrics.count("test", 1)
sentry_sdk.get_client().flush()
metrics = envelopes_to_metrics(envelopes)
(metric,) = metrics
# Attrs originally from the scope are serialized when sent
assert isinstance(metric["attributes"]["instance"], str)
assert isinstance(metric["attributes"]["dictionary"], str)