fix: prevent tracestate/traceparent header duplication in upstream requests#42
Merged
fix: prevent tracestate/traceparent header duplication in upstream requests#42
Conversation
…quests Add traceparent and tracestate to NOT_FORWARDED_HEADERS so they are not manually forwarded to the upstream AI gateway. OTel auto-instrumentation (UndiciInstrumentation) already injects these W3C trace-context headers from the active span context, so forwarding them manually caused each value to appear twice in the outgoing request.
Dependency Review✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.Scanned FilesNone |
frac
approved these changes
Apr 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When responses.js forwards a request to the AI gateway, the
tracestateandtraceparentheaders are duplicated. For example, an incomingtracestate: origin=testarrives at the upstream astracestate: origin=test, origin=test.Evidence
Trace showing the duplicated
tracestatein Grafana:Grafana Tempo trace
Root Cause
Two independent mechanisms both add W3C trace context headers to the outgoing request:
Manual header forwarding — In
innerStream.ts, all incoming request headers (except those inNOT_FORWARDED_HEADERS) are passed asdefaultHeadersto the OpenAI SDK client. Sincetraceparentandtracestatewere not in the exclusion list, they were forwarded verbatim.OTel auto-instrumentation — In
stateful_responses, responses.js is started with--import ./instrumentation.mjs(seeDockerfile.responses-js). That file sets up@opentelemetry/auto-instrumentations-nodeviagetNodeAutoInstrumentations(), which includesUndiciInstrumentationandHttpInstrumentation. These instrumentations hook into the outgoing request pipeline and callpropagation.inject()to addtraceparent/tracestatefrom the active span context.The combination of both results in each header value appearing twice.
Fix
Add
traceparentandtracestateto theNOT_FORWARDED_HEADERSset insrc/routes/responses/utils.ts. This lets OTel instrumentation be the single owner of trace context propagation to upstream services, which is the correct behavior — it ensures the child span ID (not the original caller's) is propagated.Before fix:
After fix: