Skip to content

feat(telemetry): support OTEL_EXPORTER_OTLP_HEADERS on all exporters - #1719

Open
DustinReynoldsPE wants to merge 2 commits into
alibaba:mainfrom
DustinReynoldsPE:pr/otlp-headers
Open

DustinReynoldsPE wants to merge 2 commits into
alibaba:mainfrom
DustinReynoldsPE:pr/otlp-headers

Conversation

@DustinReynoldsPE

Copy link
Copy Markdown

What

OTEL_EXPORTER_OTLP_ENDPOINT and OTEL_EXPORTER_OTLP_PROTOCOL are honoured today, but OTEL_EXPORTER_OTLP_HEADERS — the standard OTel environment variable for request headers — is silently ignored. This parses it (key=value,key2=value2) and passes WithHeaders to 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 packages
  • New table cases in TestResolveEnv cover parsing, whitespace handling, and the unset default
  • Verified end-to-end against a live Langfuse v4 instance: spans exported with authorization=Basic … over http/protobuf are accepted and rendered

AI 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.

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).
@CLAassistant

CLAassistant commented Oct 12, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions

github-actions Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

🔍 OpenCodeReview found 1 issue(s) in this PR.

  • ✅ Successfully posted inline: 1 comment(s)

Comment on lines +71 to +80
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
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bug · medium
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:

Suggested change
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
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 NanaseInori left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

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.

3 participants