Skip to content

Commit 9805b06

Browse files
authored
Merge pull request #885 from getsentry/sample-rate
Add sampleRate config option
2 parents 0d040fc + 5f5d354 commit 9805b06

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

docs/config.rst

+11
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,17 @@ Those configuration options are documented below:
145145
includePaths: [/https?:\/\/getsentry\.com/, /https?:\/\/cdn\.getsentry\.com/]
146146
}
147147
148+
.. describe:: sampleRate
149+
150+
A sampling rate to apply to events. A value of 0.0 will send no events,
151+
and a value of 1.0 will send all events (default).
152+
153+
.. code-block:: javascript
154+
155+
{
156+
sampleRate: 0.5 // send 50% of events, drop the other half
157+
}
158+
148159
.. describe:: dataCallback
149160

150161
A function that allows mutation of the data payload right before being

src/raven.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function Raven() {
5252
collectWindowErrors: true,
5353
maxMessageLength: 0,
5454
stackTraceLimit: 50,
55-
autoBreadcrumbs: true
55+
autoBreadcrumbs: true,
56+
sampleRate: 1
5657
};
5758
this._ignoreOnError = 0;
5859
this._isRavenInstalled = false;
@@ -1489,7 +1490,13 @@ Raven.prototype = {
14891490
return;
14901491
}
14911492

1492-
this._sendProcessedPayload(data);
1493+
if (typeof globalOptions.sampleRate === 'number') {
1494+
if (Math.random() < globalOptions.sampleRate) {
1495+
this._sendProcessedPayload(data);
1496+
}
1497+
} else {
1498+
this._sendProcessedPayload(data);
1499+
}
14931500
},
14941501

14951502
_getUuid: function () {

test/raven.test.js

+22
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)