Skip to content

Commit

Permalink
Fix error due to missing header in the response in resolveSampling me…
Browse files Browse the repository at this point in the history
…thod (#706)

* Fix error where header is missing in the response in resolveSampling method

* update var naming
  • Loading branch information
jj22ee authored Jan 10, 2025
1 parent 72d523d commit d27bfd2
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 9 deletions.
10 changes: 6 additions & 4 deletions packages/core/lib/middleware/mw_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ var utils = {
}
}

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

if (!isSampled) {
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
80 changes: 75 additions & 5 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 @@ -149,7 +148,7 @@ describe('Middleware utils', function() {
});

describe('#resolveSampling', function() {
var res, sandbox, segment, shouldSampleStub;
var res, sandbox, segment, responseHeaders, shouldSampleStub;

beforeEach(function() {
sandbox = sinon.createSandbox();
Expand All @@ -158,18 +157,22 @@ describe('Middleware utils', function() {
shouldSampleStub = sandbox.stub(MWUtils.sampler, 'shouldSample').returns(true);

segment = {};
responseHeaders = {};
res = {
req: {
headers: { host: 'moop.hello.com' },
url: '/evergreen',
method: 'GET',
},
header: {}
header: (headerKey, headerValue) => {
responseHeaders[headerKey] = headerValue;
}
};
});

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

it('should not mark segment as not traced if the sampled header is set to "1"', function() {
Expand Down Expand Up @@ -209,7 +212,7 @@ describe('Middleware utils', function() {
MWUtils.resolveSampling(headers, segment, res);

var expected = new RegExp('^Root=' + traceId + ';Sampled=1$');
assert.match(res.header[XRAY_HEADER], expected);
assert.match(responseHeaders[XRAY_HEADER], expected);
});

it('should mark segment as not traced if the sampling rules check returns false', function() {
Expand All @@ -220,6 +223,73 @@ 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',
[XRAY_HEADER]: 'Root=' + traceId + ';Parent=' + parentId + ';Sampled=?'
},
host: hostName,
method: 'GET',
};
var segment;
var responseHeaders = {};

var res = {
req: req,
on: (name, callback) => {},
header: (headerKey, headerValue) => {
responseHeaders[headerKey] = headerValue;
}
};
shouldSampleStub.returns(false);

assert.doesNotThrow(
() => {
segment = MWUtils.traceRequestResponseCycle(req, res);
}
);
assert.equal(segment.notTraced, true);
assert.equal(responseHeaders[XRAY_HEADER], 'Root=' + traceId + ';Sampled=0');
});
});

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

0 comments on commit d27bfd2

Please sign in to comment.