Skip to content

Commit

Permalink
get host from request instead
Browse files Browse the repository at this point in the history
  • Loading branch information
William Armiros committed May 18, 2021
1 parent 287e8d3 commit 54efc22
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 20 deletions.
4 changes: 2 additions & 2 deletions packages/core/lib/patchers/http_p.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function enableCapture(module, downstreamXRayEnabled, subsegmentCallback) {
} else {
const madeItToDownstream = (e.code !== 'ECONNREFUSED');

subsegment.addRemoteRequestData(this, null, madeItToDownstream && downstreamXRayEnabled, options);
subsegment.addRemoteRequestData(this, null, madeItToDownstream && downstreamXRayEnabled);
subsegment.close(e);
}

Expand Down Expand Up @@ -165,7 +165,7 @@ function enableCapture(module, downstreamXRayEnabled, subsegmentCallback) {
if (cause)
subsegment[cause] = true;

subsegment.addRemoteRequestData(res.req, res, !!downstreamXRayEnabled, options);
subsegment.addRemoteRequestData(res.req, res, !!downstreamXRayEnabled);
subsegment.close();
});

Expand Down
10 changes: 4 additions & 6 deletions packages/core/lib/segments/attributes/remote_request_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ var { stripQueryStringFromPath } = require('../../utils');
* @param {http.ClientRequest|https.ClientRequest} req - The request object from the HTTP/HTTPS call.
* @param {http.IncomingMessage|https.IncomingMessage} res - The response object from the HTTP/HTTPS call.
* @param {boolean} downstreamXRayEnabled - when true, adds a "traced": true hint to generated subsegments such that the AWS X-Ray service expects a corresponding segment from the downstream service.
* @param {object} [options] - Optional HTTP Request options, which will take precedence over header data to populate request metadata.
*/

function RemoteRequestData(req, res, downstreamXRayEnabled, options) {
this.init(req, res, downstreamXRayEnabled, options);
function RemoteRequestData(req, res, downstreamXRayEnabled) {
this.init(req, res, downstreamXRayEnabled);
}

RemoteRequestData.prototype.init = function init(req, res, downstreamXRayEnabled, options) {
const useOptionsHost = options && options.hostname;
RemoteRequestData.prototype.init = function init(req, res, downstreamXRayEnabled) {
this.request = {
url: (req.agent && req.agent.protocol) ? (req.agent.protocol + '//' + (useOptionsHost ? options.hostname : req.getHeader('host')) + stripQueryStringFromPath(req.path)) : '',
url: (req.agent && req.agent.protocol) ? (req.agent.protocol + '//' + (req.host || req.getHeader('host')) + stripQueryStringFromPath(req.path)) : '',
method: req.method || '',
};

Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/segments/attributes/subsegment.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ declare class Subsegment {

addError(err: Error | string, remote?: boolean): void;

addRemoteRequestData(req: http.ClientRequest, res: http.IncomingMessage, downstreamXRayEnabled?: boolean, options?: object): void;
addRemoteRequestData(req: http.ClientRequest, res: http.IncomingMessage, downstreamXRayEnabled?: boolean): void;

addFaultFlag(): void;

Expand Down
5 changes: 2 additions & 3 deletions packages/core/lib/segments/attributes/subsegment.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,10 @@ Subsegment.prototype.addError = function addError(err, remote) {
* @param {http.ClientRequest/https.ClientRequest} req - The request object from the HTTP/HTTPS call.
* @param {http.IncomingMessage/https.IncomingMessage} res - The response object from the HTTP/HTTPS call.
* @param {boolean} downstreamXRayEnabled - when true, adds a "traced": true hint to generated subsegments such that the AWS X-Ray service expects a corresponding segment from the downstream service.
* @param {object} [options] - Optional HTTP Request options, which will take precedence over header data to populate request metadata.
*/

Subsegment.prototype.addRemoteRequestData = function addRemoteRequestData(req, res, downstreamXRayEnabled, options) {
this.http = new RemoteRequestData(req, res, downstreamXRayEnabled, options);
Subsegment.prototype.addRemoteRequestData = function addRemoteRequestData(req, res, downstreamXRayEnabled) {
this.http = new RemoteRequestData(req, res, downstreamXRayEnabled);
if ('traced' in this.http.request) {
this.traced = this.http.request.traced;
delete this.http.request.traced;
Expand Down
1 change: 0 additions & 1 deletion packages/core/test-d/index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ expectType<void>(subsegment.addSqlData({}));
const clientRequest = new http.ClientRequest('http://localhost');
expectType<void>(subsegment.addRemoteRequestData(clientRequest, incomingMessage));
expectType<void>(subsegment.addRemoteRequestData(clientRequest, incomingMessage, true));
expectType<void>(subsegment.addRemoteRequestData(clientRequest, incomingMessage, false, {}));
expectType<true | undefined>(subsegment.streamSubsegments());
expectType<{ [key: string]: any }>(subsegment.toJSON());

Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/unit/patchers/http_p.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ describe('HTTP/S', function() {
capturedHttp.request(httpOptions);

setTimeout(function() {
addRemoteDataStub.should.have.been.calledWithExactly(fakeRequest, fakeResponse, false, httpOptions);
addRemoteDataStub.should.have.been.calledWithExactly(fakeRequest, fakeResponse, false);
done();
}, 50);
});
Expand All @@ -293,7 +293,7 @@ describe('HTTP/S', function() {
capturedHttp.request(httpOptions);

setTimeout(function() {
addRemoteDataStub.should.have.been.calledWithExactly(fakeRequest, fakeResponse, true, httpOptions);
addRemoteDataStub.should.have.been.calledWithExactly(fakeRequest, fakeResponse, true);
done();
}, 50);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,11 @@ describe('RemoteRequestData', function() {
''
);
});
it('should use the host from the options', () => {
const options = {
hostname: 'different-site.com'
};
it('should use the host from the request object over headers', () => {
const requestWithHost = Object.assign(request, { host: 'different-site.com' });

assert.propertyVal(
new RemoteRequestData(request, response, true, options).request,
new RemoteRequestData(requestWithHost, response, true).request,
'url',
'https://different-site.com/path/to/resource'
);
Expand Down

0 comments on commit 54efc22

Please sign in to comment.