Skip to content

Commit

Permalink
Case insensitive header key retrieval for asgi instrumentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Garner Jervis Tan committed Jan 31, 2021
1 parent f0adb23 commit 67e2c06
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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, [])

0 comments on commit 67e2c06

Please sign in to comment.