Skip to content

Commit 55efeb6

Browse files
authored
Case insensitive header key retrieval for asgi instrumentation (#308)
1 parent b53b9a0 commit 55efeb6

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ 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
14+
([#308](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/308))
1315

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

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

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ def get(
4747
else None.
4848
"""
4949
headers = carrier.get("headers")
50+
if not headers:
51+
return None
52+
53+
# asgi header keys are in lower case
54+
key = key.lower()
5055
decoded = [
5156
_value.decode("utf8")
5257
for (_key, _value) in headers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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(
32+
getter.get(carrier, "Test-Key"),
33+
expected_val,
34+
"Should be case insensitive",
35+
)
36+
self.assertEqual(
37+
getter.get(carrier, "test-key"),
38+
expected_val,
39+
"Should be case insensitive",
40+
)
41+
self.assertEqual(
42+
getter.get(carrier, "TEST-KEY"),
43+
expected_val,
44+
"Should be case insensitive",
45+
)
46+
47+
def test_keys(self):
48+
getter = CarrierGetter()
49+
keys = getter.keys({})
50+
self.assertEqual(keys, [])

0 commit comments

Comments
 (0)