Skip to content

Add dynamic headers to Exporter constructor#4431

Closed
DylanRussell wants to merge 0 commit intoopen-telemetry:mainfrom
DylanRussell:main
Closed

Add dynamic headers to Exporter constructor#4431
DylanRussell wants to merge 0 commit intoopen-telemetry:mainfrom
DylanRussell:main

Conversation

@DylanRussell
Copy link
Copy Markdown
Contributor

The draft PR prototypes adding additional_dynamic_headers to OTLP gRPC / HTTP Exporter constructor.

Additional dynamic headers is a map where the key is the header name and the value is a generator function which produces the header value when called. These header functions are called on each call to export.

Google needs something like this in order to set the Authorization header, which periodically needs to be refreshed and updated.

Here's how we'd use this new constructor:

from opentelemetry.exporter.otlp.proto.http._log_exporter import OTLPLogExporter as HTTPOTLPLogExporter
from opentelemetry.exporter.otlp.proto.grpc._log_exporter import OTLPLogExporter as GRPCOTLPLogExporter
import google.auth
from google.auth.transport import requests

def build_google_grpc_otlp_log_exporter() -> GRPCOTLPLogExporter:
    return GRPCOTLPLogExporter(additional_dynamic_headers={'Authorization': build_auth_stuff})

def build_google_http_otlp_log_exporter() -> HTTPOTLPLogExporter:
    return HTTPOTLPLogExporter(additional_dynamic_headers={'Authorization': build_auth_stuff})

def build_auth_stuff() ->  Iterator[str]:
    credentials, _ = google.auth.default()
    request = google.auth.transport.requests.Request()
    while True:
        if credentials.expired:
            credentials.refresh(request)
        yield f'Bearer: {credentials.token}'

Java has something similar

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Feb 14, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@Kludex
Copy link
Copy Markdown
Member

Kludex commented Feb 20, 2025

Same comment as #4432.

@dimaqq
Copy link
Copy Markdown
Contributor

dimaqq commented Feb 21, 2025

Sure, but why a generator and not a callable?

@dimaqq
Copy link
Copy Markdown
Contributor

dimaqq commented Feb 21, 2025

Other projects solved this issue, aiodynamo is one such case that I'm familiar with and can recommend looking into for inspiration:

Custom credentials: https://aiodynamo.readthedocs.io/en/latest/advanced.html#custom-credentials-loader

Overall use: https://aiodynamo.readthedocs.io/en/latest/quickstart.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants