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

Conversation

DylanRussell
Copy link

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

Copy link

linux-foundation-easycla bot commented Feb 14, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@Kludex
Copy link
Contributor

Kludex commented Feb 20, 2025

Same comment as #4432.

@dimaqq
Copy link

dimaqq commented Feb 21, 2025

Sure, but why a generator and not a callable?

@dimaqq
Copy link

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