Skip to content

Commit

Permalink
Case insensitive header key retrieval for asgi instrumentation (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
garnertan authored Feb 5, 2021
1 parent b53b9a0 commit 55efeb6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ 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
([#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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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,50 @@
# 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 55efeb6

Please sign in to comment.