Skip to content

Commit 3b4425e

Browse files
committed
fix(mustCollect): use fix value for error
1 parent 3e579b0 commit 3b4425e

File tree

6 files changed

+26
-15
lines changed

6 files changed

+26
-15
lines changed

lib/agent/index.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var defaults = require('lodash.defaults')
66

77
var CollectorApi = require('./api')
88
var Metrics = require('./metrics')
9+
var consts = require('../consts')
910

1011
var REQUEST_ID = 'request-id'
1112
var SPAN_ID = 'span-id'
@@ -103,7 +104,7 @@ Agent.prototype.serverSend = function (data) {
103104
})
104105

105106
span.isSampled = (1 / this.sampleRate) > Math.random()
106-
span.isForceSampled = span.isForceSampled || !!data.mustCollect
107+
span.isForceSampled = span.isForceSampled || data.mustCollect === consts.MUST_COLLECT.ERROR
107108

108109
if (span.isForceSampled || span.isSampled) {
109110
this.spans.push(span)

lib/consts.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
MUST_COLLECT: {
3+
ERROR: '1'
4+
}
5+
}

lib/instrumentations/core/http/request.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var url = require('url')
33
var microtime = require('microtime')
44

55
var util = require('./util')
6+
var consts = require('../../../consts')
67

78
function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
89
var whiteListHosts = agent.getConfig().whiteListHosts
@@ -41,7 +42,7 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
4142

4243
if (mustCollectStore[requestId]) {
4344
debug('trace event (cs); reqId: %s, spanId: %s must collect', requestId, spanId)
44-
requestParams.headers['x-must-collect'] = '1'
45+
requestParams.headers['x-must-collect'] = consts.MUST_COLLECT.ERROR
4546
}
4647

4748
if (typeof agent.getServiceKey() !== 'undefined') {
@@ -59,7 +60,7 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
5960
url: util.formatDataUrl(requestParams.path),
6061
time: clientSendTime,
6162
method: requestParams.method,
62-
mustCollect: !!mustCollectStore[requestId]
63+
mustCollect: mustCollectStore[requestId]
6364
}
6465

6566
// Collect request start
@@ -79,7 +80,7 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
7980
spanId: spanId,
8081
host: requestParams.host,
8182
url: util.formatDataUrl(requestParams.path),
82-
mustCollect: true,
83+
mustCollect: consts.MUST_COLLECT.ERROR,
8384
err: {
8485
type: 'network-error',
8586
message: err.message,
@@ -97,8 +98,8 @@ function wrapRequest (originalHttpRequest, agent, mustCollectStore) {
9798

9899
// returns with response
99100
returned.on('response', function (incomingMessage) {
100-
mustCollectStore[requestId] = !!incomingMessage.headers['x-must-collect'] ||
101-
!!mustCollectStore[requestId]
101+
mustCollectStore[requestId] = incomingMessage.headers['x-must-collect'] ||
102+
mustCollectStore[requestId]
102103

103104
if (mustCollectStore[requestId]) {
104105
debug('trace event (cr) on response; reqId: %s, spanId: %s must collect', requestId, spanId)

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,15 @@ describe('The http.request wrapper module', function () {
9393
id: transactionId,
9494
spanId: spanId,
9595
method: 'GET',
96-
mustCollect: false,
96+
mustCollect: undefined,
9797
time: 12345678,
9898
url: '/'
9999
})
100100

101101
expect(agent.clientReceive).to.be.calledWith({
102102
host: 'localhost',
103103
id: transactionId,
104-
mustCollect: false,
104+
mustCollect: undefined,
105105
spanId: spanId,
106106
statusCode: 200,
107107
url: '/'

lib/instrumentations/core/http/server.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var microtime = require('microtime')
55
var debug = require('debug')('risingstack/trace')
66

77
var util = require('./util')
8+
var consts = require('../../../consts')
89

910
function wrapListener (listener, agent, mustCollectStore) {
1011
var ignoreHeaders = agent.getConfig().ignoreHeaders
@@ -36,7 +37,7 @@ function wrapListener (listener, agent, mustCollectStore) {
3637
// must be collected
3738
if (headers['x-must-collect']) {
3839
debug('trace event (sr); request: %s, set must collect store', requestId)
39-
mustCollectStore[requestId] = true
40+
mustCollectStore[requestId] = consts.MUST_COLLECT.ERROR
4041
}
4142

4243
// setting the spanId in cls
@@ -91,7 +92,9 @@ function wrapListener (listener, agent, mustCollectStore) {
9192
serverSendTime = microtime.now()
9293

9394
// collected because previous reason like (x-must-collect etc.) or wrong status code
94-
mustCollectStore[requestId] = mustCollectStore[requestId] || response.statusCode > 399
95+
if (response.statusCode > 399) {
96+
mustCollectStore[requestId] = consts.MUST_COLLECT.ERROR
97+
}
9598

9699
/* Service name may be unavailable due to uninitialized reporter */
97100
var serviceKey = agent.getServiceKey()
@@ -110,7 +113,7 @@ function wrapListener (listener, agent, mustCollectStore) {
110113

111114
if (mustCollectStore[requestId]) {
112115
debug('trace event (ss); request: %s x-must-collect header has been set', requestId)
113-
response.setHeader('x-must-collect', 1)
116+
response.setHeader('x-must-collect', consts.MUST_COLLECT.ERROR)
114117
}
115118

116119
originalWriteHead.apply(response, arguments)

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ var expect = require('chai').expect
22
var microtime = require('microtime')
33

44
var server = require('./server')
5+
var consts = require('../../../consts')
56

67
describe('The http.Server.prototype wrapper module', function () {
78
var mustCollectStore
@@ -143,7 +144,7 @@ describe('The http.Server.prototype wrapper module', function () {
143144
expect(agent.serverSend).to.be.calledWith({
144145
host: 'localhost',
145146
id: transactionId,
146-
mustCollect: false,
147+
mustCollect: undefined,
147148
spanId: undefined,
148149
statusCode: statusCode,
149150
time: 12345678,
@@ -166,7 +167,7 @@ describe('The http.Server.prototype wrapper module', function () {
166167
headers: {
167168
host: 'localhost',
168169
'x-span-id': spanId,
169-
'x-must-collect': 1
170+
'x-must-collect': '1'
170171
},
171172
pathname: '/',
172173
url: '/?id=2',
@@ -207,7 +208,7 @@ describe('The http.Server.prototype wrapper module', function () {
207208
id: transactionId,
208209
spanId: spanId,
209210
statusCode: statusCode,
210-
mustCollect: true,
211+
mustCollect: consts.MUST_COLLECT.ERROR,
211212
time: 12345678,
212213
url: '/',
213214
responseTime: 0
@@ -217,7 +218,7 @@ describe('The http.Server.prototype wrapper module', function () {
217218
expect(setHeaderSpy).to.be.calledWith('x-client-send', 12345678)
218219
expect(setHeaderSpy).to.be.calledWith('x-parent', 0)
219220
expect(setHeaderSpy).to.be.calledWith('x-span-id', spanId)
220-
expect(setHeaderSpy).to.be.calledWith('x-must-collect', 1)
221+
expect(setHeaderSpy).to.be.calledWith('x-must-collect', '1')
221222
expect(writeHeadSpy).to.be.calledOnce
222223
})
223224
})

0 commit comments

Comments
 (0)