diff --git a/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py b/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py index 5e619eb7c6..5fb58a5dd2 100644 --- a/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py +++ b/ext/opentelemetry-ext-wsgi/src/opentelemetry/ext/wsgi/__init__.py @@ -108,6 +108,9 @@ def __call__(self, environ, start_response): tracer = trace.tracer() path_info = environ["PATH_INFO"] or "/" parent_span = propagators.extract(_get_header_from_environ, environ) + # if extracing fails then use current span as parent + if parent_span is trace.INVALID_SPAN_CONTEXT: + parent_span = tracer.CURRENT_SPAN span = tracer.create_span( path_info, parent_span, kind=trace.SpanKind.SERVER diff --git a/ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py b/ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py index a88782d642..16f43a0adf 100644 --- a/ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py +++ b/ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py @@ -75,10 +75,10 @@ def error_wsgi(environ, start_response): class TestWsgiApplication(unittest.TestCase): def setUp(self): - tracer = trace_api.tracer() + self.tracer = trace_api.tracer() self.span = mock.create_autospec(trace_api.Span, spec_set=True) self.create_span_patcher = mock.patch.object( - tracer, + self.tracer, "create_span", autospec=True, spec_set=True, @@ -131,7 +131,7 @@ def validate_response(self, response, error=None): # Verify that start_span has been called self.create_span.assert_called_with( - "/", trace_api.INVALID_SPAN_CONTEXT, kind=trace_api.SpanKind.SERVER + "/", self.tracer.CURRENT_SPAN, kind=trace_api.SpanKind.SERVER ) self.span.start.assert_called_with()