Skip to content

Commit

Permalink
feat: maxEventsPerPage config option
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxBittker authored and kamilogorek committed Sep 12, 2017
1 parent 1c4da6a commit 4eb7a2c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
6 changes: 5 additions & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,6 @@ Those configuration options are documented below:

If set to `true`, Raven.js outputs some light debugging information onto the console.


.. describe:: instrument

Enables/disables instrumentation of globals. Possible values are:
Expand All @@ -314,6 +313,11 @@ Those configuration options are documented below:
'tryCatch': true, // Instruments timers and event targets
}
.. describe:: maxEventsPerPage

By default, Raven captures as many events as possible. If you want to reduce this number, you can change
it by setting `maxEventsPerPage`. The counter will automatically restart on every page change.

Putting it all together
-----------------------

Expand Down
17 changes: 17 additions & 0 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function Raven() {
this._keypressTimeout;
this._location = _window.location;
this._lastHref = this._location && this._location.href;
this._sentEvents = 0;
this._resetBackoff();

// eslint-disable-next-line guard-for-in
Expand Down Expand Up @@ -875,6 +876,9 @@ Raven.prototype = {
var parsedTo = parseUrl(to);
var parsedFrom = parseUrl(from);

// refresh max events count
this._sentEvents = 0;

// because onpopstate only tells you the "new" (to) value of location.href, and
// not the previous (from) value, we need to track the value of the current URL
// state ourselves
Expand Down Expand Up @@ -1255,6 +1259,9 @@ Raven.prototype = {
return function(/* state, title, url */) {
var url = arguments.length > 2 ? arguments[2] : undefined;

// refresh max events count
self._sentEvents = 0;

// url argument is optional
if (url) {
// coerce to string (this is what pushState does)
Expand Down Expand Up @@ -1699,13 +1706,23 @@ Raven.prototype = {
return;
}

// Check if the request should be filtered due to max events per page
if (
globalOptions.maxEventsPerPage &&
this._sentEvents >= globalOptions.maxEventsPerPage
) {
return;
}

// Backoff state: Sentry server previously responded w/ an error (e.g. 429 - too many requests),
// so drop requests until "cool-off" period has elapsed.
if (this._shouldBackoff()) {
this._logDebug('warn', 'Raven dropped error due to backoff: ', data);
return;
}

this._sentEvents++; // failed events count towards maxEvents

if (typeof globalOptions.sampleRate === 'number') {
if (Math.random() < globalOptions.sampleRate) {
this._sendProcessedPayload(data);
Expand Down
51 changes: 51 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,57 @@ describe('Raven (public API)', function() {
});
});
});

describe('maxEventsPerPage', function(){
it('allows many events when maxEventsPerPage is undefined', function () {
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
this.sinon.spy(stub)

Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))

assert.equal(Raven._sendProcessedPayload.callCount, 5);
});

it('should only allow up to maxEventsPerPage requests', function () {
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
this.sinon.spy(stub)

Raven._globalOptions.maxEventsPerPage = 3;

Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))

assert.equal(Raven._sendProcessedPayload.callCount, 3);
});

it('should reset maxErrors on SPA page change', function () {
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
this.sinon.spy(stub)

Raven._globalOptions.maxEventsPerPage = 3;

Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))

assert.equal(Raven._sendProcessedPayload.callCount, 3);

Raven._captureUrlChange('/foo', '/bar');

Raven.captureException(new Error('foo'))
Raven.captureException(new Error('foo'))

assert.equal(Raven._sendProcessedPayload.callCount, 5);
});
});
});

describe('.wrap', function() {
Expand Down

0 comments on commit 4eb7a2c

Please sign in to comment.