Skip to content

Commit f4add9d

Browse files
peteyyczgergelyke
authored andcommitted
fix(sampleRate): fix sample rate calculation
1 parent abb8daf commit f4add9d

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

lib/providers/httpTransaction/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function HttpTransaction(eventBus, options) {
2626
this.partials = {};
2727
this.traces = [];
2828
this.rpmMetrics = {};
29+
this.totalRequestCount = 0;
2930

3031
this.on(HttpTransaction.CLIENT_RECV, this.onClientReceive);
3132
this.on(HttpTransaction.CLIENT_SEND, this.onClientSend);
@@ -150,9 +151,11 @@ HttpTransaction.prototype.onServerSend = function (data) {
150151
type: HttpTransaction.SERVER_SEND
151152
});
152153

153-
var isSampled = trace.isSampled || this.sampleRate > Math.random();
154+
var isSampled = trace.isSampled || (1 / this.sampleRate) > Math.random();
154155
if (data.mustCollect || isSampled) {
156+
trace.isForceSampled = !!trace.isSampled;
155157
delete trace.isSampled;
158+
156159
this.traces.push(trace);
157160
}
158161

@@ -169,6 +172,7 @@ HttpTransaction.prototype.collectStatusCode = function (trace) {
169172
};
170173

171174
HttpTransaction.prototype.onServerReceive = function (data) {
175+
this.totalRequestCount++;
172176
var headers = data.headers;
173177
var spanId = headers['x-span-id'];
174178
var parentId = headers['x-parent'];
@@ -222,8 +226,10 @@ HttpTransaction.prototype._send = function (options) {
222226
var dataBag = {};
223227
dataBag.sampleRate = this.sampleRate;
224228
dataBag.samples = cloneDeep(this.traces);
229+
dataBag.totalRequestCount = this.totalRequestCount;
225230

226-
this.sampleRate = Math.floor(dataBag.samples.length / this.sampleSize) || 1;
231+
this.sampleRate = Math.floor(this.totalRequestCount / this.sampleSize) || 1;
232+
this.totalRequestCount = 0;
227233

228234
this.traces = [];
229235

lib/providers/httpTransaction/index.spec.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ describe('The HttpTransaction module', function () {
2323

2424
it('can be instantiated', function () {
2525
var httpTransaction = HttpTransaction.create(eventBus, {});
26+
expect(httpTransaction.totalRequestCount).to.be.eql(0);
2627
expect(httpTransaction).to.be.ok;
2728
});
2829

@@ -48,6 +49,45 @@ describe('The HttpTransaction module', function () {
4849
expect(httpTransaction.partials[traceId].events[0].data).to.eql(data);
4950
});
5051

52+
it('emits a send event onto the event bus', function () {
53+
var emitStub = this.sandbox.stub(eventBus, 'emit');
54+
55+
var httpTransaction = HttpTransaction.create(eventBus, {
56+
service: 'aladdin'
57+
});
58+
59+
var options = {
60+
isSync: false
61+
};
62+
63+
var totalRequestCount = 10000;
64+
var sampleSize = 60;
65+
var sampleRate = 1;
66+
var newSampleRate = Math.floor(totalRequestCount / sampleSize);
67+
68+
var databag = {
69+
sampleRate: sampleRate,
70+
samples: [
71+
1
72+
],
73+
totalRequestCount: totalRequestCount
74+
};
75+
76+
httpTransaction.totalRequestCount = totalRequestCount;
77+
httpTransaction.sampleSize = sampleSize;
78+
httpTransaction.traces = [
79+
1
80+
];
81+
82+
httpTransaction._send(options);
83+
84+
expect(httpTransaction.totalRequestCount).to.be.eql(0);
85+
expect(httpTransaction.sampleRate).to.be.eql(newSampleRate);
86+
87+
expect(emitStub).to.be.calledOnce;
88+
expect(emitStub).to.be.calledWith(eventBus.HTTP_TRANSACTION_STACK_TRACE, databag);
89+
});
90+
5191
describe('events', function () {
5292

5393
it('stores the "ClientReceive" events w/ `x-span-id`', function () {
@@ -179,6 +219,8 @@ describe('The HttpTransaction module', function () {
179219
]
180220
}
181221
});
222+
223+
expect(httpTransaction.totalRequestCount).to.be.eql(1);
182224
});
183225

184226
it('stores the "ServerRecieve" events with parent and timing data', function () {
@@ -282,6 +324,7 @@ describe('The HttpTransaction module', function () {
282324
span: url,
283325
host: host,
284326
trace: id,
327+
isForceSampled: false,
285328
service: service,
286329
statusCode: 301,
287330
events: [{

0 commit comments

Comments
 (0)