Skip to content

can set maxEventsPerPage config option #767

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 5 additions & 1 deletion docs/config.rst
Original file line number Diff line number Diff line change
@@ -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:
@@ -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
-----------------------

17 changes: 17 additions & 0 deletions src/raven.js
Original file line number Diff line number Diff line change
@@ -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
@@ -875,6 +876,9 @@ Raven.prototype = {
var parsedTo = parseUrl(to);
var parsedFrom = parseUrl(from);

// refresh max events count
this._sentEvents = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If breadcrumbs are disabled, this won't get called / won't reset.


// 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
@@ -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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above.


// url argument is optional
if (url) {
// coerce to string (this is what pushState does)
@@ -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);
51 changes: 51 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
@@ -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() {
3 changes: 3 additions & 0 deletions typescript/raven.d.ts
Original file line number Diff line number Diff line change
@@ -71,6 +71,9 @@ declare module Raven {

/** Enables/disables automatic collection of breadcrumbs. */
autoBreadcrumbs?: boolean | AutoBreadcrumbOptions

/** 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. */
maxEventsPerPage?: number;
}

interface RavenInstrumentationOptions {