Skip to content

Commit 2383afa

Browse files
committed
continue_trace tests
1 parent c0bf515 commit 2383afa

1 file changed

Lines changed: 137 additions & 0 deletions

File tree

tests/tracing/test_span_streaming.py

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1+
import asyncio
12
import time
3+
import sys
24
from unittest import mock
35

6+
import pytest
7+
48
import sentry_sdk
59

10+
minimum_python_38 = pytest.mark.skipif(
11+
sys.version_info < (3, 8), reason="Asyncio tests need Python >= 3.8"
12+
)
13+
614

715
def envelopes_to_spans(envelopes):
816
res = [] # type: List[Metric]
@@ -322,6 +330,135 @@ def test_sibling_segments_new_trace(sentry_init, capture_envelopes):
322330
assert segment1["trace_id"] != segment2["trace_id"]
323331

324332

333+
def test_continue_trace_sampled(sentry_init, capture_envelopes):
334+
sentry_init(
335+
# parent sampling decision takes precedence over traces_sample_rate
336+
traces_sample_rate=0.0,
337+
_experiments={"trace_lifecycle": "stream"},
338+
)
339+
340+
events = capture_envelopes()
341+
342+
trace_id = "0af7651916cd43dd8448eb211c80319c"
343+
parent_span_id = "b7ad6b7169203331"
344+
sample_rand = "0.222222"
345+
sampled = "1"
346+
347+
sentry_sdk.traces.continue_trace(
348+
{
349+
"sentry-trace": f"{trace_id}-{parent_span_id}-{sampled}",
350+
"baggage": f"sentry-trace_id={trace_id},sentry-sample_rate=0.5,sentry-sample_rand={sample_rand}",
351+
}
352+
)
353+
354+
with sentry_sdk.traces.start_span(name="segment") as span:
355+
...
356+
357+
assert span.sampled is True
358+
assert span.trace_id == trace_id
359+
assert span.parent_span_id == parent_span_id
360+
assert span._sample_rand == float(sample_rand)
361+
362+
sentry_sdk.get_client().flush()
363+
spans = envelopes_to_spans(events)
364+
365+
assert len(spans) == 1
366+
(segment,) = spans
367+
368+
assert segment["is_segment"] is True
369+
assert segment["parent_span_id"] == parent_span_id
370+
assert segment["trace_id"] == trace_id
371+
372+
373+
def test_continue_trace_unsampled(sentry_init, capture_envelopes):
374+
sentry_init(
375+
# parent sampling decision takes precedence over traces_sample_rate
376+
traces_sample_rate=1.0,
377+
_experiments={"trace_lifecycle": "stream"},
378+
)
379+
380+
events = capture_envelopes()
381+
382+
trace_id = "0af7651916cd43dd8448eb211c80319c"
383+
parent_span_id = "b7ad6b7169203331"
384+
sample_rand = "0.999999"
385+
sampled = "0"
386+
387+
sentry_sdk.traces.continue_trace(
388+
{
389+
"sentry-trace": f"{trace_id}-{parent_span_id}-{sampled}",
390+
"baggage": f"sentry-trace_id={trace_id},sentry-sample_rate=0.5,sentry-sample_rand={sample_rand}",
391+
}
392+
)
393+
394+
with sentry_sdk.traces.start_span(name="segment") as span:
395+
...
396+
397+
assert span.sampled is False
398+
assert span.trace_id == trace_id
399+
assert span.parent_span_id == parent_span_id
400+
assert span._sample_rand == float(sample_rand)
401+
402+
sentry_sdk.get_client().flush()
403+
spans = envelopes_to_spans(events)
404+
405+
assert len(spans) == 0
406+
407+
408+
def test_trace_decorator(sentry_init, capture_envelopes):
409+
sentry_init(
410+
traces_sample_rate=1.0,
411+
_experiments={"trace_lifecycle": "stream"},
412+
)
413+
414+
events = capture_envelopes()
415+
416+
@sentry_sdk.traces.trace
417+
def traced_function(): ...
418+
419+
traced_function()
420+
421+
sentry_sdk.get_client().flush()
422+
spans = envelopes_to_spans(events)
423+
424+
assert len(spans) == 1
425+
(span,) = spans
426+
427+
assert (
428+
span["name"]
429+
== "test_span_streaming.test_trace_decorator.<locals>.traced_function"
430+
)
431+
assert span["status"] == "ok"
432+
433+
434+
@minimum_python_38
435+
@pytest.mark.asyncio
436+
def test_trace_decorator_async(sentry_init, capture_envelopes):
437+
sentry_init(
438+
traces_sample_rate=1.0,
439+
_experiments={"trace_lifecycle": "stream"},
440+
)
441+
442+
events = capture_envelopes()
443+
444+
@sentry_sdk.traces.trace
445+
async def traced_function(): ...
446+
447+
asyncio.run(traced_function())
448+
449+
sentry_sdk.get_client().flush()
450+
spans = envelopes_to_spans(events)
451+
452+
assert len(spans) == 1
453+
(span,) = spans
454+
455+
assert (
456+
span["name"]
457+
== "test_span_streaming.test_trace_decorator_async.<locals>.traced_function"
458+
)
459+
assert span["status"] == "ok"
460+
461+
325462
def test_transport_format(sentry_init, capture_envelopes):
326463
sentry_init(
327464
server_name="test-server",

0 commit comments

Comments
 (0)