Skip to content

Commit ad6cf78

Browse files
committed
ref: Add test for continue_from_headers
1 parent 140c9e2 commit ad6cf78

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

sentry_sdk/hub.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,8 +383,8 @@ def span(self, span=None, **kwargs):
383383
span.finish()
384384
self.finish_trace(span)
385385

386-
def trace(self, **kwargs):
387-
return self.span(self.start_trace(**kwargs))
386+
def trace(self, *args, **kwargs):
387+
return self.span(self.start_trace(*args, **kwargs))
388388

389389
def start_span(self, **kwargs):
390390
_, scope = self._stack[-1]
@@ -393,8 +393,10 @@ def start_span(self, **kwargs):
393393
return span.new_span(**kwargs)
394394
return None
395395

396-
def start_trace(self, **kwargs):
397-
span = Span.start_trace(**kwargs)
396+
def start_trace(self, span=None, **kwargs):
397+
if span is None:
398+
span = Span.start_trace(**kwargs)
399+
398400
_, scope = self._stack[-1]
399401

400402
if scope.span is not None:

sentry_sdk/tracing.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ def from_traceparent(cls, traceparent):
125125
trace_id, span_id, sampled_str = match.groups()
126126

127127
if trace_id is not None:
128-
trace_id = "%x" % (int(trace_id, 16),)
128+
trace_id = "{:032x}".format(int(trace_id, 16))
129129
if span_id is not None:
130-
span_id = "%x" % (int(span_id, 16),)
130+
span_id = "{:016x}".format(int(span_id, 16))
131131

132132
if sampled_str is not None:
133133
sampled = sampled_str != "0"

tests/test_tracing.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

3-
from sentry_sdk import Hub
3+
from sentry_sdk import Hub, capture_message
4+
from sentry_sdk.tracing import Span
45

56

67
@pytest.mark.parametrize("sample_rate", [0.0, 1.0])
@@ -26,3 +27,35 @@ def test_basic(sentry_init, capture_events, sample_rate):
2627
assert parent_span["transaction"] == "hi"
2728
else:
2829
assert not events
30+
31+
32+
def test_continue_from_headers(sentry_init, capture_events):
33+
sentry_init(traces_sample_rate=1.0)
34+
events = capture_events()
35+
36+
with Hub.current.trace(transaction="hi"):
37+
with Hub.current.span() as old_span:
38+
headers = dict(Hub.current.iter_trace_propagation_headers())
39+
40+
span = Span.continue_from_headers(headers)
41+
assert span is not None
42+
assert span.trace_id == old_span.trace_id
43+
44+
with Hub.current.trace(span):
45+
with Hub.current.configure_scope() as scope:
46+
scope.transaction = "ho"
47+
48+
capture_message("hello")
49+
50+
trace1, message, trace2 = events
51+
52+
assert trace1["transaction"] == "hi"
53+
assert trace2["transaction"] == "ho"
54+
55+
assert (
56+
trace1["contexts"]["trace"]["trace_id"]
57+
== trace2["contexts"]["trace"]["trace_id"]
58+
== span.trace_id
59+
== message["contexts"]["trace"]["trace_id"]
60+
)
61+
assert message["message"] == "hello"

0 commit comments

Comments
 (0)