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

Dynamic Header for OTLPTraceExporter - Package @opentelemetry/exporter-trace-otlp-http #2903

Open
prebours opened this issue Apr 18, 2022 · 8 comments · May be fixed by #3662 or #5118
Open

Dynamic Header for OTLPTraceExporter - Package @opentelemetry/exporter-trace-otlp-http #2903

prebours opened this issue Apr 18, 2022 · 8 comments · May be fixed by #3662 or #5118

Comments

@prebours
Copy link

prebours commented Apr 18, 2022

Is your feature request related to a problem?

I am using the package @opentelemetry/exporter-trace-otlp-http to export the trace from the browser to the tracing server.

The tracing server is behind an authenticated proxy, where the token is provided through the authorization header.

Currently, we can provide custom headers when the exporter is created:

 const collectorOptions = {
    url, // url is optional and can be omitted - default is http://localhost:55681/v1/traces
    headers: {
      'authorization': `Bearer ${token}`
    }, // an optional object containing custom headers to be sent with each request
    concurrencyLimit: 10, // an optional limit on pending requests
  }

  const provider = new WebTracerProvider()
  provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter(
    collectorOptions
  )))

The token can change overtime (when it is renewed for example). I would need a way to provide an up-to-date token to the exporter before the exporter sends the payload.

Describe the solution you'd like

The easiest way would be to provide a way to update the header.

const collectorOptions = {
    url, // url is optional and can be omitted - default is http://localhost:55681/v1/traces
    headers: {
      'authorization': `Bearer ${token}`
    }, // an optional object containing custom headers to be sent with each request
    concurrencyLimit: 10, // an optional limit on pending requests
  }
  
  const exporter = new OTLPTraceExporter(
    collectorOptions
  )
  const provider = new WebTracerProvider()
  provider.addSpanProcessor(new SimpleSpanProcessor(exporter))

.... store exporter as a global variable

// when the token is updated
exporter.setHeader({
 'authorization': `Bearer ${token}`
})

Describe alternatives you've considered

An alternative could be to provide a callback. But it would mean that the token is stored somehow in a global variable. It may not be the workflow that people have. I have my token that is actually updated through an redux action.

const collectorOptions = {
    url, 
    headers: () => {
       return {
          authorization: `Bearer ${getTokenFromAGlobalFunction()}`
        }
    }.
    concurrencyLimit: 10, // an optional limit on pending requests
  }

Or would it be possible to clear the span processor and add a new one?

provider.clearSpanProcessor() // does it exist?
provider.addSpanProcessor(new SimpleSpanProcessor(new OTLPTraceExporter(
    collectorOptions
  )))

Other comments

What would happen if the token is nil? Is there a way to desactivate tracing until the token is issue?

@dyladan
Copy link
Member

dyladan commented Apr 18, 2022

Hmm there are several ways this could be solved I guess

  • The setHeaders call you suggested
  • A more general setConfig call
  • A headers callback which is called on each export request to get a new set of headers
  • passing the headers object by reference and mutating it (this might actually already work) but is probably the least stable

@github-actions
Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Jun 20, 2022
@github-actions
Copy link

github-actions bot commented Jul 4, 2022

This issue was closed because it has been stale for 14 days with no activity.

@github-actions github-actions bot closed this as completed Jul 4, 2022
@Josh-a-e
Copy link

This is a feature I'd be very interested in - I'd like to add my auth token to the headers so I can validate serverside if traces come from an authenticated source.

@legendecas legendecas reopened this Sep 13, 2022
@legendecas legendecas removed the stale label Sep 13, 2022
@eloo-abi
Copy link

eloo-abi commented Mar 3, 2023

Hi, are there any news about this issue?

With the current implementation it seems that the while oauth mechanism will not because its not possible to renew the token used for authentication.

@tarasvarshava-oviva
Copy link

tarasvarshava-oviva commented Jun 27, 2024

In case someone is looking for a workaround. This will overwrite the Authorization header every time an export is made:

import { ReadableSpan } from '@opentelemetry/sdk-trace-web';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { ExportResult } from '@opentelemetry/core';
import { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';

...

class CustomOTLPTraceExporter extends OTLPTraceExporter {
  private readonly getToken: () => string | undefined;
  private _headers: Record<string, unknown>;

  constructor(config: OTLPExporterNodeConfigBase, getToken: () => string | undefined) {
    super(config);
    this.getToken = getToken;
    this._headers = { ...config.headers };
  }

  export(items: ReadableSpan[], resultCallback: (result: ExportResult) => void) {
    this._headers = { ...this._headers, Authorization: `Bearer ${this.getToken()}` };
    return super.export(items, resultCallback);
  }
}

...

const exporter = new CustomOTLPTraceExporter({ url: collectorPath, headers: {} }, getToken);

Where getToken is your callback to get a valid token on every export call.

Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days.

@github-actions github-actions bot added the stale label Oct 14, 2024
@bozzelliandrea bozzelliandrea linked a pull request Nov 6, 2024 that will close this issue
8 tasks
@bozzelliandrea
Copy link

Hi @tarasvarshava-oviva @eloo-abi @Josh-a-e @dyladan @prebours if you still interested on this feature, i start the implementation about it on pr: #5118

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