-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add tracestate header when starting lambda transaction #2185
Conversation
💚 Build Succeeded
Expand to view the summary
Build stats
Test stats 🧪
Trends 🧪 |
lib/lambda.js
Outdated
@@ -86,11 +87,14 @@ module.exports = function elasticApmAwsLambda (agent) { | |||
* elastic-apm-traceparent if available. | |||
*/ | |||
parentId = value |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- The handling will have to change to avoid that
break
preferring 'elastic-apm-traceparent' to 'traceparent'. IIUC, with the current logic it will miss tracestate if it comes after 'elastic-apm-traceparent' inpayload.headers
. - Also, per feat: prefer 'traceparent' header over 'elastic-apm-traceparent' header #2079 the logic should change (could be a separate PR) to prefer 'traceparent' to 'elastic-apm-traceparent'.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@trentm Ugh -- that's a weird construct. Thanks for catching it. Any strong preference between
- more code and fewer loop cycles (matching most closely with current intent/pattern)
if (payload.headers !== undefined) {
for (const [key, value] of Object.entries(payload.headers)) {
const lowerCaseKey = key.toLowerCase()
if (lowerCaseKey === 'traceparent') {
parentId = value
} else if (!parentId && lowerCaseKey === 'elastic-apm-traceparent') {
parentId = value
} else if(lowerCaseKey === 'tracestate') {
tracestate = value
}
if(parentId && tracestate) {
break
}
}
}
- or less code but slightly more work normalizing all the headers
if (payload.headers !== undefined) {
const normalizedHeaders = {}
for (const [key, value] of Object.entries(payload.headers)) {
const lowerCaseKey = key.toLowerCase()
normalizedHeaders[lowerCaseKey] = value
}
parentId = normalizedHeaders.traceparent ? normalizedHeaders.traceparent : normalizedHeaders['elastic-apm-traceparent']
tracestate = normalizedHeaders.tracestate
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The former will prefer 'elastic-apm-traceparent' over 'traceparent' if it and 'tracestate' come first, so doesn't work as written.
No strong preference between those two until perf profiles show that the section is a hot spot.
Slightly related: from old https://elastic.slack.com/archives/C5M02D1GE/p1619802623086700?thread_ts=1619742610.083400&cid=C5M02D1GE for ([key, value] of Object.entries(obj))
is dog slow. Perhaps premature optimization, but I'd generally bias against using it.
I've replaced the I've opted for the clearer (to me) code of normalizing all the headers rather than the micro-optimization of finishing once we have the specific headers we're interested in. I've added test cases with both |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
…#2185) * feat: add tracestate header when starting a lambda transaction part of elastic#2156
Checklist
Part of #2156
This PR ensures that if the Lambda function is responding to an API Gateway request (or other request that includes a
tracestate
header) that its transaction will start using that tracestate. This is needed because our existing lambda instrumentation was written beforetracestate
was a thing.