Skip to content

Commit 7715a4a

Browse files
Shadowbeetledszakallas
authored andcommitted
feat(http): add keep query params option
1 parent 1511760 commit 7715a4a

5 files changed

Lines changed: 131 additions & 5 deletions

File tree

lib/instrumentations/core/http/request.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ var format = require('util').format
77

88
function httpClient (originalHttpRequest, agent) {
99
var whiteListHosts = agent.getConfig().whiteListHosts
10+
var config = agent.getConfig()
11+
var keepQueryParams = config.keepQueryParams
1012

1113
return function wrappedRequest (requestParams) {
1214
var collector = agent.tracer.collector
@@ -29,7 +31,13 @@ function httpClient (originalHttpRequest, agent) {
2931
return originalHttpRequest.apply(this, arguments)
3032
}
3133

32-
var resource = util.formatDataUrl(requestParams.path)
34+
var resource
35+
if (keepQueryParams) {
36+
var requestUrl = requestParams.path
37+
resource = util.formatDataUrl(requestUrl)
38+
} else {
39+
resource = requestParams.path.split('?')[0]
40+
}
3341

3442
var cs = collector.clientSend({
3543
protocol: 'http',

lib/instrumentations/core/http/request.spec.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,4 +439,66 @@ describe('The http.request wrapper module', function () {
439439
targetHost: 'localhost'
440440
})
441441
})
442+
443+
it('should not keep queryParams by default', function () {
444+
var r = request(original, agent)
445+
446+
appliedOriginal.on = this.sandbox.spy(function (name, cb) {
447+
if (name === 'response') {
448+
cb({
449+
headers: {
450+
'x-parent': '2',
451+
'x-server-send': '12345668',
452+
'x-server-receive': '12345698'
453+
},
454+
statusCode: 200
455+
})
456+
}
457+
})
458+
459+
r({
460+
host: 'localhost',
461+
path: '/query?foo=bar',
462+
headers: {},
463+
method: 'GET'
464+
})
465+
466+
expect(agent.tracer.collector.clientSend.args[0][0].resource).to.eql('/query')
467+
})
468+
469+
it('should keep queryParams if set in config', function () {
470+
this.sandbox.stub(agent, 'getConfig').returns({
471+
whiteListHosts: [
472+
'risingstack.com'
473+
],
474+
ignoreHeaders: {
475+
'user-agent': ['006', '007']
476+
},
477+
keepQueryParams: true
478+
})
479+
480+
appliedOriginal.on = this.sandbox.spy(function (name, cb) {
481+
if (name === 'response') {
482+
cb({
483+
headers: {
484+
'x-parent': '2',
485+
'x-server-send': '12345668',
486+
'x-server-receive': '12345698'
487+
},
488+
statusCode: 200
489+
})
490+
}
491+
})
492+
493+
var r = request(original, agent)
494+
495+
r({
496+
host: 'localhost',
497+
path: '/query?foo=bar',
498+
headers: {},
499+
method: 'GET'
500+
})
501+
502+
expect(agent.tracer.collector.clientSend.args[0][0].resource).to.eql('/query?foo=bar')
503+
})
442504
})

lib/instrumentations/core/http/server.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,16 @@ function httpServer (listener, agent) {
4040
var ignoreHeaders = config.ignoreHeaders
4141
var ignorePaths = config.ignorePaths
4242
var ignoreStatusCodes = config.ignoreStatusCodes
43+
var keepQueryParams = config.keepQueryParams
4344

4445
return function (request, response) {
45-
var requestUrl = request.url.split('?')[0]
46+
var resource
47+
var requestUrl = request.url
48+
if (keepQueryParams) {
49+
resource = util.formatDataUrl(requestUrl)
50+
} else {
51+
resource = request.url.split('?')[0]
52+
}
4653

4754
var headers = request.headers
4855

@@ -74,8 +81,6 @@ function httpServer (listener, agent) {
7481
communicationId: communicationId
7582
}
7683

77-
var resource = util.formatDataUrl(requestUrl)
78-
7984
var payload = {
8085
protocol: 'http',
8186
host: headers.host,

lib/instrumentations/core/http/server.spec.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,4 +458,54 @@ describe('The http.Server.prototype wrapper module', function () {
458458
response.writeHead()
459459
expect(agent.tracer.collector.serverSend.args[0][1]).to.eql(serverRecvResult.briefcase)
460460
})
461+
462+
it('should not keep queryParams by default', function () {
463+
var s = server(original, agent)
464+
465+
var request = {
466+
headers: {
467+
host: 'host',
468+
'x-span-id': 'comm-id',
469+
'x-request-id': 'tr-id',
470+
'x-parent': '1'
471+
},
472+
url: '/query?foo=bar',
473+
method: 'GET'
474+
}
475+
s(request, response)
476+
477+
response.writeHead()
478+
479+
expect(agent.tracer.collector.serverRecv.args[0][0].resource).to.eql('/query')
480+
})
481+
482+
it('should keep queryParams if set in config', function () {
483+
this.sandbox.stub(agent, 'getConfig').returns({
484+
whiteListHosts: [
485+
'risingstack.com'
486+
],
487+
ignoreHeaders: {
488+
'user-agent': ['006', '007']
489+
},
490+
keepQueryParams: true
491+
})
492+
493+
var s = server(original, agent)
494+
495+
var request = {
496+
headers: {
497+
host: 'host',
498+
'x-span-id': 'comm-id',
499+
'x-request-id': 'tr-id',
500+
'x-parent': '1'
501+
},
502+
url: '/query?foo=bar',
503+
method: 'GET'
504+
}
505+
s(request, response)
506+
507+
response.writeHead()
508+
509+
expect(agent.tracer.collector.serverRecv.args[0][0].resource).to.eql('/query?foo=bar')
510+
})
461511
})

lib/utils/configReader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ ConfigReader.prototype._getEnvVarConfig = function () {
6363
disableInstrumentations: process.env.TRACE_DISABLE_INSTRUMENTATIONS
6464
? process.env.TRACE_DISABLE_INSTRUMENTATIONS.split(',')
6565
: undefined,
66-
proxy: process.env.TRACE_PROXY || process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY
66+
proxy: process.env.TRACE_PROXY || process.env.HTTPS_PROXY || process.env.https_proxy || process.env.HTTP_PROXY,
67+
keepQueryParams: process.env.TRACE_KEEP_QUERY_PARAMS
6768
}
6869

6970
var ignoreHeaders

0 commit comments

Comments
 (0)