Skip to content

Commit 5785911

Browse files
brentvatnebenvinegar
authored andcommitted
Add config to disable try/catch instrumentation (#938)
1 parent 9020164 commit 5785911

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

docs/config.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,21 @@ Those configuration options are documented below:
299299

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

302+
303+
.. describe:: instrument
304+
305+
Enables/disables instrumentation of globals. Possible values are:
306+
307+
* `true` (default)
308+
* `false` - all instrumentation disabled
309+
* A dictionary of individual instrumentation types that can be enabled/disabled:
310+
311+
.. code-block:: javascript
312+
313+
instrument: {
314+
'tryCatch': true, // Instruments timers and event targets
315+
}
316+
302317
Putting it all together
303318
-----------------------
304319

src/raven.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function Raven() {
5555
maxUrlLength: 250,
5656
stackTraceLimit: 50,
5757
autoBreadcrumbs: true,
58+
instrument: true,
5859
sampleRate: 1
5960
};
6061
this._ignoreOnError = 0;
@@ -155,6 +156,18 @@ Raven.prototype = {
155156
}
156157
globalOptions.autoBreadcrumbs = autoBreadcrumbs;
157158

159+
var instrumentDefaults = {
160+
tryCatch: true
161+
};
162+
163+
var instrument = globalOptions.instrument;
164+
if ({}.toString.call(instrument) === '[object Object]') {
165+
instrument = objectMerge(instrumentDefaults, instrument);
166+
} else if (instrument !== false) {
167+
instrument = instrumentDefaults;
168+
}
169+
globalOptions.instrument = instrument;
170+
158171
TraceKit.collectWindowErrors = !!globalOptions.collectWindowErrors;
159172

160173
// return for chaining
@@ -175,7 +188,10 @@ Raven.prototype = {
175188
TraceKit.report.subscribe(function () {
176189
self._handleOnErrorStackInfo.apply(self, arguments);
177190
});
178-
self._instrumentTryCatch();
191+
if (self._globalOptions.instrument && self._globalOptions.instrument.tryCatch) {
192+
self._instrumentTryCatch();
193+
}
194+
179195
if (self._globalOptions.autoBreadcrumbs)
180196
self._instrumentBreadcrumbs();
181197

@@ -853,7 +869,8 @@ Raven.prototype = {
853869
},
854870

855871
/**
856-
* Install any queued plugins
872+
* Wrap timer functions and event targets to catch errors and provide
873+
* better metadata.
857874
*/
858875
_instrumentTryCatch: function() {
859876
var self = this;

test/raven.test.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,33 @@ describe('Raven (public API)', function() {
18301830
});
18311831
});
18321832

1833+
describe('instrument', function () {
1834+
it('should convert `true` to a dictionary of enabled instrument features', function () {
1835+
Raven.config(SENTRY_DSN);
1836+
assert.deepEqual(Raven._globalOptions.instrument, {
1837+
tryCatch: true,
1838+
});
1839+
});
1840+
1841+
it('should leave false as-is', function () {
1842+
Raven.config(SENTRY_DSN, {
1843+
instrument: false
1844+
});
1845+
assert.equal(Raven._globalOptions.instrument, false);
1846+
});
1847+
1848+
it('should merge objects with the default instrument settings', function () {
1849+
Raven.config(SENTRY_DSN, {
1850+
instrument: {
1851+
tryCatch: false
1852+
}
1853+
});
1854+
assert.deepEqual(Raven._globalOptions.instrument, {
1855+
tryCatch: false,
1856+
});
1857+
});
1858+
});
1859+
18331860
describe('autoBreadcrumbs', function () {
18341861
it('should convert `true` to a dictionary of enabled breadcrumb features', function () {
18351862
Raven.config(SENTRY_DSN);
@@ -2936,6 +2963,30 @@ describe('install/uninstall', function () {
29362963
document.addEventListener = temp;
29372964
});
29382965

2966+
it('should instrument try/catch by default', function () {
2967+
this.sinon.stub(Raven, '_instrumentTryCatch');
2968+
Raven.config(SENTRY_DSN).install();
2969+
assert.isTrue(Raven._instrumentTryCatch.calledOnce);
2970+
});
2971+
2972+
it('should not instrument try/catch if instrument is false', function () {
2973+
this.sinon.stub(Raven, '_instrumentTryCatch');
2974+
Raven.config(SENTRY_DSN, {
2975+
instrument: false
2976+
}).install();
2977+
assert.isFalse(Raven._instrumentTryCatch.called);
2978+
});
2979+
2980+
it('should not instrument try/catch if instrument.tryCatch is false', function () {
2981+
this.sinon.stub(Raven, '_instrumentTryCatch');
2982+
Raven.config(SENTRY_DSN, {
2983+
instrument: {
2984+
tryCatch: false
2985+
}
2986+
}).install();
2987+
assert.isFalse(Raven._instrumentTryCatch.called);
2988+
});
2989+
29392990
it('should instrument breadcrumbs by default', function () {
29402991
this.sinon.stub(Raven, '_instrumentBreadcrumbs');
29412992
Raven.config(SENTRY_DSN).install();

typescript/raven.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ interface RavenOptions {
6161

6262
/** Override the default HTTP data transport handler. */
6363
transport?: (options: RavenTransportOptions) => void;
64+
65+
/** Enables/disables instrumentation of globals. */
66+
instrument?: boolean | RavenInstrumentationOptions;
67+
}
68+
69+
interface RavenInstrumentationOptions {
70+
tryCatch?: boolean;
6471
}
6572

6673
interface RavenStatic {

0 commit comments

Comments
 (0)