Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changelog/5241.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Preserve the random trace ID flag when creating child spans instead of always setting the random trace id bit depending on the available trace id generator.
7 changes: 6 additions & 1 deletion opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,12 @@ def start_span( # pylint: disable=too-many-locals
else trace_api.TraceFlags(trace_api.TraceFlags.DEFAULT)
)

if self.id_generator.is_trace_id_random():
if parent_span_context is None:
random_trace_id = self.id_generator.is_trace_id_random()
else:
random_trace_id = parent_span_context.trace_flags.random_trace_id

if random_trace_id:
trace_flags = trace_api.TraceFlags(
trace_flags | trace_api.TraceFlags.RANDOM_TRACE_ID
)
Expand Down
29 changes: 29 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,35 @@ def test_start_span_explicit(self):
self.assertIs(trace_api.get_current_span(), root)
self.assertIsNotNone(child.end_time)

def test_start_span_preserves_parent_random_trace_id_flag(self):
tracer = new_tracer()

for parent_trace_flags in (
trace_api.TraceFlags(trace_api.TraceFlags.SAMPLED),
trace_api.TraceFlags(
trace_api.TraceFlags.SAMPLED
| trace_api.TraceFlags.RANDOM_TRACE_ID
),
):
with self.subTest(parent_trace_flags=parent_trace_flags):
parent_context = trace_api.SpanContext(
trace_id=0x000000000000000000000000DEADBEEF,
span_id=0x00000000DEADBEF0,
is_remote=True,
trace_flags=parent_trace_flags,
)
context = trace_api.set_span_in_context(
trace_api.NonRecordingSpan(parent_context)
)

child = tracer.start_span("child", context)
child_trace_flags = child.get_span_context().trace_flags

self.assertEqual(
parent_trace_flags.random_trace_id,
child_trace_flags.random_trace_id,
)

def test_start_as_current_span_implicit(self):
tracer = new_tracer()

Expand Down
Loading