Skip to content

feat: TryCatch Integration options #2601

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

Merged
merged 5 commits into from
May 19, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

- [core] feat: Send transactions in envelopes (#2553)
- [core] fix: Send event timestamp (#2575)
- [browser] feat: Allow for configuring TryCatch integration (#2601)
- [browser] fix: Call wrapped `RequestAnimationFrame` with correct context (#2570)
- [node] fix: Prevent reading the same source file multiple times (#2569)
- [integrations] feat: Vue performance monitoring (#2571)
Expand Down
18 changes: 9 additions & 9 deletions packages/browser/src/integrations/breadcrumbs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ export interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
}

/** JSDoc */
interface BreadcrumbIntegrations {
console?: boolean;
dom?: boolean;
fetch?: boolean;
history?: boolean;
sentry?: boolean;
xhr?: boolean;
interface BreadcrumbsOptions {
console: boolean;
dom: boolean;
fetch: boolean;
history: boolean;
sentry: boolean;
xhr: boolean;
}

/**
Expand All @@ -50,12 +50,12 @@ export class Breadcrumbs implements Integration {
public static id: string = 'Breadcrumbs';

/** JSDoc */
private readonly _options: BreadcrumbIntegrations;
private readonly _options: BreadcrumbsOptions;

/**
* @inheritDoc
*/
public constructor(options?: BreadcrumbIntegrations) {
public constructor(options?: Partial<BreadcrumbsOptions>) {
this._options = {
console: true,
dom: true,
Expand Down
114 changes: 74 additions & 40 deletions packages/browser/src/integrations/trycatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,51 @@ import { fill, getFunctionName, getGlobalObject } from '@sentry/utils';

import { wrap } from '../helpers';

const DEFAULT_EVENT_TARGET = [
'EventTarget',
'Window',
'Node',
'ApplicationCache',
'AudioTrackList',
'ChannelMergerNode',
'CryptoOperation',
'EventSource',
'FileReader',
'HTMLUnknownElement',
'IDBDatabase',
'IDBRequest',
'IDBTransaction',
'KeyOperation',
'MediaController',
'MessagePort',
'ModalWindow',
'Notification',
'SVGElementInstance',
'Screen',
'TextTrack',
'TextTrackCue',
'TextTrackList',
'WebSocket',
'WebSocketWorker',
'Worker',
'XMLHttpRequest',
'XMLHttpRequestEventTarget',
'XMLHttpRequestUpload',
];

type XMLHttpRequestProp = 'onload' | 'onerror' | 'onprogress' | 'onreadystatechange';

/** JSDoc */
interface TryCatchOptions {
setTimeout: boolean;
setInterval: boolean;
requestAnimationFrame: boolean;
XMLHttpRequest: boolean;
eventTarget: boolean | string[];
}

/** Wrap timer functions and event targets to catch errors and provide better meta data */
export class TryCatch implements Integration {
/** JSDoc */
private _ignoreOnError: number = 0;

/**
* @inheritDoc
*/
Expand All @@ -20,6 +58,23 @@ export class TryCatch implements Integration {
*/
public static id: string = 'TryCatch';

/** JSDoc */
private readonly _options: TryCatchOptions;

/**
* @inheritDoc
*/
public constructor(options?: Partial<TryCatchOptions>) {
this._options = {
XMLHttpRequest: true,
eventTarget: true,
requestAnimationFrame: true,
setInterval: true,
setTimeout: true,
...options,
};
}

/** JSDoc */
private _wrapTimeFunction(original: () => void): () => number {
return function(this: any, ...args: any[]): number {
Expand Down Expand Up @@ -170,48 +225,27 @@ export class TryCatch implements Integration {
* and provide better metadata.
*/
public setupOnce(): void {
this._ignoreOnError = this._ignoreOnError;

const global = getGlobalObject();

fill(global, 'setTimeout', this._wrapTimeFunction.bind(this));
fill(global, 'setInterval', this._wrapTimeFunction.bind(this));
fill(global, 'requestAnimationFrame', this._wrapRAF.bind(this));
if (this._options.setTimeout) {
fill(global, 'setTimeout', this._wrapTimeFunction.bind(this));
}

if (this._options.setInterval) {
fill(global, 'setInterval', this._wrapTimeFunction.bind(this));
}

if ('XMLHttpRequest' in global) {
if (this._options.requestAnimationFrame) {
fill(global, 'requestAnimationFrame', this._wrapRAF.bind(this));
}

if (this._options.XMLHttpRequest && 'XMLHttpRequest' in global) {
fill(XMLHttpRequest.prototype, 'send', this._wrapXHR.bind(this));
}

[
'EventTarget',
'Window',
'Node',
'ApplicationCache',
'AudioTrackList',
'ChannelMergerNode',
'CryptoOperation',
'EventSource',
'FileReader',
'HTMLUnknownElement',
'IDBDatabase',
'IDBRequest',
'IDBTransaction',
'KeyOperation',
'MediaController',
'MessagePort',
'ModalWindow',
'Notification',
'SVGElementInstance',
'Screen',
'TextTrack',
'TextTrackCue',
'TextTrackList',
'WebSocket',
'WebSocketWorker',
'Worker',
'XMLHttpRequest',
'XMLHttpRequestEventTarget',
'XMLHttpRequestUpload',
].forEach(this._wrapEventTarget.bind(this));
if (this._options.eventTarget) {
const eventTarget = Array.isArray(this._options.eventTarget) ? this._options.eventTarget : DEFAULT_EVENT_TARGET;
eventTarget.forEach(this._wrapEventTarget.bind(this));
}
}
}