Skip to content

Commit fcba751

Browse files
author
Alex Boten
authored
[instrumentation/wsgi] fix NonRecordingSpan bug (#957)
* [instrumentation/wsgi] fix NonRecordingSpan bug There was a bug caused by accessing `.kind` on a NonRecordingSpan. Added a test to validate the fix. Fix #956 * fix lint * use is_recording * fix lint * fix lint * fix lint
1 parent 1bf9e0c commit fcba751

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ def _create_start_response(span, start_response, response_hook):
313313
@functools.wraps(start_response)
314314
def _start_response(status, response_headers, *args, **kwargs):
315315
add_response_attributes(span, status, response_headers)
316-
if span.kind == trace.SpanKind.SERVER:
316+
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
317317
add_custom_response_headers(span, response_headers)
318318
if response_hook:
319319
response_hook(status, response_headers)
@@ -336,7 +336,7 @@ def __call__(self, environ, start_response):
336336
context_getter=wsgi_getter,
337337
attributes=collect_request_attributes(environ),
338338
)
339-
if span.kind == trace.SpanKind.SERVER:
339+
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
340340
add_custom_request_headers(span, environ)
341341

342342
if self.request_hook:

instrumentation/opentelemetry-instrumentation-wsgi/tests/test_wsgi_middleware.py

+23
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,29 @@ def iterate_response(self, response):
475475
except StopIteration:
476476
break
477477

478+
@mock.patch.dict(
479+
"os.environ",
480+
{
481+
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3",
482+
},
483+
)
484+
def test_custom_request_headers_non_recording_span(self):
485+
try:
486+
tracer_provider = trace_api.NoOpTracerProvider()
487+
self.environ.update(
488+
{
489+
"HTTP_CUSTOM_TEST_HEADER_1": "Test Value 2",
490+
"HTTP_CUSTOM_TEST_HEADER_2": "TestValue2,TestValue3",
491+
}
492+
)
493+
app = otel_wsgi.OpenTelemetryMiddleware(
494+
simple_wsgi, tracer_provider=tracer_provider
495+
)
496+
response = app(self.environ, self.start_response)
497+
self.iterate_response(response)
498+
except Exception as exc: # pylint: disable=W0703
499+
self.fail(f"Exception raised with NonRecordingSpan {exc}")
500+
478501
@mock.patch.dict(
479502
"os.environ",
480503
{

0 commit comments

Comments
 (0)