Skip to content

Commit 488da05

Browse files
committed
fix(instrumentation): fix http request emitter context propagation
1 parent 4258e86 commit 488da05

3 files changed

Lines changed: 26 additions & 12 deletions

File tree

example/config/trace.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module.exports = {
66
serviceName: 'your-awesome-app',
7-
apiKey: 'KEEP_ME_SECRET',
7+
apiKey: 'KEEP.ME.SECRET',
88
ignoreHeaders: {
99
'user-agent': ['007']
1010
},

lib/instrumentations/core/http/request.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,37 @@ function httpClient (originalHttpRequest, agent) {
1818

1919
// parse request
2020
if (typeof requestParams === 'string') {
21-
requestParams = url.parse(requestParams)
22-
requestParams.method = 'GET'
21+
var parsedParams = url.parse(requestParams)
22+
if (!parsedParams.hostname) {
23+
return originalHttpRequest.apply(this, arguments)
24+
} else {
25+
requestParams = parsedParams
26+
}
2327
}
2428

25-
if (requestParams.hostname) {
26-
requestParams.host = requestParams.hostname
29+
if (typeof requestParams !== 'object') {
30+
return originalHttpRequest.apply(this, arguments)
2731
}
2832

33+
requestParams.method = (requestParams.method || 'GET').toUpperCase()
34+
requestParams.host = requestParams.host || requestParams.hostname || 'localhost'
35+
requestParams.path = requestParams.path || '/'
36+
2937
// call original and do not collect
3038
if (whiteListHosts.indexOf(requestParams.host) > -1) {
3139
return originalHttpRequest.apply(this, arguments)
3240
}
3341

3442
var resource
3543
if (keepQueryParams) {
36-
var requestUrl = requestParams.path
37-
resource = util.formatDataUrl(requestUrl)
44+
resource = util.formatDataUrl(requestParams.path)
3845
} else {
39-
resource = requestParams.path.split('?')[0]
46+
var i = requestParams.path.indexOf('?')
47+
if (i >= 0) {
48+
resource = requestParams.path.substring(0, i)
49+
} else {
50+
resource = requestParams.path
51+
}
4052
}
4153

4254
var cs = collector.clientSend({
@@ -78,6 +90,8 @@ function httpClient (originalHttpRequest, agent) {
7890

7991
var returned = originalHttpRequest.apply(this, arguments)
8092

93+
agent.storage.bindEmitter(returned)
94+
8195
// returns with error
8296
returned.on('error', function (err) {
8397
debug('#httpClient', format('NE(%s) [%s]', csCtx.communicationId, err.code))
@@ -129,8 +143,6 @@ function httpClient (originalHttpRequest, agent) {
129143
}
130144
})
131145

132-
agent.storage.bind(returned)
133-
134146
return returned
135147
}
136148
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('The http.request wrapper module', function () {
6161
get: this.sandbox.stub().returns({
6262
communication: {}
6363
}),
64-
bind: this.sandbox.spy()
64+
bindEmitter: this.sandbox.spy()
6565
}
6666
}
6767
})
@@ -76,7 +76,9 @@ describe('The http.request wrapper module', function () {
7676
expect(agent.tracer.collector.clientSend).not.to.be.called
7777

7878
expect(original).to.be.calledWith({
79-
host: 'risingstack.com'
79+
host: 'risingstack.com',
80+
method: 'GET',
81+
path: '/'
8082
})
8183
})
8284

0 commit comments

Comments
 (0)