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
214 lines (148 loc) · 6.67 KB
/
Copy pathtest_attributes.py
File metadata and controls
214 lines (148 loc) · 6.67 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
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
import sentry_sdk
def test_top_level_api(sentry_init, capture_items):
sentry_init()
items = capture_items("trace_metric")
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 = [item.payload for item in items]
(metric,) = metrics
assert metric["attributes"]["set"] == "value"
assert "removed" not in metric["attributes"]
def test_scope_precedence(sentry_init, capture_items):
# 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()
items = capture_items("trace_metric")
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 = [item.payload for item in items]
(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_items):
# 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()
items = capture_items("trace_metric")
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 = [item.payload for item in items]
(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_items):
sentry_init()
items = capture_items("trace_metric")
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 = [item.payload for item in items]
(metric,) = metrics
assert "outofscope.attribute" not in metric["attributes"]
def test_remove_attribute(sentry_init, capture_items):
sentry_init()
items = capture_items("trace_metric")
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 = [item.payload for item in items]
(metric,) = metrics
assert "some.attribute" not in metric["attributes"]
def test_scope_attributes_preserialized(sentry_init, capture_items):
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)
items = capture_items("trace_metric")
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 = [item.payload for item in items]
(metric,) = metrics
# Attrs originally from the scope are serialized when sent
assert isinstance(metric["attributes"]["instance"], str)
assert isinstance(metric["attributes"]["dictionary"], str)
def test_user_attributes(sentry_init, capture_items):
sentry_init(
traces_sample_rate=1.0,
send_default_pii=True,
_experiments={
"trace_lifecycle": "stream",
},
)
items = capture_items("trace_metric", "span")
sentry_sdk.set_user(
{
"id": "123456",
"username": "username",
"email": "username@example.com",
"ip_address": "127.0.0.1",
}
)
with sentry_sdk.traces.start_span(name="login"):
sentry_sdk.metrics.count("test", 1)
sentry_sdk.get_client().flush()
metric, span = [item.payload for item in items]
assert metric["attributes"]["user.id"] == "123456"
assert metric["attributes"]["user.name"] == "username"
assert metric["attributes"]["user.email"] == "username@example.com"
assert metric["attributes"]["user.ip_address"] == "127.0.0.1"
assert span["attributes"]["user.id"] == "123456"
assert span["attributes"]["user.name"] == "username"
assert span["attributes"]["user.email"] == "username@example.com"
assert span["attributes"]["user.ip_address"] == "127.0.0.1"