From c7875b1ede6b9ec913f3cca1213cfaaa8283a41e Mon Sep 17 00:00:00 2001 From: Lewis Ellis Date: Thu, 9 Mar 2017 19:18:00 -0800 Subject: [PATCH 1/2] Add sampleRate config option --- docs/config.rst | 11 +++++++++++ src/raven.js | 8 ++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/docs/config.rst b/docs/config.rst index b5577be02e03..8bfe6f1a4dc5 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -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 diff --git a/src/raven.js b/src/raven.js index f0991d9c0967..7921f62086b1 100644 --- a/src/raven.js +++ b/src/raven.js @@ -52,7 +52,8 @@ function Raven() { collectWindowErrors: true, maxMessageLength: 0, stackTraceLimit: 50, - autoBreadcrumbs: true + autoBreadcrumbs: true, + sampleRate: 1 }; this._ignoreOnError = 0; this._isRavenInstalled = false; @@ -1489,7 +1490,10 @@ Raven.prototype = { return; } - this._sendProcessedPayload(data); + if (Math.random() < globalOptions.sampleRate) { + this._sendProcessedPayload(data); + } + }, _getUuid: function () { From 5f5d3540550b0c86f7f1dda84285510678baa1f3 Mon Sep 17 00:00:00 2001 From: Lewis Ellis Date: Tue, 14 Mar 2017 17:21:27 -0700 Subject: [PATCH 2/2] Add sampleRate config test, make sample check more defensive --- src/raven.js | 7 +++++-- test/raven.test.js | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/raven.js b/src/raven.js index 7921f62086b1..0bf40a0adbbd 100644 --- a/src/raven.js +++ b/src/raven.js @@ -1490,10 +1490,13 @@ Raven.prototype = { return; } - if (Math.random() < globalOptions.sampleRate) { + if (typeof globalOptions.sampleRate === 'number') { + if (Math.random() < globalOptions.sampleRate) { + this._sendProcessedPayload(data); + } + } else { this._sendProcessedPayload(data); } - }, _getUuid: function () { diff --git a/test/raven.test.js b/test/raven.test.js index 4877f7cbf798..cd182c52bff4 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -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');