From 499f67ed7edea494876b54520a95e2d4041444f1 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Fri, 17 Jul 2020 20:40:22 -0600 Subject: [PATCH] Add test case Fixes #161 --- .../opentracing_shim/__init__.py | 5 ++++ .../tests/test_shim.py | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py index 6c76444e6ee..8660662d34b 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/src/opentelemetry/instrumentation/opentracing_shim/__init__.py @@ -570,6 +570,11 @@ def start_active_span( :class:`ScopeManagerShim`. """ + current_span = get_current_span() + + if child_of is None and current_span is not None: + child_of = SpanShim(None, None, current_span) + span = self.start_span( operation_name=operation_name, child_of=child_of, diff --git a/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py b/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py index 2906b5803cb..c880913a877 100644 --- a/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py +++ b/instrumentation/opentelemetry-instrumentation-opentracing-shim/tests/test_shim.py @@ -591,3 +591,31 @@ def test_active(self): # Verify no span is active. self.assertIsNone(self.shim.active_span) + + def test_mixed_mode(self): + """Test that span parent-child relationship is kept between + OpenTelemetry and the OpenTracing shim""" + + span_shim = self.shim.start_span("TestSpan16") + + with self.shim.scope_manager.activate(span_shim, finish_on_close=True): + + with ( + TracerProvider() + .get_tracer(__name__) + .start_as_current_span("abc") + ) as opentelemetry_span: + + self.assertIs( + span_shim.unwrap().context, opentelemetry_span.parent, + ) + + with ( + TracerProvider().get_tracer(__name__).start_as_current_span("abc") + ) as opentelemetry_span: + + with self.shim.start_active_span("TestSpan17") as scope: + + self.assertIs( + scope.span.unwrap().parent, opentelemetry_span.context, + )