From 67e2c0647660303356c973ec013b3fdd712e3440 Mon Sep 17 00:00:00 2001 From: Garner Jervis Tan Date: Sun, 31 Jan 2021 19:40:33 +0900 Subject: [PATCH 1/3] Case insensitive header key retrieval for asgi instrumentation --- CHANGELOG.md | 1 + .../instrumentation/asgi/__init__.py | 5 +++ .../tests/test_getter.py | 38 +++++++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py 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, []) From aad89817055e6791b46d5ee6e4d477d980c57a57 Mon Sep 17 00:00:00 2001 From: Garner Jervis Tan Date: Mon, 1 Feb 2021 10:15:34 +0900 Subject: [PATCH 2/3] Fixed lint errors --- .../tests/test_getter.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py index fbf4b8ded6..c7a5f1ef53 100644 --- a/instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py +++ b/instrumentation/opentelemetry-instrumentation-asgi/tests/test_getter.py @@ -28,9 +28,21 @@ 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") + 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() From 58f04e942f440d0184c67e0e28f50da09fb00e30 Mon Sep 17 00:00:00 2001 From: Garner Jervis Tan Date: Tue, 2 Feb 2021 10:12:15 +0900 Subject: [PATCH 3/3] Added pull request link in changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fbc2a84120..c24dcef50f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `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 + ([#308](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/308)) ## [0.17b0](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.17b0) - 2021-01-20