mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-29 21:23:55 +08:00
[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
This commit is contained in:
@ -313,7 +313,7 @@ class OpenTelemetryMiddleware:
|
|||||||
@functools.wraps(start_response)
|
@functools.wraps(start_response)
|
||||||
def _start_response(status, response_headers, *args, **kwargs):
|
def _start_response(status, response_headers, *args, **kwargs):
|
||||||
add_response_attributes(span, status, response_headers)
|
add_response_attributes(span, status, response_headers)
|
||||||
if span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
add_custom_response_headers(span, response_headers)
|
add_custom_response_headers(span, response_headers)
|
||||||
if response_hook:
|
if response_hook:
|
||||||
response_hook(status, response_headers)
|
response_hook(status, response_headers)
|
||||||
@ -336,7 +336,7 @@ class OpenTelemetryMiddleware:
|
|||||||
context_getter=wsgi_getter,
|
context_getter=wsgi_getter,
|
||||||
attributes=collect_request_attributes(environ),
|
attributes=collect_request_attributes(environ),
|
||||||
)
|
)
|
||||||
if span.kind == trace.SpanKind.SERVER:
|
if span.is_recording() and span.kind == trace.SpanKind.SERVER:
|
||||||
add_custom_request_headers(span, environ)
|
add_custom_request_headers(span, environ)
|
||||||
|
|
||||||
if self.request_hook:
|
if self.request_hook:
|
||||||
|
@ -475,6 +475,29 @@ class TestAdditionOfCustomRequestResponseHeaders(WsgiTestBase, TestBase):
|
|||||||
except StopIteration:
|
except StopIteration:
|
||||||
break
|
break
|
||||||
|
|
||||||
|
@mock.patch.dict(
|
||||||
|
"os.environ",
|
||||||
|
{
|
||||||
|
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST: "Custom-Test-Header-1,Custom-Test-Header-2,Custom-Test-Header-3",
|
||||||
|
},
|
||||||
|
)
|
||||||
|
def test_custom_request_headers_non_recording_span(self):
|
||||||
|
try:
|
||||||
|
tracer_provider = trace_api.NoOpTracerProvider()
|
||||||
|
self.environ.update(
|
||||||
|
{
|
||||||
|
"HTTP_CUSTOM_TEST_HEADER_1": "Test Value 2",
|
||||||
|
"HTTP_CUSTOM_TEST_HEADER_2": "TestValue2,TestValue3",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
app = otel_wsgi.OpenTelemetryMiddleware(
|
||||||
|
simple_wsgi, tracer_provider=tracer_provider
|
||||||
|
)
|
||||||
|
response = app(self.environ, self.start_response)
|
||||||
|
self.iterate_response(response)
|
||||||
|
except Exception as exc: # pylint: disable=W0703
|
||||||
|
self.fail(f"Exception raised with NonRecordingSpan {exc}")
|
||||||
|
|
||||||
@mock.patch.dict(
|
@mock.patch.dict(
|
||||||
"os.environ",
|
"os.environ",
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user