Skip to content

Commit 2329ad7

Browse files
committed
fix(collector): sample according to TRN-41
1 parent 071a45b commit 2329ad7

9 files changed

Lines changed: 220 additions & 233 deletions

File tree

lib/agent/tracer/collector.js

Lines changed: 153 additions & 168 deletions
Large diffs are not rendered by default.

lib/agent/tracer/collector.spec.js

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,6 @@ describe('Collector', function () {
2727
parentServiceKey: 8,
2828
timestamp: 1
2929
}
30-
it('should set default severity', function () {
31-
var incompleteDuffelBag = { }
32-
33-
var tracer = new Collector(options)
34-
35-
var id = 'uuid'
36-
37-
this.sandbox.stub(uuid, 'v4', function () {
38-
return id
39-
})
40-
41-
var result = tracer.serverRecv(payload, incompleteDuffelBag)
42-
expect(result.briefcase.severity).to.eql(tracer.defaultSeverity)
43-
})
4430

4531
it('should fill missing communication info', function () {
4632
var incompleteDuffelBag = { }
@@ -76,19 +62,6 @@ describe('Collector', function () {
7662
})
7763
})
7864

79-
it('should set severity', function () {
80-
var tracer = new Collector(options)
81-
82-
var id = 'uuid'
83-
84-
this.sandbox.stub(uuid, 'v4', function () {
85-
return id
86-
})
87-
88-
var result = tracer.serverRecv(payload, duffelBag)
89-
expect(result.briefcase.severity).to.eql(duffelBag.severity)
90-
})
91-
9265
it('should not be collected', function () {
9366
var tracer = new Collector(options)
9467

@@ -238,14 +211,14 @@ describe('Collector', function () {
238211
status: 'ok',
239212
data: {
240213
statusCode: 200
241-
}
214+
},
215+
severity: tracer.mustCollectSeverity
242216
},
243217
briefcase: {
244218
communication: {
245219
id: 'communicationId',
246220
transactionId: 'transactionId'
247-
},
248-
severity: tracer.mustCollectSeverity
221+
}
249222
}
250223
}
251224
tracer.serverSend(ss.payload, ss.briefcase)
@@ -289,13 +262,14 @@ describe('Collector', function () {
289262
}
290263
}
291264
var ss = {
292-
payload: { },
265+
payload: {
266+
severity: tracer.mustCollectSeverity
267+
},
293268
briefcase: {
294269
communication: {
295270
id: sr.duffelBag.communicationId,
296271
transactionId: sr.duffelBag.transactionId
297-
},
298-
severity: tracer.mustCollectSeverity
272+
}
299273
}
300274
}
301275

@@ -375,14 +349,14 @@ describe('Collector', function () {
375349
action: 'action',
376350
resource: 'resource',
377351
host: 'host',
378-
data: { }
352+
data: { },
353+
severity: tracer.mustCollectSeverity
379354
}
380355
var briefcase = {
381356
communication: {
382357
id: 'communicationId',
383358
transactionId: 'transactionId'
384-
},
385-
severity: tracer.mustCollectSeverity
359+
}
386360
}
387361
var id = 'uuid'
388362

