diff --git a/CHANGELOG.md b/CHANGELOG.md index adfaace015..fbc2a84120 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Remove `component` span attribute in instrumentations. `opentelemetry-instrumentation-aiopg`, `opentelemetry-instrumentation-dbapi` Remove unused `database_type` parameter from `trace_integration` function. ([#301](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/301)) +- `opentelemetry-instrumentation-asgi` Return header values using case insensitive keys ## [0.17b0](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.17b0) - 2021-01-20 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 b81b8b77f7..743001ebf2 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py @@ -48,6 +48,11 @@ def get( else None. """ headers = carrier.get("headers") + if not headers: + return None + + # asgi header keys are in lower case + key = key.lower() decoded = [ _value.decode("utf8") for (_key, _value) in headers diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py new file mode 100644 index 0000000000..fbf4b8ded6 --- /dev/null +++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py @@ -0,0 +1,38 @@ +# Copyright The OpenTelemetry Authors +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from unittest import TestCase + +from opentelemetry.instrumentation.asgi import CarrierGetter + + +class TestCarrierGetter(TestCase): + def test_get_none(self): + getter = CarrierGetter() + carrier = {} + val = getter.get(carrier, "test") + self.assertIsNone(val) + + def test_get_(self): + getter = CarrierGetter() + carrier = {"headers": [(b"test-key", b"val")]} + expected_val = ["val"] + self.assertEqual(getter.get(carrier, "Test-Key"), expected_val, "Should be case insensitive") + self.assertEqual(getter.get(carrier, "test-key"), expected_val, "Should be case insensitive") + self.assertEqual(getter.get(carrier, "TEST-KEY"), expected_val, "Should be case insensitive") + + def test_keys(self): + getter = CarrierGetter() + keys = getter.keys({}) + self.assertEqual(keys, [])