-
Notifications
You must be signed in to change notification settings - Fork 860
Expand file tree
/
Copy path_common.py
More file actions
81 lines (68 loc) · 2.56 KB
/
_common.py
File metadata and controls
81 lines (68 loc) · 2.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# 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 __future__ import annotations
import logging
from typing import Optional
from opentelemetry.sdk._configuration._exceptions import ConfigurationError
_logger = logging.getLogger(__name__)
def _parse_headers(
headers: Optional[list],
headers_list: Optional[str],
) -> Optional[dict[str, str]]:
"""Merge headers struct and headers_list into a dict.
Returns None if neither is set, letting the exporter read env vars.
headers struct takes priority over headers_list for the same key.
"""
if headers is None and headers_list is None:
return None
result: dict[str, str] = {}
if headers_list:
for item in headers_list.split(","):
item = item.strip()
if "=" in item:
key, value = item.split("=", 1)
result[key.strip()] = value.strip()
elif item:
_logger.warning(
"Invalid header pair in headers_list (missing '='): %s",
item,
)
if headers:
for pair in headers:
result[pair.name] = pair.value or ""
return result
def _map_compression(
value: str | None,
compression_enum: type,
*,
allow_deflate: bool = False,
) -> object | None:
"""Map a compression string to the given Compression enum value."""
if value is None:
return None
value_lower = value.lower()
supports_deflate = allow_deflate and hasattr(compression_enum, "Deflate")
if value_lower == "none":
return None
if value_lower == "gzip":
return compression_enum.Gzip # type: ignore[attr-defined]
if value_lower == "deflate" and supports_deflate:
return compression_enum.Deflate # type: ignore[attr-defined]
supported_values = ["'gzip'", "'none'"]
if supports_deflate:
supported_values.insert(1, "'deflate'")
raise ConfigurationError(
f"Unsupported compression value '{value}'. Supported values: "
f"{', '.join(supported_values)}."
)