Skip to content

Commit 5f5d354

Browse files
committed
Add sampleRate config test, make sample check more defensive
1 parent c7875b1 commit 5f5d354

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/raven.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,10 +1490,13 @@ Raven.prototype = {
14901490
return;
14911491
}
14921492

1493-
if (Math.random() < globalOptions.sampleRate) {
1493+
if (typeof globalOptions.sampleRate === 'number') {
1494+
if (Math.random() < globalOptions.sampleRate) {
1495+
this._sendProcessedPayload(data);
1496+
}
1497+
} else {
14941498
this._sendProcessedPayload(data);
14951499
}
1496-
14971500
},
14981501

14991502
_getUuid: function () {

test/raven.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,28 @@ describe('globals', function() {
841841
});
842842
});
843843

844+
it('should respect `globalOptions.sampleRate` to omit event', function() {
845+
Raven._globalOptions.sampleRate = 0.5;
846+
this.sinon.stub(Math, 'random').returns(0.8);
847+
this.sinon.stub(Raven, '_sendProcessedPayload');
848+
Raven._send({message: 'bar'});
849+
assert.isFalse(Raven._sendProcessedPayload.called);
850+
});
851+
852+
it('should respect `globalOptions.sampleRate` to include event', function() {
853+
Raven._globalOptions.sampleRate = 0.5;
854+
this.sinon.stub(Math, 'random').returns(0.3);
855+
this.sinon.stub(Raven, '_sendProcessedPayload');
856+
Raven._send({message: 'bar'});
857+
assert.isTrue(Raven._sendProcessedPayload.called);
858+
});
859+
860+
it('should always send if `globalOptions.sampleRate` is omitted', function() {
861+
this.sinon.stub(Raven, '_makeRequest');
862+
Raven._send({message: 'bar'});
863+
assert.isTrue(Raven._makeRequest.called);
864+
});
865+
844866
it('should strip empty tags', function() {
845867
this.sinon.stub(Raven, 'isSetup').returns(true);
846868
this.sinon.stub(Raven, '_makeRequest');

0 commit comments

Comments
 (0)