-
Notifications
You must be signed in to change notification settings - Fork 651
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
176 additions
and
72 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# 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. | ||
|
||
import logging | ||
from re import compile, split | ||
from typing import Mapping | ||
|
||
_logger = logging.getLogger(__name__) | ||
|
||
|
||
# https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables | ||
_OWS = r"[ \t]*" | ||
# A key contains one or more US-ASCII character except CTLs or separators. | ||
_KEY_FORMAT = ( | ||
r"[\x21\x23-\x27\x2a\x2b\x2d\x2e\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+" | ||
) | ||
# A value contains a URL encoded UTF-8 string. | ||
_VALUE_FORMAT = r"[\x21\x23-\x2b\x2d-\x3a\x3c-\x5b\x5d-\x7e]*" | ||
_HEADER_FORMAT = _KEY_FORMAT + _OWS + r"=" + _OWS + _VALUE_FORMAT | ||
_HEADER_PATTERN = compile(_HEADER_FORMAT) | ||
_DELIMITER_PATTERN = compile(r"[ \t]*,[ \t]*") | ||
|
||
|
||
# pylint: disable=invalid-name | ||
def parse_headers(s: str) -> Mapping[str, str]: | ||
""" | ||
Parse ``s`` (a ``str`` instance containing HTTP headers). Uses W3C Baggage | ||
HTTP header format https://www.w3.org/TR/baggage/#baggage-http-header-format, except that | ||
additional semi-colon delimited metadata is not supported. | ||
""" | ||
headers = {} | ||
for header in split(_DELIMITER_PATTERN, s): | ||
if not header: # empty string | ||
continue | ||
match = _HEADER_PATTERN.fullmatch(header.strip()) | ||
if not match: | ||
_logger.warning("Header doesn't match the format: %s.", header) | ||
continue | ||
# value may contain any number of `=` | ||
name, value = match.string.split("=", 1) | ||
name = name.strip().lower() | ||
value = value.strip() | ||
headers[name] = value | ||
|
||
return headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# 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. | ||
|
||
# type: ignore | ||
|
||
import unittest | ||
|
||
from opentelemetry.util.re import parse_headers | ||
|
||
|
||
class TestParseHeaders(unittest.TestCase): | ||
def test_parse_headers(self): | ||
inp = [ | ||
# invalid header name | ||
("=value", [], True), | ||
("}key=value", [], True), | ||
("@key()=value", [], True), | ||
("/key=value", [], True), | ||
# invalid header value | ||
("name=\\", [], True), | ||
('name=value"', [], True), | ||
("name=;value", [], True), | ||
# different header values | ||
("name=", [("name", "")], False), | ||
("name===value=", [("name", "==value=")], False), | ||
# mix of valid and invalid headers | ||
( | ||
"name1=value1,invalidName, name2 = value2 , name3=value3==", | ||
[ | ||
( | ||
"name1", | ||
"value1", | ||
), | ||
("name2", "value2"), | ||
("name3", "value3=="), | ||
], | ||
True, | ||
), | ||
( | ||
"=name=valu3; key1; key2, content = application, red=\tvelvet; cake", | ||
[("content", "application")], | ||
True, | ||
), | ||
] | ||
for case in inp: | ||
s, expected, warn = case | ||
if warn: | ||
with self.assertLogs(level="WARNING") as cm: | ||
self.assertEqual(parse_headers(s), dict(expected)) | ||
self.assertTrue( | ||
"Header doesn't match the format:" | ||
in cm.records[0].message, | ||
) | ||
else: | ||
self.assertEqual(parse_headers(s), dict(expected)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters