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

Set otel exporter headers value during runtime. #5129

Open
huliang-microsoft opened this issue Mar 31, 2024 · 5 comments
Open

Set otel exporter headers value during runtime. #5129

huliang-microsoft opened this issue Mar 31, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@huliang-microsoft
Copy link

Problem Statement

Sorry if this is not a feasible question.
I am trying to use otel exporter lib to export otel data to an otel collector. And I need to set auth header. This is what I do:

	kv := make(map[string]string)
	kv["Authorization"] = "Bearer " + token

	exporter := otlpmetrichttp.New(ctx, otlpmetrichttp.WithHeaders(kv))

The problem is that I cannot find an API to update the header once the exporter is initialized but when the token is expired, seems the only way is to create another exporter instance.

Is there a way to update the header in the exporter? or if there is not, is it Okay to add an API to update that?

@huliang-microsoft huliang-microsoft added the enhancement New feature or request label Mar 31, 2024
@XSAM
Copy link
Member

XSAM commented Apr 2, 2024

if there is not, is it Okay to add an API to update that?

I feel this is a legitimate request.

@dmathieu
Copy link
Member

dmathieu commented Apr 2, 2024

I see two ways we could do this:

  • Add a WithHeadersFn method, which would be executed for every HTTP request, and retrieve the proper headers. It's up to the users to decide how those headers are updated.
  • Add an UpdateHeaders method, which allows updating the headers into the exporter (either with a replace all, or with a merge of the existing ones, to only override the changed headers).

I'd lean towards the second option, as with the first one, the added method would be in a rather hot path. And the second one should allow more flexibility.

@pellared
Copy link
Member

pellared commented Apr 2, 2024

I am not sure if adding options like WithHeadersFn/UpdateHeaders would be good enough. Wouldn't the user also need to add a HTTP/gRPC response handler to invoke e.g. UpdateHeaders when the OTLP receiver/ingest returns e.g. 401 HTTP Status Code?

The client may know the token's expiration date. However, the token could be invalidated sooner than that.

@huliang-microsoft
Copy link
Author

@dmathieu @XSAM @pellared Thanks for looking into this issue. While before this is implemented, I would like to find a workaround to achieve what I need.

I tried to create a new metric.MeterProvider and call otel.SetMeterProvider(meterProvider) during runtime, inside the new MeterProvider, I have a new metricExporter with the updated token. But it seems like the new MeterProvider is only added but not replacing the old one. Is there a way to replace the existing MeterProvider registered so I can use the new MeterProvider with the new token? Or is there other way to do that?

Thanks for the help!

@dmathieu
Copy link
Member

dmathieu commented Apr 6, 2024

Providers are additive, not replacing each other. So this approach won't be valid.
Also, replacing the meter provider would reset cumulative metrics, which is probably not what you want.

I would instead setup a custom exporter, with a way to replace the header value at runtime.
Then, that exporter can hold an OTLP exporter, and replace it with a new one every time the header changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants