Skip to content

Commit ba906fe

Browse files
committed
Addressed feedback
1 parent 1d9ea2e commit ba906fe

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ def get_host_port_url_tuple(scope):
451451
host_value = host_header[0]
452452
# Ensure host_value is a string, not bytes
453453
if isinstance(host_value, bytes):
454-
host_value = host_value.decode("utf-8")
454+
host_value = _decode_header_item(host_value)
455455

456456
url_host = host_value
457457

instrumentation/opentelemetry-instrumentation-fastapi/tests/test_fastapi_instrumentation.py

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,7 +1896,7 @@ def test_host_header_url_construction(self):
18961896
# Find the server span (the main span, not internal middleware spans)
18971897
server_span = None
18981898
for span in spans:
1899-
if HTTP_URL in span.attributes:
1899+
if span.kind == trace.SpanKind.SERVER and HTTP_URL in span.attributes:
19001900
server_span = span
19011901
break
19021902

@@ -1923,7 +1923,7 @@ def test_host_header_with_port_url_construction(self):
19231923

19241924
spans = self.memory_exporter.get_finished_spans()
19251925
server_span = next(
1926-
(span for span in spans if HTTP_URL in span.attributes), None
1926+
(span for span in spans if span.kind == trace.SpanKind.SERVER and HTTP_URL in span.attributes), None
19271927
)
19281928
self.assertIsNotNone(server_span)
19291929

@@ -1940,7 +1940,7 @@ def test_no_host_header_fallback_behavior(self):
19401940

19411941
spans = self.memory_exporter.get_finished_spans()
19421942
server_span = next(
1943-
(span for span in spans if HTTP_URL in span.attributes), None
1943+
(span for span in spans if span.kind == trace.SpanKind.SERVER and HTTP_URL in span.attributes), None
19441944
)
19451945
self.assertIsNotNone(server_span)
19461946

@@ -1965,7 +1965,7 @@ def test_production_scenario_host_header(self):
19651965

19661966
spans = self.memory_exporter.get_finished_spans()
19671967
server_span = next(
1968-
(span for span in spans if HTTP_URL in span.attributes), None
1968+
(span for span in spans if span.kind == trace.SpanKind.SERVER and HTTP_URL in span.attributes), None
19691969
)
19701970
self.assertIsNotNone(server_span)
19711971

@@ -2012,33 +2012,13 @@ def test_host_header_with_special_characters(self):
20122012

20132013
spans = self.memory_exporter.get_finished_spans()
20142014
server_span = next(
2015-
(span for span in spans if HTTP_URL in span.attributes),
2015+
(span for span in spans if span.kind == trace.SpanKind.SERVER and HTTP_URL in span.attributes),
20162016
None,
20172017
)
20182018
self.assertIsNotNone(server_span)
2019-
20202019
actual_url = server_span.attributes[HTTP_URL]
20212020
self.assertEqual(expected_url, actual_url)
20222021

2023-
def test_host_header_bytes_handling(self):
2024-
"""Test that Host header values are properly decoded from bytes."""
2025-
# This test verifies the fix for bytes vs string handling in our implementation
2026-
resp = self._client.get(
2027-
"/foobar", headers={"host": "bytes-test.example.com"}
2028-
)
2029-
self.assertEqual(200, resp.status_code)
2030-
2031-
spans = self.memory_exporter.get_finished_spans()
2032-
server_span = next(
2033-
(span for span in spans if HTTP_URL in span.attributes), None
2034-
)
2035-
self.assertIsNotNone(server_span)
2036-
2037-
# Should properly decode and use the host header
2038-
expected_url = "https://bytes-test.example.com/foobar"
2039-
actual_url = server_span.attributes[HTTP_URL]
2040-
self.assertEqual(expected_url, actual_url)
2041-
20422022
def test_host_header_maintains_span_attributes(self):
20432023
"""Test that using Host header doesn't break other span attributes."""
20442024
resp = self._client.get(
@@ -2052,7 +2032,7 @@ def test_host_header_maintains_span_attributes(self):
20522032

20532033
spans = self.memory_exporter.get_finished_spans()
20542034
server_span = next(
2055-
(span for span in spans if HTTP_URL in span.attributes), None
2035+
(span for span in spans if span.kind == trace.SpanKind.SERVER and HTTP_URL in span.attributes), None
20562036
)
20572037
self.assertIsNotNone(server_span)
20582038

@@ -2096,7 +2076,7 @@ def test_host_header_url_new_semconv(self):
20962076
spans = self.memory_exporter.get_finished_spans()
20972077
# With new semantic conventions, look for the main HTTP span with route information
20982078
server_span = next(
2099-
(span for span in spans if "http.route" in span.attributes), None
2079+
(span for span in spans if span.kind == trace.SpanKind.SERVER and "http.route" in span.attributes), None
21002080
)
21012081
self.assertIsNotNone(server_span)
21022082

@@ -2110,7 +2090,7 @@ def test_host_header_url_new_semconv(self):
21102090
# Current behavior: Host header may not affect server.address in new semantic conventions
21112091
# This test documents the current behavior rather than enforcing Host header usage
21122092
server_address = server_span.attributes.get("server.address", "")
2113-
self.assertIsNotNone(server_address) # Should have some value
2093+
self.assertIsNotNone(server_address, "testserver") # Should have some value
21142094

21152095

21162096
class TestFastAPIHostHeaderURLBothSemconv(TestFastAPIHostHeaderURL):
@@ -2125,7 +2105,7 @@ def test_host_header_url_both_semconv(self):
21252105

21262106
spans = self.memory_exporter.get_finished_spans()
21272107
server_span = next(
2128-
(span for span in spans if HTTP_URL in span.attributes), None
2108+
(span for span in spans if span.kind == trace.SpanKind.SERVER and HTTP_URL in span.attributes), None
21292109
)
21302110
self.assertIsNotNone(server_span)
21312111

@@ -2163,7 +2143,7 @@ def test_fastapi_unhandled_exception_both_semconv(self):
21632143
server_spans = [
21642144
span
21652145
for span in spans
2166-
if hasattr(span, "attributes")
2146+
if span.kind == trace.SpanKind.SERVER and hasattr(span, "attributes")
21672147
and span.attributes
21682148
and HTTP_URL in span.attributes
21692149
]

0 commit comments

Comments
 (0)