Skip to content

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

@prebours

Description

@prebours

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?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions