Skip to content

Commit 3341a8e

Browse files
authored
instrumentation/opentracing-shim: Preserve parent-child relationship between OpenTelemetry and OpenTracing spans (open-telemetry#924)
1 parent e8f7b6f commit 3341a8e

File tree

2 files changed

+33
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-opentracing-shim

2 files changed

+33
-1
lines changed

instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,11 @@ def start_active_span(
570570
:class:`ScopeManagerShim`.
571571
"""
572572

573+
current_span = get_current_span()
574+
575+
if child_of is None and current_span is not INVALID_SPAN_CONTEXT:
576+
child_of = SpanShim(None, None, current_span)
577+
573578
span = self.start_span(
574579
operation_name=operation_name,
575580
child_of=child_of,

instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ def test_extract_binary(self):
557557
self.shim.extract(opentracing.Format.BINARY, bytearray())
558558

559559
def test_baggage(self):
560-
"""Test SpanShim baggage being set and being immutable"""
561560

562561
span_context_shim = SpanContextShim(
563562
trace.SpanContext(1234, 5678, is_remote=False)
@@ -592,3 +591,31 @@ def test_active(self):
592591

593592
# Verify no span is active.
594593
self.assertIsNone(self.shim.active_span)
594+
595+
def test_mixed_mode(self):
596+
"""Test that span parent-child relationship is kept between
597+
OpenTelemetry and the OpenTracing shim"""
598+
599+
span_shim = self.shim.start_span("TestSpan16")
600+
601+
with self.shim.scope_manager.activate(span_shim, finish_on_close=True):
602+
603+
with (
604+
TracerProvider()
605+
.get_tracer(__name__)
606+
.start_as_current_span("abc")
607+
) as opentelemetry_span:
608+
609+
self.assertIs(
610+
span_shim.unwrap().context, opentelemetry_span.parent,
611+
)
612+
613+
with (
614+
TracerProvider().get_tracer(__name__).start_as_current_span("abc")
615+
) as opentelemetry_span:
616+
617+
with self.shim.start_active_span("TestSpan17") as scope:
618+
619+
self.assertIs(
620+
scope.span.unwrap().parent, opentelemetry_span.context,
621+
)

0 commit comments

Comments
 (0)