Skip to content

Commit

Permalink
Fix error where header is missing in the response in resolveSampling …
Browse files Browse the repository at this point in the history
…method
  • Loading branch information
jj22ee committed Jan 9, 2025
1 parent 72d523d commit eb8ae16
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
8 changes: 5 additions & 3 deletions packages/core/lib/middleware/mw_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ var utils = {
}
}

if (amznTraceHeader.sampled === '?') {
if (amznTraceHeader.sampled === '?' && res.header) {
res.header[XRAY_HEADER] = 'Root=' + amznTraceHeader.root + ';Sampled=' + (isSampled ? '1' : '0');
}

Expand Down Expand Up @@ -172,8 +172,10 @@ var utils = {
var name = this.resolveName(req.headers.host);
var segment = new Segment(name, amznTraceHeader.root, amznTraceHeader.parent);

var responseWithEmbeddedRequest = Object.assign({}, res, { req: req });
this.resolveSampling(amznTraceHeader, segment, responseWithEmbeddedRequest);
if (!res.req) {
res.req = req;
}
this.resolveSampling(amznTraceHeader, segment, res);

segment.addIncomingRequestData(new IncomingRequestData(req));

Expand Down
64 changes: 62 additions & 2 deletions packages/core/test/unit/middleware/mw_utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ describe('Middleware utils', function() {
var envVarName = 'envDefaultName';
var hostName = 'www.myhost.com';
var traceId = '1-f9194208-2c7ad569f5d6ff149137be86';
var parentId = '74051af127d2bcba';

function reloadMWUtils() {
var path = '../../../lib/logger';
Expand Down Expand Up @@ -45,8 +46,6 @@ describe('Middleware utils', function() {
});

describe('#processHeaders', function() {
var parentId = '74051af127d2bcba';

it('should return an empty array on an undefined request', function() {
var headers = MWUtils.processHeaders();

Expand Down Expand Up @@ -220,6 +219,67 @@ describe('Middleware utils', function() {

assert.equal(segment.notTraced, true);
});

it('should not throw error when res.header is undefined and Sampled=?', function() {
var resWithoutHeader = {
req: {
headers: {},
url: '/api/move/up',
method: 'GET',
}
};
shouldSampleStub.returns(false);
var headers = { root: traceId, sampled: '?' };

assert.doesNotThrow(
() => {
MWUtils.resolveSampling(headers, segment, resWithoutHeader);
}
);

assert.equal(segment.notTraced, true);
});
});


describe('#traceRequestResponseCycle', function() {
var sandbox, shouldSampleStub;

beforeEach(function() {
sandbox = sinon.createSandbox();
MWUtils.sampler = localSampler;
MWUtils.setDefaultName(defaultName);

shouldSampleStub = sandbox.stub(MWUtils.sampler, 'shouldSample').returns(true);
});

afterEach(function() {
sandbox.restore();
});

it('should not throw error when Sampled=?', function() {
var req = {
headers: {},
url: '/api/move/up',
host: hostName,
method: 'GET',
[XRAY_HEADER]: 'Root=' + traceId + '; Parent=' + parentId + '; Sampled=?'
};
var segment;

var resWithoutHeader = {
req: req,
on: (name, callback) => {}
};
shouldSampleStub.returns(false);

assert.doesNotThrow(
() => {
segment = MWUtils.traceRequestResponseCycle(req, resWithoutHeader);
}
);
assert.equal(segment.notTraced, true);
});
});

describe('#samplingWildcardMatch', function() {
Expand Down

0 comments on commit eb8ae16

Please sign in to comment.