Skip to content

fix(otlp): let headers set in code win over OTEL_EXPORTER_OTLP_HEADERS - #3899

Open
vignesh2027 wants to merge 2 commits into
open-telemetry:mainfrom
vignesh2027:fix-otlp-headers-precedence
Open

vignesh2027 wants to merge 2 commits into
open-telemetry:mainfrom
vignesh2027:fix-otlp-headers-precedence

Conversation

@vignesh2027

Copy link
Copy Markdown
Contributor

Fixes #3898

The OTLP HTTP and gRPC exporters merged OTEL_EXPORTER_OTLP_HEADERS (and the signal specific variants) over headers set with with_headers and with_metadata. When the same name was set in both places the environment value was sent, which contradicts the documented rule that builder configuration always wins.

The environment is now applied first and the builder values on top. Environment headers that the code does not set are still sent. In the HTTP exporter the default User-Agent stays the lowest layer, so the environment can still replace it. To do that the default is no longer stored in the builder config, it is added when the client is built.

Tests cover both transports, plus the default User-Agent behaviour. The existing gRPC merge test asserted the old precedence, so I updated its expectation.

Merge requirement checklist

  • CONTRIBUTING guidelines followed
  • Unit tests added/updated (if applicable)
  • Appropriate CHANGELOG.md files updated for non-trivial, user-facing changes
  • Changes in public API reviewed (if applicable)

The HTTP and gRPC exporters merged the environment headers over the ones
set with with_headers and with_metadata, so a header present in both
places was sent with the environment value. That contradicts the
documented precedence where builder configuration always wins.

Apply the environment first and the builder values on top. The default
User-Agent stays the lowest layer, so the environment can still replace
it.
@vignesh2027
vignesh2027 requested a review from a team as a code owner December 3, 2026 07:44
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-12-03 07:54 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Should this be with the author? Comment /dashboard route:author to route it back.
  • Anything wrong — including the routing? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 86.8%. Comparing base (070a73f) to head (fcdf4d9).

Additional details and impacted files
@@          Coverage Diff          @@
##            main   #3899   +/-   ##
=====================================
  Coverage   86.7%   86.8%           
=====================================
  Files        131     131           
  Lines      34712   34785   +73     
=====================================
+ Hits       30116   30194   +78     
+ Misses      4596    4591    -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cijothomas

Copy link
Copy Markdown
Member

Thanks. It looks like we'd still keep the env+code merge, but this PR makes sure code wins on conflict?

Should we instead ignore the env ones completely if code sets headers? I don't think there's any spec wording for this either. Tagging @martincostello from .NET and @MrAlias from Go since they seem to do the "drop/replace" behavior, i.e. if code sets headers, env is ignored entirely. Curious about the reasoning there and whether we should align with other languages (given no spec guidance!).

Adding @jack-berg to see if he has thoughts. (With auto configure in Java, it'd doing the merge, similar to Rust I think..)

@jack-berg

Copy link
Copy Markdown
Member

Languages are on their own here. The spec never defined a precedent for code configuration vs. env vars, and there's probably too much divergence / strong opinions to ever get consistency now. Declarative config fixes this, but still no answer for env vars.

Personally, I think its a better design to build dedicated code for reading env vars and applying them through standard code configuration APIs. In this setup, you don't worry about precedence because you don't embed the reading of env vars into the various components. If I landed in a position of being stuck with components individually interpreting env vars and I had to make a choice on precedence, I would chose code config as the priority. Seems really unintuitive to me to have explicit code setting something just for an env var to sneakily override it.

@vignesh2027

Copy link
Copy Markdown
Contributor Author

Thanks both. I checked Go: the env vars are read into the config first and code options are applied after, and WithHeaders replaces the whole map, so once code sets headers no env header survives. Here (before and after this PR) it goes defaults, then env, then code, key by key, so code wins on a conflict but env only headers are still sent.

I'd keep the per key merge in this PR. It fixes the actual bug, an env value silently overriding an explicit code value, and it doesn't drop headers people may rely on today, for example a gateway token from OTEL_EXPORTER_OTLP_HEADERS while code only adds a tenant header. Switching to full replacement would change behaviour for those setups.

That said, if you'd rather match Go and .NET, I can switch it here: when headers are set in code, ignore both OTEL_EXPORTER_OTLP_HEADERS and the signal specific variable, for HTTP and tonic metadata, with tests and a changelog note.

This branch has not been deployed

No deployments
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.

[Bug]: Headers from OTEL_EXPORTER_OTLP_HEADERS replace the ones set in code

3 participants