Case insensitive header key retrieval for asgi instrumentation (#308)

This commit is contained in:
Garner Jervis Tan
2021-02-06 01:51:06 +09:00
committed by GitHub
parent b53b9a012f
commit 55efeb6063
3 changed files with 57 additions and 0 deletions

View File

@ -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

View File

@ -47,6 +47,11 @@ class CarrierGetter(DictGetter):
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

View File

@ -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, [])