Skip to content

Commit

Permalink
Passing response headers in correct format in pyramid
Browse files Browse the repository at this point in the history
Fixing lint errors
  • Loading branch information
ashu658 committed Feb 17, 2022
1 parent 57126e2 commit f2b2288
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
21 changes: 13 additions & 8 deletions util/opentelemetry-util-http/tests/test_capture_custom_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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")

0 comments on commit f2b2288

Please sign in to comment.