diff --git a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py index d4653ac50b..53bdb10938 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py @@ -251,17 +251,15 @@ def get( # ASGI header keys are in lower case key = key.lower() decoded = [ - _value.decode("utf8") + _decode_header_item(_value) for (_key, _value) in headers - if _key.decode("utf8").lower() == key + if _decode_header_item(_key).lower() == key ] - if not decoded: - return None - return decoded + return decoded or None def keys(self, carrier: dict) -> typing.List[str]: headers = carrier.get("headers") or [] - return [_key.decode("utf8") for (_key, _value) in headers] + return [_decode_header_item(_key) for (_key, _value) in headers] asgi_getter = ASGIGetter() @@ -344,10 +342,7 @@ def collect_custom_request_headers_attributes(scope): ) # Decode headers before processing. - headers = { - _key.decode("utf8"): _value.decode("utf8") - for (_key, _value) in scope.get("headers") - } + headers = _decode_headers(scope.get("headers")) return sanitize.sanitize_header_values( headers, @@ -370,10 +365,7 @@ def collect_custom_response_headers_attributes(message): ) # Decode headers before processing. - headers = { - _key.decode("utf8"): _value.decode("utf8") - for (_key, _value) in message.get("headers") - } + headers = _decode_headers(message.get("headers")) return sanitize.sanitize_header_values( headers, @@ -657,3 +649,17 @@ async def otel_send(message): await send(message) return otel_send + + +def _decode_headers(headers): + return { + _decode_header_item(key): _decode_header_item(value) + for key, value in headers + } + + +def _decode_header_item(value): + try: + return value.decode("utf-8") + except ValueError: + return value.decode("latin-1")