-
Notifications
You must be signed in to change notification settings - Fork 535
/
Copy pathtest_span_processor.py
617 lines (505 loc) · 20.9 KB
/
test_span_processor.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import time
from datetime import datetime, timezone
from unittest import mock
from unittest.mock import MagicMock
import pytest
from opentelemetry.trace import SpanKind, SpanContext, Status, StatusCode
import sentry_sdk
from sentry_sdk.integrations.opentelemetry.span_processor import (
SentrySpanProcessor,
link_trace_context_to_error_event,
)
from sentry_sdk.tracing import Span, Transaction
from sentry_sdk.tracing_utils import extract_sentrytrace_data
def test_is_sentry_span():
otel_span = MagicMock()
span_processor = SentrySpanProcessor()
assert not span_processor._is_sentry_span(otel_span)
client = MagicMock()
client.options = {"instrumenter": "otel"}
client.dsn = "https://[email protected]/123456"
sentry_sdk.get_global_scope().set_client(client)
assert not span_processor._is_sentry_span(otel_span)
otel_span.attributes = {
"http.url": "https://example.com",
}
assert not span_processor._is_sentry_span(otel_span)
otel_span.attributes = {
"http.url": "https://o123456.ingest.sentry.io/api/123/envelope",
}
assert span_processor._is_sentry_span(otel_span)
def test_get_otel_context():
otel_span = MagicMock()
otel_span.attributes = {"foo": "bar"}
otel_span.resource = MagicMock()
otel_span.resource.attributes = {"baz": "qux"}
span_processor = SentrySpanProcessor()
otel_context = span_processor._get_otel_context(otel_span)
assert otel_context == {
"attributes": {"foo": "bar"},
"resource": {"baz": "qux"},
}
def test_get_trace_data_with_span_and_trace():
otel_span = MagicMock()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = None
parent_context = {}
span_processor = SentrySpanProcessor()
sentry_trace_data = span_processor._get_trace_data(otel_span, parent_context)
assert sentry_trace_data["trace_id"] == "1234567890abcdef1234567890abcdef"
assert sentry_trace_data["span_id"] == "1234567890abcdef"
assert sentry_trace_data["parent_span_id"] is None
assert sentry_trace_data["parent_sampled"] is None
assert sentry_trace_data["baggage"] is None
def test_get_trace_data_with_span_and_trace_and_parent():
otel_span = MagicMock()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = MagicMock()
otel_span.parent.span_id = int("abcdef1234567890", 16)
parent_context = {}
span_processor = SentrySpanProcessor()
sentry_trace_data = span_processor._get_trace_data(otel_span, parent_context)
assert sentry_trace_data["trace_id"] == "1234567890abcdef1234567890abcdef"
assert sentry_trace_data["span_id"] == "1234567890abcdef"
assert sentry_trace_data["parent_span_id"] == "abcdef1234567890"
assert sentry_trace_data["parent_sampled"] is None
assert sentry_trace_data["baggage"] is None
def test_get_trace_data_with_sentry_trace():
otel_span = MagicMock()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = MagicMock()
otel_span.parent.span_id = int("abcdef1234567890", 16)
parent_context = {}
with mock.patch(
"sentry_sdk.integrations.opentelemetry.span_processor.get_value",
side_effect=[
extract_sentrytrace_data(
"1234567890abcdef1234567890abcdef-1234567890abcdef-1"
),
None,
],
):
span_processor = SentrySpanProcessor()
sentry_trace_data = span_processor._get_trace_data(otel_span, parent_context)
assert sentry_trace_data["trace_id"] == "1234567890abcdef1234567890abcdef"
assert sentry_trace_data["span_id"] == "1234567890abcdef"
assert sentry_trace_data["parent_span_id"] == "abcdef1234567890"
assert sentry_trace_data["parent_sampled"] is True
assert sentry_trace_data["baggage"] is None
with mock.patch(
"sentry_sdk.integrations.opentelemetry.span_processor.get_value",
side_effect=[
extract_sentrytrace_data(
"1234567890abcdef1234567890abcdef-1234567890abcdef-0"
),
None,
],
):
span_processor = SentrySpanProcessor()
sentry_trace_data = span_processor._get_trace_data(otel_span, parent_context)
assert sentry_trace_data["trace_id"] == "1234567890abcdef1234567890abcdef"
assert sentry_trace_data["span_id"] == "1234567890abcdef"
assert sentry_trace_data["parent_span_id"] == "abcdef1234567890"
assert sentry_trace_data["parent_sampled"] is False
assert sentry_trace_data["baggage"] is None
def test_get_trace_data_with_sentry_trace_and_baggage():
otel_span = MagicMock()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = MagicMock()
otel_span.parent.span_id = int("abcdef1234567890", 16)
parent_context = {}
baggage = (
"sentry-trace_id=771a43a4192642f0b136d5159a501700,"
"sentry-public_key=49d0f7386ad645858ae85020e393bef3,"
"sentry-sample_rate=0.01337,sentry-user_id=Am%C3%A9lie"
)
with mock.patch(
"sentry_sdk.integrations.opentelemetry.span_processor.get_value",
side_effect=[
extract_sentrytrace_data(
"1234567890abcdef1234567890abcdef-1234567890abcdef-1"
),
baggage,
],
):
span_processor = SentrySpanProcessor()
sentry_trace_data = span_processor._get_trace_data(otel_span, parent_context)
assert sentry_trace_data["trace_id"] == "1234567890abcdef1234567890abcdef"
assert sentry_trace_data["span_id"] == "1234567890abcdef"
assert sentry_trace_data["parent_span_id"] == "abcdef1234567890"
assert sentry_trace_data["parent_sampled"]
assert sentry_trace_data["baggage"] == baggage
def test_update_span_with_otel_data_http_method():
sentry_span = Span()
otel_span = MagicMock()
otel_span.name = "Test OTel Span"
otel_span.kind = SpanKind.CLIENT
otel_span.attributes = {
"http.method": "GET",
"http.status_code": 429,
"http.status_text": "xxx",
"http.user_agent": "curl/7.64.1",
"net.peer.name": "example.com",
"http.target": "/",
}
span_processor = SentrySpanProcessor()
span_processor._update_span_with_otel_data(sentry_span, otel_span)
assert sentry_span.op == "http.client"
assert sentry_span.description == "GET example.com /"
assert sentry_span.status == "resource_exhausted"
assert sentry_span._data["http.method"] == "GET"
assert sentry_span._data["http.response.status_code"] == 429
assert sentry_span._data["http.status_text"] == "xxx"
assert sentry_span._data["http.user_agent"] == "curl/7.64.1"
assert sentry_span._data["net.peer.name"] == "example.com"
assert sentry_span._data["http.target"] == "/"
@pytest.mark.parametrize(
"otel_status, expected_status",
[
pytest.param(Status(StatusCode.UNSET), None, id="unset"),
pytest.param(Status(StatusCode.OK), "ok", id="ok"),
pytest.param(Status(StatusCode.ERROR), "internal_error", id="error"),
],
)
def test_update_span_with_otel_status(otel_status, expected_status):
sentry_span = Span()
otel_span = MagicMock()
otel_span.name = "Test OTel Span"
otel_span.kind = SpanKind.INTERNAL
otel_span.status = otel_status
span_processor = SentrySpanProcessor()
span_processor._update_span_with_otel_status(sentry_span, otel_span)
assert sentry_span.get_trace_context().get("status") == expected_status
def test_update_span_with_otel_data_http_method2():
sentry_span = Span()
otel_span = MagicMock()
otel_span.name = "Test OTel Span"
otel_span.kind = SpanKind.SERVER
otel_span.attributes = {
"http.method": "GET",
"http.status_code": 429,
"http.status_text": "xxx",
"http.user_agent": "curl/7.64.1",
"http.url": "https://example.com/status/403?password=123&[email protected]&author=User123&auth=1234567890abcdef",
}
span_processor = SentrySpanProcessor()
span_processor._update_span_with_otel_data(sentry_span, otel_span)
assert sentry_span.op == "http.server"
assert sentry_span.description == "GET https://example.com/status/403"
assert sentry_span.status == "resource_exhausted"
assert sentry_span._data["http.method"] == "GET"
assert sentry_span._data["http.response.status_code"] == 429
assert sentry_span._data["http.status_text"] == "xxx"
assert sentry_span._data["http.user_agent"] == "curl/7.64.1"
assert (
sentry_span._data["http.url"]
== "https://example.com/status/403?password=123&[email protected]&author=User123&auth=1234567890abcdef"
)
def test_update_span_with_otel_data_db_query():
sentry_span = Span()
otel_span = MagicMock()
otel_span.name = "Test OTel Span"
otel_span.attributes = {
"db.system": "postgresql",
"db.statement": "SELECT * FROM table where pwd = '123456'",
}
span_processor = SentrySpanProcessor()
span_processor._update_span_with_otel_data(sentry_span, otel_span)
assert sentry_span.op == "db"
assert sentry_span.description == "SELECT * FROM table where pwd = '123456'"
assert sentry_span._data["db.system"] == "postgresql"
assert (
sentry_span._data["db.statement"] == "SELECT * FROM table where pwd = '123456'"
)
@pytest.mark.parametrize(
["dsn"],
[
pytest.param(
"https://[email protected]/123456", id="with a DSN"
),
pytest.param(None, id="with no DSN"),
],
)
def test_on_start_transaction(dsn):
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.start_time = time.time_ns()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = MagicMock()
otel_span.parent.span_id = int("abcdef1234567890", 16)
parent_context = {}
fake_start_transaction = MagicMock()
fake_client = MagicMock()
fake_client.options = {"instrumenter": "otel"}
fake_client.dsn = dsn
sentry_sdk.get_global_scope().set_client(fake_client)
with mock.patch(
"sentry_sdk.integrations.opentelemetry.span_processor.start_transaction",
fake_start_transaction,
):
span_processor = SentrySpanProcessor()
span_processor.on_start(otel_span, parent_context)
fake_start_transaction.assert_called_once_with(
name="Sample OTel Span",
span_id="1234567890abcdef",
parent_span_id="abcdef1234567890",
trace_id="1234567890abcdef1234567890abcdef",
baggage=None,
start_timestamp=datetime.fromtimestamp(
otel_span.start_time / 1e9, timezone.utc
),
instrumenter="otel",
origin="auto.otel",
)
assert len(span_processor.otel_span_map.keys()) == 1
assert list(span_processor.otel_span_map.keys())[0] == "1234567890abcdef"
def test_on_start_child():
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.start_time = time.time_ns()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = MagicMock()
otel_span.parent.span_id = int("abcdef1234567890", 16)
parent_context = {}
fake_client = MagicMock()
fake_client.options = {"instrumenter": "otel"}
fake_client.dsn = "https://[email protected]/123456"
sentry_sdk.get_global_scope().set_client(fake_client)
fake_span = MagicMock()
span_processor = SentrySpanProcessor()
span_processor.otel_span_map["abcdef1234567890"] = fake_span
span_processor.on_start(otel_span, parent_context)
fake_span.start_child.assert_called_once_with(
span_id="1234567890abcdef",
name="Sample OTel Span",
start_timestamp=datetime.fromtimestamp(
otel_span.start_time / 1e9, timezone.utc
),
instrumenter="otel",
origin="auto.otel",
)
assert len(span_processor.otel_span_map.keys()) == 2
assert "abcdef1234567890" in span_processor.otel_span_map.keys()
assert "1234567890abcdef" in span_processor.otel_span_map.keys()
def test_on_end_no_sentry_span():
"""
If on_end is called on a span that is not in the otel_span_map, it should be a no-op.
"""
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.end_time = time.time_ns()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
span_processor = SentrySpanProcessor()
span_processor.otel_span_map = {}
span_processor._get_otel_context = MagicMock()
span_processor._update_span_with_otel_data = MagicMock()
span_processor.on_end(otel_span)
span_processor._get_otel_context.assert_not_called()
span_processor._update_span_with_otel_data.assert_not_called()
def test_on_end_sentry_transaction():
"""
Test on_end for a sentry Transaction.
"""
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.end_time = time.time_ns()
otel_span.status = Status(StatusCode.OK)
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
fake_client = MagicMock()
fake_client.options = {"instrumenter": "otel"}
sentry_sdk.get_global_scope().set_client(fake_client)
fake_sentry_span = MagicMock(spec=Transaction)
fake_sentry_span.set_context = MagicMock()
fake_sentry_span.finish = MagicMock()
span_processor = SentrySpanProcessor()
span_processor._get_otel_context = MagicMock()
span_processor._update_span_with_otel_data = MagicMock()
span_processor.otel_span_map["1234567890abcdef"] = fake_sentry_span
span_processor.on_end(otel_span)
fake_sentry_span.set_context.assert_called_once()
span_processor._update_span_with_otel_data.assert_not_called()
fake_sentry_span.set_status.assert_called_once_with("ok")
fake_sentry_span.finish.assert_called_once()
def test_on_end_sentry_span():
"""
Test on_end for a sentry Span.
"""
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.end_time = time.time_ns()
otel_span.status = Status(StatusCode.OK)
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
fake_client = MagicMock()
fake_client.options = {"instrumenter": "otel"}
sentry_sdk.get_global_scope().set_client(fake_client)
fake_sentry_span = MagicMock(spec=Span)
fake_sentry_span.set_context = MagicMock()
fake_sentry_span.finish = MagicMock()
span_processor = SentrySpanProcessor()
span_processor._get_otel_context = MagicMock()
span_processor._update_span_with_otel_data = MagicMock()
span_processor.otel_span_map["1234567890abcdef"] = fake_sentry_span
span_processor.on_end(otel_span)
fake_sentry_span.set_context.assert_not_called()
span_processor._update_span_with_otel_data.assert_called_once_with(
fake_sentry_span, otel_span
)
fake_sentry_span.set_status.assert_called_once_with("ok")
fake_sentry_span.finish.assert_called_once()
def test_link_trace_context_to_error_event():
"""
Test that the trace context is added to the error event.
"""
fake_client = MagicMock()
fake_client.options = {"instrumenter": "otel"}
sentry_sdk.get_global_scope().set_client(fake_client)
span_id = "1234567890abcdef"
trace_id = "1234567890abcdef1234567890abcdef"
fake_trace_context = {
"bla": "blub",
"foo": "bar",
"baz": 123,
}
sentry_span = MagicMock()
sentry_span.get_trace_context = MagicMock(return_value=fake_trace_context)
otel_span_map = {
span_id: sentry_span,
}
span_context = SpanContext(
trace_id=int(trace_id, 16),
span_id=int(span_id, 16),
is_remote=True,
)
otel_span = MagicMock()
otel_span.get_span_context = MagicMock(return_value=span_context)
fake_event = {"event_id": "1234567890abcdef1234567890abcdef"}
with mock.patch(
"sentry_sdk.integrations.opentelemetry.span_processor.get_current_span",
return_value=otel_span,
):
event = link_trace_context_to_error_event(fake_event, otel_span_map)
assert event
assert event == fake_event # the event is changed in place inside the function
assert "contexts" in event
assert "trace" in event["contexts"]
assert event["contexts"]["trace"] == fake_trace_context
def test_pruning_old_spans_on_start():
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.start_time = time.time_ns()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = MagicMock()
otel_span.parent.span_id = int("abcdef1234567890", 16)
parent_context = {}
fake_client = MagicMock()
fake_client.options = {"instrumenter": "otel", "debug": False}
fake_client.dsn = "https://[email protected]/123456"
sentry_sdk.get_global_scope().set_client(fake_client)
span_processor = SentrySpanProcessor()
span_processor.otel_span_map = {
"111111111abcdef": MagicMock(), # should stay
"2222222222abcdef": MagicMock(), # should go
"3333333333abcdef": MagicMock(), # should go
}
current_time_minutes = int(time.time() / 60)
span_processor.open_spans = {
current_time_minutes - 3: {"111111111abcdef"}, # should stay
current_time_minutes
- 11: {"2222222222abcdef", "3333333333abcdef"}, # should go
}
span_processor.on_start(otel_span, parent_context)
assert sorted(list(span_processor.otel_span_map.keys())) == [
"111111111abcdef",
"1234567890abcdef",
]
assert sorted(list(span_processor.open_spans.values())) == [
{"111111111abcdef"},
{"1234567890abcdef"},
]
def test_pruning_old_spans_on_end():
otel_span = MagicMock()
otel_span.name = "Sample OTel Span"
otel_span.start_time = time.time_ns()
span_context = SpanContext(
trace_id=int("1234567890abcdef1234567890abcdef", 16),
span_id=int("1234567890abcdef", 16),
is_remote=True,
)
otel_span.get_span_context.return_value = span_context
otel_span.parent = MagicMock()
otel_span.parent.span_id = int("abcdef1234567890", 16)
fake_client = MagicMock()
fake_client.options = {"instrumenter": "otel"}
sentry_sdk.get_global_scope().set_client(fake_client)
fake_sentry_span = MagicMock(spec=Span)
fake_sentry_span.set_context = MagicMock()
fake_sentry_span.finish = MagicMock()
span_processor = SentrySpanProcessor()
span_processor._get_otel_context = MagicMock()
span_processor._update_span_with_otel_data = MagicMock()
span_processor.otel_span_map = {
"111111111abcdef": MagicMock(), # should stay
"2222222222abcdef": MagicMock(), # should go
"3333333333abcdef": MagicMock(), # should go
"1234567890abcdef": fake_sentry_span, # should go (because it is closed)
}
current_time_minutes = int(time.time() / 60)
span_processor.open_spans = {
current_time_minutes: {"1234567890abcdef"}, # should go (because it is closed)
current_time_minutes - 3: {"111111111abcdef"}, # should stay
current_time_minutes
- 11: {"2222222222abcdef", "3333333333abcdef"}, # should go
}
span_processor.on_end(otel_span)
assert sorted(list(span_processor.otel_span_map.keys())) == ["111111111abcdef"]
assert sorted(list(span_processor.open_spans.values())) == [{"111111111abcdef"}]