Repository navigation
fix(otlp): let headers set in code win over OTEL_EXPORTER_OTLP_HEADERS - #3899
vignesh2027 wants to merge 2 commits into
Conversation
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.
Pull request dashboard statusWaiting on reviewers · refreshed 2026-12-03 07:54 UTC Review the latest changes. Status above doesn't look right?
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
|
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..) |
|
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. |
|
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. |
Fixes #3898
The OTLP HTTP and gRPC exporters merged
OTEL_EXPORTER_OTLP_HEADERS(and the signal specific variants) over headers set withwith_headersandwith_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-Agentstays 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-Agentbehaviour. The existing gRPC merge test asserted the old precedence, so I updated its expectation.Merge requirement checklist
CHANGELOG.mdfiles updated for non-trivial, user-facing changes