Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dynamic headers to Exporter constructor #4431

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# limitations under the License.

from os import environ
from typing import Dict, Optional, Sequence, Tuple, Union
from typing import Dict, Optional, Sequence, Tuple, Union, Iterator, Callable
from typing import Sequence as TypingSequence

from grpc import ChannelCredentials, Compression
Expand Down Expand Up @@ -58,6 +58,9 @@ def __init__(
headers: Optional[
Union[TypingSequence[Tuple[str, str]], Dict[str, str], str]
] = None,
additional_dynamic_headers: dict[
str, Callable[[], Iterator[str]]
] = None,
timeout: Optional[int] = None,
compression: Optional[Compression] = None,
):
Expand Down Expand Up @@ -97,6 +100,7 @@ def __init__(
"insecure": insecure,
"credentials": credentials,
"headers": headers,
"additional_dynamic_headers": additional_dynamic_headers,
"timeout": timeout or environ_timeout,
"compression": compression,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
Callable,
Dict,
Generic,
Iterator,
List,
Optional,
Tuple,
Expand Down Expand Up @@ -194,6 +195,9 @@ def __init__(
headers: Optional[
Union[TypingSequence[Tuple[str, str]], Dict[str, str], str]
] = None,
additional_dynamic_headers: dict[
str, Callable[[], Iterator[str]]
] = None,
timeout: Optional[int] = None,
compression: Optional[Compression] = None,
):
Expand All @@ -219,7 +223,7 @@ def __init__(

if parsed_url.netloc:
self._endpoint = parsed_url.netloc

self._additional_dynamic_headers = additional_dynamic_headers
self._headers = headers or environ.get(OTEL_EXPORTER_OTLP_HEADERS)
if isinstance(self._headers, str):
temp_headers = parse_env_headers(self._headers, liberal=True)
Expand Down Expand Up @@ -287,6 +291,11 @@ def _export(
# delay,
# )
max_value = 64
headers = self._headers
if self._additional_dynamic_headers:
headers += tuple(
(k, next(v())) for k, v in self._additional_dynamic_headers
)
# expo returns a generator that yields delay values which grow
# exponentially. Once delay is greater than max_value, the yielded
# value will remain constant.
Expand All @@ -298,7 +307,7 @@ def _export(
try:
self._client.Export(
request=self._translate_data(data),
metadata=self._headers,
metadata=headers,
timeout=self._timeout,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from dataclasses import replace
from logging import getLogger
from os import environ
from typing import Dict, Iterable, List, Optional, Tuple, Union
from typing import Dict, Iterable, List, Optional, Tuple, Union, Iterator, Callable
from typing import Sequence as TypingSequence

from grpc import ChannelCredentials, Compression
Expand Down Expand Up @@ -98,6 +98,9 @@ def __init__(
headers: Optional[
Union[TypingSequence[Tuple[str, str]], Dict[str, str], str]
] = None,
additional_dynamic_headers: dict[
str, Callable[[], Iterator[str]]
] = None,
timeout: Optional[int] = None,
compression: Optional[Compression] = None,
preferred_temporality: Dict[type, AggregationTemporality] = None,
Expand Down Expand Up @@ -143,6 +146,7 @@ def __init__(
credentials=credentials,
headers=headers or environ.get(OTEL_EXPORTER_OTLP_METRICS_HEADERS),
timeout=timeout or environ_timeout,
additional_dynamic_headers= additional_dynamic_headers,
compression=compression,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import logging
from os import environ
from typing import Dict, Optional, Sequence, Tuple, Union
from typing import Dict, Optional, Sequence, Tuple, Union, Iterator, Callable
from typing import Sequence as TypingSequence

from grpc import ChannelCredentials, Compression
Expand Down Expand Up @@ -91,6 +91,9 @@ def __init__(
headers: Optional[
Union[TypingSequence[Tuple[str, str]], Dict[str, str], str]
] = None,
additional_dynamic_headers: dict[
str, Callable[[], Iterator[str]]
] = None,
timeout: Optional[int] = None,
compression: Optional[Compression] = None,
):
Expand Down Expand Up @@ -129,6 +132,7 @@ def __init__(
"credentials": credentials,
"headers": headers
or environ.get(OTEL_EXPORTER_OTLP_TRACES_HEADERS),
"additional_dynamic_headers": additional_dynamic_headers,
"timeout": timeout or environ_timeout,
"compression": compression,
}
Expand Down