-
Notifications
You must be signed in to change notification settings - Fork 609
Expand file tree
/
Copy pathtest_metrics.py
More file actions
293 lines (211 loc) · 8.64 KB
/
test_metrics.py
File metadata and controls
293 lines (211 loc) · 8.64 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
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
import json
import sys
from typing import List, Any, Mapping
import pytest
import sentry_sdk
from sentry_sdk import get_client
from sentry_sdk.envelope import Envelope
from sentry_sdk.types import Metric
from sentry_sdk.consts import SPANDATA, VERSION
def envelopes_to_metrics(envelopes):
# type: (List[Envelope]) -> List[Metric]
res = [] # type: List[Metric]
for envelope in envelopes:
for item in envelope.items:
if item.type == "trace_metric":
for metric_json in item.payload.json["items"]:
metric = {
"timestamp": metric_json["timestamp"],
"trace_id": metric_json["trace_id"],
"span_id": metric_json.get("span_id"),
"name": metric_json["name"],
"type": metric_json["type"],
"value": metric_json["value"],
"unit": metric_json.get("unit"),
"attributes": {
k: v["value"]
for (k, v) in metric_json["attributes"].items()
},
} # type: Metric
res.append(metric)
return res
def test_metrics_disabled(sentry_init, capture_envelopes):
sentry_init(enable_metrics=False)
envelopes = capture_envelopes()
sentry_sdk.metrics.count("test.counter", 1)
sentry_sdk.metrics.gauge("test.gauge", 42)
sentry_sdk.metrics.distribution("test.distribution", 200)
assert len(envelopes) == 0
def test_metrics_basics(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()
sentry_sdk.metrics.count("test.counter", 1)
sentry_sdk.metrics.gauge("test.gauge", 42, unit="millisecond")
sentry_sdk.metrics.distribution("test.distribution", 200, unit="second")
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 3
assert metrics[0]["name"] == "test.counter"
assert metrics[0]["type"] == "counter"
assert metrics[0]["value"] == 1.0
assert metrics[0]["unit"] is None
assert "sentry.sdk.name" in metrics[0]["attributes"]
assert "sentry.sdk.version" in metrics[0]["attributes"]
assert metrics[1]["name"] == "test.gauge"
assert metrics[1]["type"] == "gauge"
assert metrics[1]["value"] == 42.0
assert metrics[1]["unit"] == "millisecond"
assert metrics[2]["name"] == "test.distribution"
assert metrics[2]["type"] == "distribution"
assert metrics[2]["value"] == 200.0
assert metrics[2]["unit"] == "second"
def test_metrics_experimental_option(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()
sentry_sdk.metrics.count("test.counter", 5)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
assert metrics[0]["name"] == "test.counter"
assert metrics[0]["type"] == "counter"
assert metrics[0]["value"] == 5.0
def test_metrics_with_attributes(sentry_init, capture_envelopes):
sentry_init(release="1.0.0", environment="test", server_name="test-server")
envelopes = capture_envelopes()
sentry_sdk.metrics.count(
"test.counter", 1, attributes={"endpoint": "/api/test", "status": "success"}
)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
assert metrics[0]["attributes"]["endpoint"] == "/api/test"
assert metrics[0]["attributes"]["status"] == "success"
assert metrics[0]["attributes"]["sentry.release"] == "1.0.0"
assert metrics[0]["attributes"]["sentry.environment"] == "test"
assert metrics[0]["attributes"][SPANDATA.SERVER_ADDRESS] == "test-server"
assert metrics[0]["attributes"]["sentry.sdk.name"].startswith("sentry.python")
assert metrics[0]["attributes"]["sentry.sdk.version"] == VERSION
def test_metrics_with_user(sentry_init, capture_envelopes):
sentry_init(send_default_pii=True)
envelopes = capture_envelopes()
sentry_sdk.set_user(
{"id": "user-123", "email": "test@example.com", "username": "testuser"}
)
sentry_sdk.metrics.count("test.user.counter", 1)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
assert metrics[0]["attributes"]["user.id"] == "user-123"
assert metrics[0]["attributes"]["user.email"] == "test@example.com"
assert metrics[0]["attributes"]["user.name"] == "testuser"
def test_metrics_no_user_if_pii_off(sentry_init, capture_envelopes):
sentry_init(send_default_pii=False)
envelopes = capture_envelopes()
sentry_sdk.set_user(
{"id": "user-123", "email": "test@example.com", "username": "testuser"}
)
sentry_sdk.metrics.count("test.user.counter", 1)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
assert "user.id" not in metrics[0]["attributes"]
assert "user.email" not in metrics[0]["attributes"]
assert "user.name" not in metrics[0]["attributes"]
def test_metrics_with_span(sentry_init, capture_envelopes):
sentry_init(traces_sample_rate=1.0)
envelopes = capture_envelopes()
with sentry_sdk.start_transaction(op="test", name="test-span") as transaction:
sentry_sdk.metrics.count("test.span.counter", 1)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
assert metrics[0]["trace_id"] is not None
assert metrics[0]["trace_id"] == transaction.trace_id
assert metrics[0]["span_id"] == transaction.span_id
def test_metrics_tracing_without_performance(sentry_init, capture_envelopes):
sentry_init()
envelopes = capture_envelopes()
with sentry_sdk.isolation_scope() as isolation_scope:
sentry_sdk.metrics.count("test.span.counter", 1)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
propagation_context = isolation_scope._propagation_context
assert propagation_context is not None
assert metrics[0]["trace_id"] == propagation_context.trace_id
assert metrics[0]["span_id"] == propagation_context.span_id
def test_metrics_before_send(sentry_init, capture_envelopes):
before_metric_called = False
def _before_metric(record, hint):
nonlocal before_metric_called
assert set(record.keys()) == {
"timestamp",
"trace_id",
"span_id",
"name",
"type",
"value",
"unit",
"attributes",
}
if record["name"] == "test.skip":
return None
before_metric_called = True
return record
sentry_init(
before_send_metric=_before_metric,
)
envelopes = capture_envelopes()
sentry_sdk.metrics.count("test.skip", 1)
sentry_sdk.metrics.count("test.keep", 1)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
assert metrics[0]["name"] == "test.keep"
assert before_metric_called
def test_metrics_experimental_before_send(sentry_init, capture_envelopes):
before_metric_called = False
def _before_metric(record, hint):
nonlocal before_metric_called
assert set(record.keys()) == {
"timestamp",
"trace_id",
"span_id",
"name",
"type",
"value",
"unit",
"attributes",
}
if record["name"] == "test.skip":
return None
before_metric_called = True
return record
sentry_init(
_experiments={
"before_send_metric": _before_metric,
},
)
envelopes = capture_envelopes()
sentry_sdk.metrics.count("test.skip", 1)
sentry_sdk.metrics.count("test.keep", 1)
get_client().flush()
metrics = envelopes_to_metrics(envelopes)
assert len(metrics) == 1
assert metrics[0]["name"] == "test.keep"
assert before_metric_called
def test_batcher_drops_metrics(sentry_init, monkeypatch):
sentry_init()
client = sentry_sdk.get_client()
def no_op_flush():
pass
monkeypatch.setattr(client.metrics_batcher, "_flush", no_op_flush)
lost_event_calls = []
def record_lost_event(reason, data_category, quantity):
lost_event_calls.append((reason, data_category, quantity))
monkeypatch.setattr(client.metrics_batcher, "_record_lost_func", record_lost_event)
for i in range(10_005): # 5 metrics over the hard limit
sentry_sdk.metrics.count("test.counter", 1)
assert len(lost_event_calls) == 5
for lost_event_call in lost_event_calls:
assert lost_event_call == ("queue_overflow", "trace_metric", 1)