-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(undici): avoid possible duplicate 'traceparent' header on instrum…
…ented HTTP requests (#3965) ... because Elasticsearch borks on these requests. This case should only be possible in a weird case like having two APM agents active. Fixes: #3964
- Loading branch information
Showing
3 changed files
with
75 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,9 @@ See the <<upgrade-to-v4>> guide. | |
* Fix undici instrumentation to cope with a bug in [email protected] where | ||
`request.addHeader()` was accidentally removed. (It was re-added in | ||
[email protected].) | ||
* Update undici instrumentation to avoid possibly adding a *second* | ||
'traceparent' header to outgoing HTTP requests, because this can break | ||
Elasticsearch requests. ({issues}3964[#3964]) | ||
[float] | ||
===== Chores | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,9 @@ try { | |
|
||
const semver = require('semver'); | ||
|
||
// Search an undici@5 request.headers string for a 'traceparent' header. | ||
const headersStrHasTraceparentRe = /^traceparent:/im; | ||
|
||
let isInstrumented = false; | ||
let spanFromReq = null; | ||
let chans = null; | ||
|
@@ -130,17 +133,37 @@ function instrumentUndici(agent) { | |
const propSpan = | ||
span || parentRunContext.currSpan() || parentRunContext.currTransaction(); | ||
if (propSpan) { | ||
propSpan.propagateTraceContextHeaders( | ||
request, | ||
function (req, name, value) { | ||
if (typeof request.addHeader === 'function') { | ||
req.addHeader(name, value); | ||
} else if (Array.isArray(request.headers)) { | ||
// [email protected] accidentally, briefly removed `request.addHeader()`. | ||
req.headers.push(name, value); | ||
// Guard against adding a duplicate 'traceparent' header, because that | ||
// breaks ES. https://github.com/elastic/apm-agent-nodejs/issues/3964 | ||
// Dev Note: This cheats a little and assumes the header names to add | ||
// will include 'traceparent'. | ||
let alreadyHasTp = false; | ||
if (Array.isArray(request.headers)) { | ||
// undici@6 | ||
for (let i = 0; i < request.headers.length; i += 2) { | ||
if (request.headers[i].toLowerCase() === 'traceparent') { | ||
alreadyHasTp = true; | ||
break; | ||
} | ||
}, | ||
); | ||
} | ||
} else if (typeof request.headers === 'string') { | ||
// undici@5 | ||
alreadyHasTp = headersStrHasTraceparentRe.test(request.headers); | ||
} | ||
|
||
if (!alreadyHasTp) { | ||
propSpan.propagateTraceContextHeaders( | ||
request, | ||
function (req, name, value) { | ||
if (typeof request.addHeader === 'function') { | ||
req.addHeader(name, value); | ||
} else if (Array.isArray(request.headers)) { | ||
// [email protected] accidentally, briefly removed `request.addHeader()`. | ||
req.headers.push(name, value); | ||
} | ||
}, | ||
); | ||
} | ||
} | ||
|
||
if (span) { | ||
|
This file contains 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