Fix error due to missing header in the response in resolveSampling method #706
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.
Issue #, if available:
The following error can occur when an incoming request header
x-amzn-trace-id
hasSampled=?
. This can cause an error to occur for this logic specific forSampled=?
.The root cause is that the traceRequestResponseCycle method is creating a copy of the response with
Object.assign({}, res, { req: req })
, but in doing so,res.header
is not copied via Object.assign since it is a prototype method. Thus when this copied response is passed intoresolveSampling
, the error occurs whenres.header
is accessed.The bug is introduced in this old commit - See Before and After.
This
traceRequestResponseCycle
logic is used byaws-xray-sdk-express
,aws-xray-sdk-restify
, andaws-xray-sdk-fastify
.Also,
res.header
is a method, not an object which the SDK currently assumes.Description of changes:
resolveSampling
method inmw_utils.js
is now the original response, which was the previous/original logic for Express and Restify. This is also the logic currently used in aws-xray-sdk-hapi and aws-xray-sdk-koa2.res.header
before it is modified inresolveSampling
. This is needed becauseres.header
is not guaranteed to exist, as is the case when this logic is used inaws-xray-sdk-fastify
for Fastify users.res.header
as a method instead of treating it like an object.Testing:
Express
,Restify
, andFastify
sample appsSampled=?
cases. Ensure error occurs before the the fix, and there is no error after the fix. Ensure that the response headers include the XRay Context header when request headers haveSampled=?
(not working forFastify
which doesn't provideheader
method in reply.raw).resolveSampling
andtraceRequestResponseCycle
whereSampled=?
.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.