Skip to content

Add sampleRate config option #885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ Those configuration options are documented below:
includePaths: [/https?:\/\/getsentry\.com/, /https?:\/\/cdn\.getsentry\.com/]
}

.. describe:: sampleRate

A sampling rate to apply to events. A value of 0.0 will send no events,
and a value of 1.0 will send all events (default).

.. code-block:: javascript

{
sampleRate: 0.5 // send 50% of events, drop the other half
}

.. describe:: dataCallback

A function that allows mutation of the data payload right before being
Expand Down
11 changes: 9 additions & 2 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ function Raven() {
collectWindowErrors: true,
maxMessageLength: 0,
stackTraceLimit: 50,
autoBreadcrumbs: true
autoBreadcrumbs: true,
sampleRate: 1
};
this._ignoreOnError = 0;
this._isRavenInstalled = false;
Expand Down Expand Up @@ -1489,7 +1490,13 @@ Raven.prototype = {
return;
}

this._sendProcessedPayload(data);
if (typeof globalOptions.sampleRate === 'number') {
if (Math.random() < globalOptions.sampleRate) {
this._sendProcessedPayload(data);
}
} else {
this._sendProcessedPayload(data);
}
},

_getUuid: function () {
Expand Down
22 changes: 22 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,28 @@ describe('globals', function() {
});
});

it('should respect `globalOptions.sampleRate` to omit event', function() {
Raven._globalOptions.sampleRate = 0.5;
this.sinon.stub(Math, 'random').returns(0.8);
this.sinon.stub(Raven, '_sendProcessedPayload');
Raven._send({message: 'bar'});
assert.isFalse(Raven._sendProcessedPayload.called);
});

it('should respect `globalOptions.sampleRate` to include event', function() {
Raven._globalOptions.sampleRate = 0.5;
this.sinon.stub(Math, 'random').returns(0.3);
this.sinon.stub(Raven, '_sendProcessedPayload');
Raven._send({message: 'bar'});
assert.isTrue(Raven._sendProcessedPayload.called);
});

it('should always send if `globalOptions.sampleRate` is omitted', function() {
this.sinon.stub(Raven, '_makeRequest');
Raven._send({message: 'bar'});
assert.isTrue(Raven._makeRequest.called);
});

it('should strip empty tags', function() {
this.sinon.stub(Raven, 'isSetup').returns(true);
this.sinon.stub(Raven, '_makeRequest');
Expand Down