From 00db78bf8df83f2d88a5c8f13fd4bb10dbec5e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joona=20P=C3=A4=C3=A4kk=C3=B6nen?= Date: Sat, 7 Oct 2017 20:01:32 +0300 Subject: [PATCH] Added skipSampleRate option to Raven.captureMessage and Raven.captureException --- docs/usage.rst | 17 +++++++++++++++++ src/raven.js | 5 ++++- test/raven.test.js | 24 ++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/docs/usage.rst b/docs/usage.rst index 21c59fa128ce..f673671b70c3 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -94,6 +94,23 @@ Capturing Messages Raven.captureMessage('Broken!') +Passing options +----------------------- + +The ``captureMessage`` and ``captureException`` both allow options to be passed for them + +.. describe:: skipSampleRate + + Skips sampling rate checking this message and always sends it. Default: ``false`` + + .. code-block:: javascript + + Raven.captureMessage('Something happened', { + options: { + skipSampleRate: true + } + }); + .. _raven-js-additional-context: Passing Additional Data diff --git a/src/raven.js b/src/raven.js index 5f79ebcb0ac9..ea75b4ca7171 100644 --- a/src/raven.js +++ b/src/raven.js @@ -1635,6 +1635,9 @@ Raven.prototype = { _send: function(data) { var globalOptions = this._globalOptions; + var options = data.options || {}; + // delete `options` from data to so it doesn't get send with the outbound payload + delete data.options; var baseData = { project: this._globalProject, @@ -1708,7 +1711,7 @@ Raven.prototype = { return; } - if (typeof globalOptions.sampleRate === 'number') { + if (typeof globalOptions.sampleRate === 'number' && !options.skipSampleRate) { if (Math.random() < globalOptions.sampleRate) { this._sendProcessedPayload(data); } diff --git a/test/raven.test.js b/test/raven.test.js index 01d9ea29cf57..66986536d25a 100644 --- a/test/raven.test.js +++ b/test/raven.test.js @@ -2627,6 +2627,18 @@ describe('Raven (public API)', function() { assert.isTrue(Raven._send.calledOnce); }); + it('should respect `skipSampleRate` option', function() { + this.sinon.stub(Raven, '_makeRequest'); + setupRaven(); + Raven._globalOptions.sampleRate = 0.0; + Raven.captureMessage('This message should be sent', { + options: { + skipSampleRate: true + } + }); + assert.equal(Raven._makeRequest.callCount, 1); + }); + it('should not throw an error if not configured', function() { this.sinon.stub(Raven, 'isSetup').returns(false); this.sinon.stub(Raven, '_send'); @@ -2750,6 +2762,18 @@ describe('Raven (public API)', function() { Raven.captureException(new Error('err')); }); }); + + it('should respect `skipSampleRate`', function() { + this.sinon.stub(Raven, '_makeRequest'); + setupRaven(); + Raven._globalOptions.sampleRate = 0.0; + Raven.captureException(new Error('This error should be sent'), { + options: { + skipSampleRate: true + } + }); + assert.equal(Raven._makeRequest.callCount, 1); + }); }); describe('.captureBreadcrumb', function() {