open-telemetry / opentelemetry-collector-contrib

Contrib repository for the OpenTelemetry Collector
https://opentelemetry.io
Apache License 2.0
2.9k stars 2.27k forks source link

default value for header setter extension #34412

Open f7o opened 1 month ago

f7o commented 1 month ago

Component(s)

extension/headerssetter

Is your feature request related to a problem? Please describe.

When using the header setter with from_context configuration set, and the key is not set within context from receiver, the header is set to nil on the exporter.

Describe the solution you'd like

  extensions:
    headers_setter:
      headers:
        - action: upsert
          key: X-TENANT
          from_context: X-TENANT
          default_value: my-tenant-1 # new proposed config field

Describe alternatives you've considered

Allow to specify a default value per header action in case X-TENANT on the provided context is empty/nil

Additional context

No response

github-actions[bot] commented 1 month ago

Pinging code owners:

Frapschen commented 1 month ago

@f7o Makes sense to me, are you interested in contribute it?

f7o commented 1 month ago

@Frapschen yeah, sure

f7o commented 1 month ago

oh wait a minute. looking at the code I believe this functionally could be achieved by something like this:

headers:
- action: upsert
  key: X-TENANT
  from_context: X-TENANT
- action: insert
  key: X-TENANT
  value: some-tenant

I believe it would first try to set it from context, even if it's empty. But second set the value only if first header does not set it. I'll test this before continue implementing the new config field.

@jpkrohling have you had this use case before?

jpkrohling commented 1 month ago

I believe that's correct: an upsert of a fixed value would cause it to act as a default value.

f7o commented 2 weeks ago

I tried the following

        - action: upsert
          key: X-NAMESPACE
          from_context: X-TENANT
        - action: insert
          key: X-NAMESPACE
          value: sre-test

This apparently does not work if X-TENANT is not provided in upstream metadata. I'll continue implement the default_value config. It does not add the header at all, once I enable the header upstream the first rule matches and picks and sets the header from upstream.