Skip to content

Commit 9ddadf0

Browse files
MaxBittkerkamilogorek
authored andcommitted
feat: maxEventsPerPage config option
1 parent 1c4da6a commit 9ddadf0

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

docs/config.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ Those configuration options are documented below:
299299

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

302-
303302
.. describe:: instrument
304303

305304
Enables/disables instrumentation of globals. Possible values are:
@@ -314,6 +313,11 @@ Those configuration options are documented below:
314313
'tryCatch': true, // Instruments timers and event targets
315314
}
316315
316+
.. describe:: maxEventsPerPage
317+
318+
By default, Raven captures as many events as possible. If you want to reduce this number, you can change
319+
it by setting `maxEventsPerPage`. The counter will automatically restart on every page change.
320+
317321
Putting it all together
318322
-----------------------
319323

src/raven.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function Raven() {
8181
this._keypressTimeout;
8282
this._location = _window.location;
8383
this._lastHref = this._location && this._location.href;
84+
this._sentEvents = 0;
8485
this._resetBackoff();
8586

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

879+
// refresh max events count
880+
this._sentEvents = 0;
881+
878882
// because onpopstate only tells you the "new" (to) value of location.href, and
879883
// not the previous (from) value, we need to track the value of the current URL
880884
// state ourselves
@@ -1255,6 +1259,9 @@ Raven.prototype = {
12551259
return function(/* state, title, url */) {
12561260
var url = arguments.length > 2 ? arguments[2] : undefined;
12571261

1262+
// refresh max events count
1263+
self._sentEvents = 0;
1264+
12581265
// url argument is optional
12591266
if (url) {
12601267
// coerce to string (this is what pushState does)
@@ -1699,13 +1706,23 @@ Raven.prototype = {
16991706
return;
17001707
}
17011708

1709+
// Check if the request should be filtered due to max events per page
1710+
if (
1711+
globalOptions.maxEventsPerPage &&
1712+
this._sentEvents >= globalOptions.maxEventsPerPage
1713+
) {
1714+
return;
1715+
}
1716+
17021717
// Backoff state: Sentry server previously responded w/ an error (e.g. 429 - too many requests),
17031718
// so drop requests until "cool-off" period has elapsed.
17041719
if (this._shouldBackoff()) {
17051720
this._logDebug('warn', 'Raven dropped error due to backoff: ', data);
17061721
return;
17071722
}
17081723

1724+
this._sentEvents++; // failed events count towards maxEvents
1725+
17091726
if (typeof globalOptions.sampleRate === 'number') {
17101727
if (Math.random() < globalOptions.sampleRate) {
17111728
this._sendProcessedPayload(data);

test/raven.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2174,6 +2174,57 @@ describe('Raven (public API)', function() {
21742174
});
21752175
});
21762176
});
2177+
2178+
describe('maxEventsPerPage', function(){
2179+
it('allows many events when maxEventsPerPage is undefined', function () {
2180+
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
2181+
this.sinon.spy(stub)
2182+
2183+
Raven.captureException(new Error('foo'))
2184+
Raven.captureException(new Error('foo'))
2185+
Raven.captureException(new Error('foo'))
2186+
Raven.captureException(new Error('foo'))
2187+
Raven.captureException(new Error('foo'))
2188+
2189+
assert.equal(Raven._sendProcessedPayload.callCount, 5);
2190+
});
2191+
2192+
it('should only allow up to maxEventsPerPage requests', function () {
2193+
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
2194+
this.sinon.spy(stub)
2195+
2196+
Raven._globalOptions.maxEventsPerPage = 3;
2197+
2198+
Raven.captureException(new Error('foo'))
2199+
Raven.captureException(new Error('foo'))
2200+
Raven.captureException(new Error('foo'))
2201+
Raven.captureException(new Error('foo'))
2202+
Raven.captureException(new Error('foo'))
2203+
2204+
assert.equal(Raven._sendProcessedPayload.callCount, 3);
2205+
});
2206+
2207+
it('should reset maxErrors on SPA page change', function () {
2208+
var stub = this.sinon.stub(Raven,'_sendProcessedPayload')
2209+
this.sinon.spy(stub)
2210+
2211+
Raven._globalOptions.maxEventsPerPage = 3;
2212+
2213+
Raven.captureException(new Error('foo'))
2214+
Raven.captureException(new Error('foo'))
2215+
Raven.captureException(new Error('foo'))
2216+
Raven.captureException(new Error('foo'))
2217+
2218+
assert.equal(Raven._sendProcessedPayload.callCount, 3);
2219+
2220+
Raven._captureUrlChange('/foo', '/bar');
2221+
2222+
Raven.captureException(new Error('foo'))
2223+
Raven.captureException(new Error('foo'))
2224+
2225+
assert.equal(Raven._sendProcessedPayload.callCount, 5);
2226+
});
2227+
});
21772228
});
21782229

21792230
describe('.wrap', function() {

typescript/raven.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ declare module Raven {
7171

7272
/** Enables/disables automatic collection of breadcrumbs. */
7373
autoBreadcrumbs?: boolean | AutoBreadcrumbOptions
74+
75+
/** 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. */
76+
maxEventsPerPage?: number;
7477
}
7578

7679
interface RavenInstrumentationOptions {

0 commit comments

Comments
 (0)