Skip to content

Commit 118f259

Browse files
committed
Add latin-1 fallback decoding for asgi headers
1 parent 161acbb commit 118f259

File tree

1 file changed

+13
-4
lines changed
  • instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi

1 file changed

+13
-4
lines changed

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,17 +284,17 @@ def get(
284284
# ASGI header keys are in lower case
285285
key = key.lower()
286286
decoded = [
287-
_value.decode("utf8")
287+
_decode_header_item(_value)
288288
for (_key, _value) in headers
289-
if _key.decode("utf8").lower() == key
289+
if _decode_header_item(_key).lower() == key
290290
]
291291
if not decoded:
292292
return None
293293
return decoded
294294

295295
def keys(self, carrier: dict) -> typing.List[str]:
296296
headers = carrier.get("headers") or []
297-
return [_key.decode("utf8") for (_key, _value) in headers]
297+
return [_decode_header_item(_key) for (_key, _value) in headers]
298298

299299

300300
asgi_getter = ASGIGetter()
@@ -410,7 +410,9 @@ def collect_custom_headers_attributes(
410410
if raw_headers:
411411
for key, value in raw_headers:
412412
# Decode headers before processing.
413-
headers[key.decode()].append(value.decode())
413+
headers[_decode_header_item(key)].append(
414+
_decode_header_item(value)
415+
)
414416

415417
return sanitize.sanitize_header_values(
416418
headers,
@@ -937,3 +939,10 @@ def _parse_active_request_count_attrs(
937939
_server_active_requests_count_attrs_new,
938940
sem_conv_opt_in_mode,
939941
)
942+
943+
944+
def _decode_header_item(value):
945+
try:
946+
return value.decode("utf-8")
947+
except ValueError:
948+
return value.decode("latin-1")

0 commit comments

Comments
 (0)