@@ -421,18 +395,16 @@ describe('Collector', function () {
421395
describe('CS - CR', function () {
422396
it('is collected when not in a transaction', function () {
423397
var tracer = new Collector(options)
424-
var payload = { }
425-
var briefcase = {
426-
severity: tracer.mustCollectSeverity
427-
}
398+
var payload = { severity: tracer.mustCollectSeverity }
399+
var briefcase = { }
428400
var cs = tracer.clientSend(payload, briefcase)
429401

430402
tracer.clientRecv({}, {}, cs.briefcase)
431403
expect(tracer.collect().length).to.eql(2)
432404
})
433405
it('two different ones do not affect each other', function () {
434406
var tracer = new Collector(options)
435-
var cs1 = tracer.clientSend({ }, { severity: tracer.mustCollectSeverity })
407+
var cs1 = tracer.clientSend({ severity: tracer.mustCollectSeverity }, { })
436408
tracer.clientRecv({}, {}, cs1.briefcase)
437409

438410
var cs2 = tracer.clientSend({ }, { })
@@ -478,10 +450,10 @@ describe('Collector', function () {
478450
}
479451
}
480452
var duffelBag = {
453+
severity: tracer.mustCollectSeverity,
481454
timestamp: 1
482455
}
483456
var briefcase = {
484-
severity: tracer.mustCollectSeverity,
485457
communication: {
486458
id: 'id',
487459
transactionId: 'id'
@@ -648,7 +620,7 @@ describe('Collector', function () {
648620
})
649621

650622
describe('Sampling', function () {
651-
it('happens when there is a big amount of data', function () {
623+
it('happens when there is a big amount of data #1', function () {
652624
this.sandbox.stub(microtime, 'now', function () {
653625
return 2
654626
})
@@ -659,25 +631,49 @@ describe('Collector', function () {
659631
duffelBag: {
660632
communicationId: 'commId',
661633
transactionId: 'trId',
662-
severity: tracer.mustCollectSeverity + 1
634+
severity: tracer.mustCollectSeverity
663635
}
664636
}
665637
var ss = {
666-
payload: { },
638+
payload: { severity: tracer.mustCollectSeverity },
667639
briefcase: {
668640
communication: {
669641
id: sr.duffelBag.communicationId,
670642
transactionId: sr.duffelBag.transactionId
671-
},
672-
severity: tracer.mustCollectSeverity
643+
}
673644
}
674645
}
675646

676647
tracer.serverRecv(sr.payload, sr.duffelBag)
677648
tracer.serverSend(ss.payload, ss.briefcase)
678649
}
679-
for (var i = 0; i < 100; ++i) { report() }
680-
expect(tracer.collect().length).to.be.eql(20)
650+
for (var i = 0; i < 50; ++i) { report() }
651+
var length = tracer.collect().length
652+
expect(length).to.be.below(50)
653+
expect(length).to.be.above(0)
654+
})
655+
656+
it('happens when there is a big amount of data #2', function () {
657+
this.sandbox.stub(microtime, 'now', function () {
658+
return 2
659+
})
660+
var tracer = new Collector(assign({ samplerLimit: 10 }, options))
661+
function report () {
662+
var cs = {
663+
payload: { severity: tracer.mustCollectSeverity },
664+
briefcase: { }
665+
}
666+
var csResult = tracer.clientSend(cs.payload, cs.briefcase)
667+
668+
var cr = {
669+
payload: { }
670+
}
671+
tracer.clientRecv(cr.payload, {}, csResult.briefcase)
672+
}
673+
for (var i = 0; i < 50; ++i) { report() }
674+
var length = tracer.collect().length
675+
expect(length).to.be.below(50)
676+
expect(length).to.be.above(0)
681677
})
682678
})
683679
})

lib/instrumentations/core/http/request.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ function httpClient (originalHttpRequest, agent) {
1313
var externalEdgeMetrics = agent.externalEdgeMetrics
1414
var briefcase = agent.storage.get('tracer.briefcase')
1515
var communication = briefcase && briefcase.communication
16-
var severity = briefcase && briefcase.severity
1716

1817
// parse request
1918
if (typeof requestParams === 'string') {
@@ -36,10 +35,10 @@ function httpClient (originalHttpRequest, agent) {
3635
protocol: 'http',
3736
host: requestParams.host,
3837
resource: resource,
39-
action: requestParams.method
38+
action: requestParams.method,
39+
severity: collector.defaultSeverity
4040
}, {
41-
communication: communication,
42-
severity: severity
41+
communication: communication
4342
})
4443

4544
var csCtx = cs.briefcase.csCtx

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ describe('The http.request wrapper module', function () {
9494
host: 'localhost',
9595
action: 'GET',
9696
protocol: 'http',
97-
resource: '/'
98-
}, { communication: {}, severity: undefined })
97+
resource: '/',
98+
severity: agent.tracer.collector.defaultSeverity
99+
}, { communication: {} })
99100
})
100101

101102
it('sets headers, does not set mustCollect header', function () {

lib/instrumentations/core/http/server.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,10 @@ function httpServer (listener, agent) {
7777
var resource = util.formatDataUrl(requestUrl)
7878

7979
var payload = {
80+
protocol: 'http',
8081
host: headers.host,
8182
action: request.method,
82-
resource: resource,
83-
protocol: 'http'
83+
resource: resource
8484
}
8585

8686
var sr = collector.serverRecv(payload, duffelBag)
@@ -121,7 +121,10 @@ function httpServer (listener, agent) {
121121
status: status,
122122
data: {
123123
statusCode: statusCode
124-
}
124+
},
125+
severity: status === 'bad'
126+
? collector.mustCollectSeverity
127+
: collector.defaultSeverity
125128
}, tracerBriefcase, { skip: skipCollecting })
126129

127130
var mustCollect = collector.LEVELS.gte(ss.duffelBag.severity, collector.mustCollectSeverity)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ describe('The http.Server.prototype wrapper module', function () {
247247
expect(agent.tracer.collector.serverSend.args[0][0]).to.eql({
248248
data: { statusCode: 401 },
249249
protocol: 'http',
250-
status: 'bad'
250+
status: 'bad',
251+
severity: agent.tracer.collector.mustCollectSeverity
251252
})
252253
})
253254

lib/instrumentations/utils/wrapQuery.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ function wrapQuery (original, args, agent, params) {
2121
action: _params.method,
2222
resource: _params.url,
2323
host: _params.host,
24-
data: _params.protocolSpecific
25-
}, {
26-
communication: communication,
24+
data: _params.protocolSpecific,
2725
severity: severity
26+
}, {
27+
communication: communication
2828
})
2929

3030
debug('#wrapQuery', format('CS(%s) [%s %s %s %s]',

lib/instrumentations/utils/wrapQuery.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ describe('wrapQuery', function () {
5757
data: undefined,
5858
host: 'fakeHost',
5959
protocol: 'protocol',
60-
resource: 'fakeUrl'
60+
resource: 'fakeUrl',
61+
severity: agent.tracer.collector.defaultSeverity
6162
}, {
62-
communication: {},
63-
severity: undefined
63+
communication: {}
6464
})
6565
})
6666
it('should call the original', function () {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@
5757
"lodash.get": "4.4.2",
5858
"lodash.isnumber": "3.0.3",
5959
"lodash.uniq": "4.5.0",
60+
"lodash.remove": "4.7.0",
61+
"lodash.some": "^4.6.0",
6062
"qs": "6.3.0",
6163
"semver": "5.3.0",
6264
"sync-request": "3.0.1",

0 commit comments

Comments
 (0)