From f2b22885ad5b98aa09d600cab8937d70956c59e7 Mon Sep 17 00:00:00 2001 From: Ashutosh Goel Date: Thu, 17 Feb 2022 19:01:34 +0530 Subject: [PATCH] Passing response headers in correct format in pyramid Fixing lint errors --- .../instrumentation/pyramid/callbacks.py | 2 +- .../instrumentation/wsgi/__init__.py | 4 +--- .../src/opentelemetry/util/http/__init__.py | 10 ++++----- .../tests/test_capture_custom_headers.py | 21 ++++++++++++------- 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py index 8dc09cae5e..cc424eb0d9 100644 --- a/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py +++ b/instrumentation/opentelemetry-instrumentation-pyramid/src/opentelemetry/instrumentation/pyramid/callbacks.py @@ -161,7 +161,7 @@ def trace_tween(request): otel_wsgi.add_response_attributes( span, response_or_exception.status, - response_or_exception.headers, + response_or_exception.headerlist, ) propagator = get_global_response_propagator() diff --git a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py index f1cbbfebd6..9253251eba 100644 --- a/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-wsgi/src/opentelemetry/instrumentation/wsgi/__init__.py @@ -229,9 +229,7 @@ def capture_custom_request_headers(environ, attributes): ) for header_name in custom_request_headers: wsgi_env_var = header_name.upper().replace("-", "_") - header_values = environ.get( - "HTTP_{wsgi_env_var}".format(wsgi_env_var=wsgi_env_var) - ) + header_values = environ.get(f"HTTP_{wsgi_env_var}") if header_values: key = normalise_request_header_name(header_name) diff --git a/util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py b/util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py index 7d53898008..c8b56403c9 100644 --- a/util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py +++ b/util/opentelemetry-util-http/src/opentelemetry/util/http/__init__.py @@ -104,15 +104,13 @@ def remove_url_credentials(url: str) -> str: def normalise_request_header_name(header): - return "http.request.header.{key}".format( - key=header.lower().replace("-", "_") - ) + key = header.lower().replace("-", "_") + return f"http.request.header.{key}" def normalise_response_header_name(header): - return "http.response.header.{key}".format( - key=header.lower().replace("-", "_") - ) + key = header.lower().replace("-", "_") + return f"http.response.header.{key}" def get_custom_headers(env_var): diff --git a/util/opentelemetry-util-http/tests/test_capture_custom_headers.py b/util/opentelemetry-util-http/tests/test_capture_custom_headers.py index b07f50eeaf..eeaf899691 100644 --- a/util/opentelemetry-util-http/tests/test_capture_custom_headers.py +++ b/util/opentelemetry-util-http/tests/test_capture_custom_headers.py @@ -33,7 +33,9 @@ def test_get_custom_request_header(self): custom_headers_to_capture = get_custom_headers( OTEL_PYTHON_CAPTURE_REQUEST_HEADERS ) - assert custom_headers_to_capture == ["User-Agent", "Test-Header"] + self.assertEqual( + custom_headers_to_capture, ["User-Agent", "Test-Header"] + ) @patch.dict( "os.environ", @@ -45,16 +47,19 @@ def test_get_custom_response_header(self): custom_headers_to_capture = get_custom_headers( OTEL_PYTHON_CAPTURE_RESPONSE_HEADERS ) - assert custom_headers_to_capture == [ - "content-type", - "content-length", - "test-header", - ] + self.assertEqual( + custom_headers_to_capture, + [ + "content-type", + "content-length", + "test-header", + ], + ) def test_normalise_request_header_name(self): key = normalise_request_header_name("Test-Header") - assert key == "http.request.header.test_header" + self.assertEqual(key, "http.request.header.test_header") def test_normalise_response_header_name(self): key = normalise_response_header_name("Test-Header") - assert key == "http.response.header.test_header" + self.assertEqual(key, "http.response.header.test_header")