Repository navigation
feat(telemetry): support OTEL_EXPORTER_OTLP_HEADERS on all exporters - #1719
DustinReynoldsPE wants to merge 2 commits into
Conversation
Standard OTel env var was read for endpoint/protocol but headers were dropped, so authenticated collectors (e.g. Langfuse's OTLP endpoint) could not be used. Parse key=value pairs and pass WithHeaders to all four exporters (trace+metric, grpc+http).
|
🔍 OpenCodeReview found 1 issue(s) in this PR.
|
| func parseOTLPHeaders(s string) map[string]string { | ||
| headers := make(map[string]string) | ||
| for _, pair := range strings.Split(s, ",") { | ||
| k, v, ok := strings.Cut(pair, "=") | ||
| if k = strings.TrimSpace(k); ok && k != "" { | ||
| headers[k] = strings.TrimSpace(v) | ||
| } | ||
| } | ||
| return headers | ||
| } |
There was a problem hiding this comment.
Per the OpenTelemetry specification, OTEL_EXPORTER_OTLP_HEADERS values are URL-encoded (e.g., spaces as %20, commas as %2C). This parser does not URL-decode keys or values, so a spec-compliant user who sets authorization=Basic%20abc123 would get the literal string "Basic%20abc123" instead of "Basic abc123", causing authentication failures.
Additionally, splitting on raw , will break header values that contain literal commas (which the spec says should be encoded as %2C).
Consider adding url.QueryUnescape for both keys and values after splitting:
Suggestion:
| func parseOTLPHeaders(s string) map[string]string { | |
| headers := make(map[string]string) | |
| for _, pair := range strings.Split(s, ",") { | |
| k, v, ok := strings.Cut(pair, "=") | |
| if k = strings.TrimSpace(k); ok && k != "" { | |
| headers[k] = strings.TrimSpace(v) | |
| } | |
| } | |
| return headers | |
| } | |
| func parseOTLPHeaders(s string) map[string]string { | |
| headers := make(map[string]string) | |
| for _, pair := range strings.Split(s, ",") { | |
| k, v, ok := strings.Cut(pair, "=") | |
| k = strings.TrimSpace(k) | |
| if !ok || k == "" { | |
| continue | |
| } | |
| v = strings.TrimSpace(v) | |
| if dk, err := url.QueryUnescape(k); err == nil { | |
| k = dk | |
| } | |
| if dv, err := url.QueryUnescape(v); err == nil { | |
| v = dv | |
| } | |
| headers[k] = v | |
| } | |
| return headers | |
| } |
There was a problem hiding this comment.
Fixed in 54d3c5c — keys and values are now URL-decoded after the comma/= split, so authorization=Basic%20abc123 yields Basic abc123 and %2C inside a value no longer breaks pair splitting.
One deviation from the suggested fix: url.PathUnescape is used instead of url.QueryUnescape. QueryUnescape decodes + as a space, which would corrupt base64-encoded credentials containing a literal + (legal in base64). Added a test covering %20, %2C, and %2B.
Review feedback: spec encodes keys/values (%20, %2C), so decode both with PathUnescape — QueryUnescape would corrupt base64 '+' as space.
NanaseInori
left a comment
There was a problem hiding this comment.
Reviewed all three changed files, the surrounding exporter construction, go.mod, and the existing discussion. The telemetry-header search found this PR and the unrelated merged raw-LLM capture work (#1109); I found no competing implementation of this change. Authenticated collectors are a valid use case, but the claim that the pinned SDK ignores the standard header environment variables needs correction.
[P2] Preserve signal-specific header precedence. go.mod pins the OTLP exporters to v1.45.0. Those exporters already consume OTEL_EXPORTER_OTLP_HEADERS and their signal-specific variants, with OTEL_EXPORTER_OTLP_TRACES_HEADERS / OTEL_EXPORTER_OTLP_METRICS_HEADERS taking precedence. An explicit WithHeaders option overrides that environment-derived configuration. The new code resolves only the generic variable and passes its map explicitly to both signal exporters whenever it is nonempty, reversing the existing precedence.
For example:
OTEL_EXPORTER_OTLP_HEADERS='authorization=generic-token'
OTEL_EXPORTER_OTLP_TRACES_HEADERS='authorization=trace-token'
OTEL_EXPORTER_OTLP_METRICS_HEADERS='authorization=metric-token'Previously the SDK selects the corresponding trace/metric credential. With these new WithHeaders calls, both exporters receive generic-token instead. A collector requiring the signal-specific credential rejects the export; signal-specific headers absent from the generic map are also lost.
Please retain the SDK's environment handling, or resolve each signal's configuration with the documented precedence before applying explicit options. Add an exporter-level regression that sets all three variables and inspects the received headers/metadata. The parser-only tests cannot catch this regression. Please also demonstrate any remaining pre-change authentication failure independently of the endpoint-path fix.
Pinned official references: trace HTTP v1.45.0 configuration, metric gRPC v1.45.0 WithHeaders.
Validation: source inspection against the pinned SDK documentation. I did not run the repository suite or a live collector experiment. The earlier URL-decoding review has been addressed in the current parser and is not repeated here.
This review was conducted by NanaseInori's review bot, using the model GPT-6 Astra Pro. If you need a human review, please manually @NanaseInori.
What
OTEL_EXPORTER_OTLP_ENDPOINTandOTEL_EXPORTER_OTLP_PROTOCOLare honoured today, butOTEL_EXPORTER_OTLP_HEADERS— the standard OTel environment variable for request headers — is silently ignored. This parses it (key=value,key2=value2) and passesWithHeadersto all four exporters (traces + metrics, gRPC + HTTP).Why
Authenticated OTLP endpoints are unusable without it: Langfuse, Honeycomb, Grafana Cloud and any collector behind an auth header all reject unauthenticated pushes. The exporter code already calls out Langfuse's path handling (
/api/public/otel, added in #667); headers are the remaining piece needed to actually reach it.Test plan
make check— pass (gofmt + vet)make test— pass, full suite (go test -race) on all packagesTestResolveEnvcover parsing, whitespace handling, and the unset defaultauthorization=Basic …overhttp/protobufare accepted and renderedAI assistance disclosure
This contribution was prepared with AI assistance (Devin, Cognition — SWE-2 model). I reviewed the change end to end, ran the checks above myself, and can explain any line of it.