Skip to content
This repository was archived by the owner on Oct 23, 2023. It is now read-only.

Add sampleRate config option #292

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 @@ -96,6 +96,17 @@ Those configuration options are documented below:
}
}

.. 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
3 changes: 3 additions & 0 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ extend(Raven.prototype, {
this.loggerName = options.logger || '';
this.dataCallback = options.dataCallback;
this.shouldSendCallback = options.shouldSendCallback;
this.sampleRate = typeof options.sampleRate === 'undefined' ? 1 : options.sampleRate;
this.parseUser = options.parseUser;

if (!this.dsn) {
Expand Down Expand Up @@ -188,11 +189,13 @@ extend(Raven.prototype, {
var shouldSend = true;
if (!this._enabled) shouldSend = false;
if (this.shouldSendCallback && !this.shouldSendCallback(kwargs)) shouldSend = false;
if (Math.random() >= this.sampleRate) shouldSend = false;

if (shouldSend) {
this.send(kwargs, cb);
} else {
// wish there was a good way to communicate to cb why we didn't send; worth considering cb api change?
// could be shouldSendCallback, could be disabled, could be sample rate
// avoiding setImmediate here because node 0.8
cb && setTimeout(function () {
cb(null, eventId);
Expand Down
79 changes: 79 additions & 0 deletions test/raven.client.js
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,85 @@ describe('raven.Client', function () {
});
});

describe('sampleRate', function () {
var origRandom;
beforeEach(function () {
origRandom = Math.random;
Math.random = function () {
return 0.5;
};
});

afterEach(function () {
Math.random = origRandom;
});

it('should respect sampleRate to omit event', function (done) {
client = new raven.Client(dsn, {
sampleRate: 0.3
});

client.process({
message: 'test'
}, function (err, eventId) {
setTimeout(done, 10);
});
});

it('should respect sampleRate to include event', function (done) {
var scope = nock('https://app.getsentry.com')
.filteringRequestBody(/.*/, '*')
.post('/api/269/store/', '*')
.reply(200, function (uri, body) {
zlib.inflate(new Buffer(body, 'base64'), function (err, dec) {
if (err) return done(err);
var msg = JSON.parse(dec.toString());
var extra = msg.extra;

extra.should.have.property('foo');
done();
});
return 'OK';
});

client = new raven.Client(dsn, {
sampleRate: 0.8
});

client.process({
message: 'test',
extra: {
foo: 'bar'
}
});

client.on('logged', function () {
scope.done();
});
});

it('should always send if sampleRate is omitted', function (done) {
var scope = nock('https://app.getsentry.com')
.filteringRequestBody(/.*/, '*')
.post('/api/269/store/', '*')
.reply(200, function (uri, body) {
zlib.inflate(new Buffer(body, 'base64'), function (err, dec) {
if (err) return done(err);
done();
});
return 'OK';
});

client.process({
message: 'test'
});

client.on('logged', function () {
scope.done();
});
});
});

it('should call the callback after sending', function (done) {
var firedCallback = false;
var sentResponse = false;
Expand Down