Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 67e2c06

Browse files
author
Garner Jervis Tan
committedJan 31, 2021
Case insensitive header key retrieval for asgi instrumentation
1 parent f0adb23 commit 67e2c06

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Remove `component` span attribute in instrumentations.
1111
`opentelemetry-instrumentation-aiopg`, `opentelemetry-instrumentation-dbapi` Remove unused `database_type` parameter from `trace_integration` function.
1212
([#301](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/301))
13+
- `opentelemetry-instrumentation-asgi` Return header values using case insensitive keys
1314

1415
## [0.17b0](https://github.com/open-telemetry/opentelemetry-python-contrib/releases/tag/v0.17b0) - 2021-01-20
1516

‎instrumentation/opentelemetry-instrumentation-asgi/src/opentelemetry/instrumentation/asgi/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ def get(
4848
else None.
4949
"""
5050
headers = carrier.get("headers")
51+
if not headers:
52+
return None
53+
54+
# asgi header keys are in lower case
55+
key = key.lower()
5156
decoded = [
5257
_value.decode("utf8")
5358
for (_key, _value) in headers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright The OpenTelemetry Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest import TestCase
16+
17+
from opentelemetry.instrumentation.asgi import CarrierGetter
18+
19+
20+
class TestCarrierGetter(TestCase):
21+
def test_get_none(self):
22+
getter = CarrierGetter()
23+
carrier = {}
24+
val = getter.get(carrier, "test")
25+
self.assertIsNone(val)
26+
27+
def test_get_(self):
28+
getter = CarrierGetter()
29+
carrier = {"headers": [(b"test-key", b"val")]}
30+
expected_val = ["val"]
31+
self.assertEqual(getter.get(carrier, "Test-Key"), expected_val, "Should be case insensitive")
32+
self.assertEqual(getter.get(carrier, "test-key"), expected_val, "Should be case insensitive")
33+
self.assertEqual(getter.get(carrier, "TEST-KEY"), expected_val, "Should be case insensitive")
34+
35+
def test_keys(self):
36+
getter = CarrierGetter()
37+
keys = getter.keys({})
38+
self.assertEqual(keys, [])

0 commit comments

Comments
 (0)
Please sign in to comment.