Skip to content

Commit 477aec2

Browse files
authored
ref(utils): Change addInstrumentationHandler to take reg args (#4309)
Change addInstrumentationHandler to take reg args Passing in an object is not necessary as there is always going to be two arguments required. We can just convert this to explicitly passing the options, it saves on bundle size. In addition, we can remove the guards around `type` and `callback` because addInstrumentationHandler is an internally used function, so Typescript and our test suite should handle the function getting called with arguments of the correct type.
1 parent 4de380d commit 477aec2

File tree

11 files changed

+62
-112
lines changed

11 files changed

+62
-112
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -85,34 +85,19 @@ export class Breadcrumbs implements Integration {
8585
*/
8686
public setupOnce(): void {
8787
if (this._options.console) {
88-
addInstrumentationHandler({
89-
callback: _consoleBreadcrumb,
90-
type: 'console',
91-
});
88+
addInstrumentationHandler('console', _consoleBreadcrumb);
9289
}
9390
if (this._options.dom) {
94-
addInstrumentationHandler({
95-
callback: _domBreadcrumb(this._options.dom),
96-
type: 'dom',
97-
});
91+
addInstrumentationHandler('dom', _domBreadcrumb(this._options.dom));
9892
}
9993
if (this._options.xhr) {
100-
addInstrumentationHandler({
101-
callback: _xhrBreadcrumb,
102-
type: 'xhr',
103-
});
94+
addInstrumentationHandler('xhr', _xhrBreadcrumb);
10495
}
10596
if (this._options.fetch) {
106-
addInstrumentationHandler({
107-
callback: _fetchBreadcrumb,
108-
type: 'fetch',
109-
});
97+
addInstrumentationHandler('fetch', _fetchBreadcrumb);
11098
}
11199
if (this._options.history) {
112-
addInstrumentationHandler({
113-
callback: _historyBreadcrumb,
114-
type: 'history',
115-
});
100+
addInstrumentationHandler('history', _historyBreadcrumb);
116101
}
117102
}
118103
}

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,10 @@ export class GlobalHandlers implements Integration {
7474

7575
/** JSDoc */
7676
function _installGlobalOnErrorHandler(): void {
77-
addInstrumentationHandler({
77+
addInstrumentationHandler(
78+
'error',
7879
// eslint-disable-next-line @typescript-eslint/no-explicit-any
79-
callback: (data: { msg: any; url: any; line: any; column: any; error: any }) => {
80+
(data: { msg: any; url: any; line: any; column: any; error: any }) => {
8081
const [hub, attachStacktrace] = getHubAndAttachStacktrace();
8182
if (!hub.getIntegration(GlobalHandlers)) {
8283
return;
@@ -101,15 +102,15 @@ function _installGlobalOnErrorHandler(): void {
101102

102103
addMechanismAndCapture(hub, error, event, 'onerror');
103104
},
104-
type: 'error',
105-
});
105+
);
106106
}
107107

108108
/** JSDoc */
109109
function _installGlobalOnUnhandledRejectionHandler(): void {
110-
addInstrumentationHandler({
110+
addInstrumentationHandler(
111+
'unhandledrejection',
111112
// eslint-disable-next-line @typescript-eslint/no-explicit-any
112-
callback: (e: any) => {
113+
(e: any) => {
113114
const [hub, attachStacktrace] = getHubAndAttachStacktrace();
114115
if (!hub.getIntegration(GlobalHandlers)) {
115116
return;
@@ -151,8 +152,7 @@ function _installGlobalOnUnhandledRejectionHandler(): void {
151152
addMechanismAndCapture(hub, error, event, 'onunhandledrejection');
152153
return;
153154
},
154-
type: 'unhandledrejection',
155-
});
155+
);
156156
}
157157

158158
/**

packages/browser/src/sdk.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -226,15 +226,12 @@ function startSessionTracking(): void {
226226
hub.captureSession();
227227

228228
// We want to create a session for every navigation as well
229-
addInstrumentationHandler({
230-
callback: ({ from, to }) => {
231-
// Don't create an additional session for the initial route or if the location did not change
232-
if (from === undefined || from === to) {
233-
return;
234-
}
235-
hub.startSession({ ignoreDuration: true });
236-
hub.captureSession();
237-
},
238-
type: 'history',
229+
addInstrumentationHandler('history', ({ from, to }) => {
230+
// Don't create an additional session for the initial route or if the location did not change
231+
if (from === undefined || from === to) {
232+
return;
233+
}
234+
hub.startSession({ ignoreDuration: true });
235+
hub.captureSession();
239236
});
240237
}

packages/tracing/src/browser/request.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -117,20 +117,14 @@ export function instrumentOutgoingRequests(_options?: Partial<RequestInstrumenta
117117
const spans: Record<string, Span> = {};
118118

119119
if (traceFetch) {
120-
addInstrumentationHandler({
121-
callback: (handlerData: FetchData) => {
122-
fetchCallback(handlerData, shouldCreateSpan, spans);
123-
},
124-
type: 'fetch',
120+
addInstrumentationHandler('fetch', (handlerData: FetchData) => {
121+
fetchCallback(handlerData, shouldCreateSpan, spans);
125122
});
126123
}
127124

128125
if (traceXHR) {
129-
addInstrumentationHandler({
130-
callback: (handlerData: XHRData) => {
131-
xhrCallback(handlerData, shouldCreateSpan, spans);
132-
},
133-
type: 'xhr',
126+
addInstrumentationHandler('xhr', (handlerData: XHRData) => {
127+
xhrCallback(handlerData, shouldCreateSpan, spans);
134128
});
135129
}
136130
}

packages/tracing/src/browser/router.ts

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,30 @@ export function instrumentRoutingWithDefaults<T extends Transaction>(
2424
}
2525

2626
if (startTransactionOnLocationChange) {
27-
addInstrumentationHandler({
28-
callback: ({ to, from }: { to: string; from?: string }) => {
29-
/**
30-
* This early return is there to account for some cases where a navigation transaction starts right after
31-
* long-running pageload. We make sure that if `from` is undefined and a valid `startingURL` exists, we don't
32-
* create an uneccessary navigation transaction.
33-
*
34-
* This was hard to duplicate, but this behavior stopped as soon as this fix was applied. This issue might also
35-
* only be caused in certain development environments where the usage of a hot module reloader is causing
36-
* errors.
37-
*/
38-
if (from === undefined && startingUrl && startingUrl.indexOf(to) !== -1) {
39-
startingUrl = undefined;
40-
return;
41-
}
27+
addInstrumentationHandler('history', ({ to, from }: { to: string; from?: string }) => {
28+
/**
29+
* This early return is there to account for some cases where a navigation transaction starts right after
30+
* long-running pageload. We make sure that if `from` is undefined and a valid `startingURL` exists, we don't
31+
* create an uneccessary navigation transaction.
32+
*
33+
* This was hard to duplicate, but this behavior stopped as soon as this fix was applied. This issue might also
34+
* only be caused in certain development environments where the usage of a hot module reloader is causing
35+
* errors.
36+
*/
37+
if (from === undefined && startingUrl && startingUrl.indexOf(to) !== -1) {
38+
startingUrl = undefined;
39+
return;
40+
}
4241

43-
if (from !== to) {
44-
startingUrl = undefined;
45-
if (activeTransaction) {
46-
logger.log(`[Tracing] Finishing current transaction with op: ${activeTransaction.op}`);
47-
// If there's an open transaction on the scope, we need to finish it before creating an new one.
48-
activeTransaction.finish();
49-
}
50-
activeTransaction = customStartTransaction({ name: global.location.pathname, op: 'navigation' });
42+
if (from !== to) {
43+
startingUrl = undefined;
44+
if (activeTransaction) {
45+
logger.log(`[Tracing] Finishing current transaction with op: ${activeTransaction.op}`);
46+
// If there's an open transaction on the scope, we need to finish it before creating an new one.
47+
activeTransaction.finish();
5148
}
52-
},
53-
type: 'history',
49+
activeTransaction = customStartTransaction({ name: global.location.pathname, op: 'navigation' });
50+
}
5451
});
5552
}
5653
}

packages/tracing/src/errors.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ import { getActiveTransaction } from './utils';
77
* Configures global error listeners
88
*/
99
export function registerErrorInstrumentation(): void {
10-
addInstrumentationHandler({
11-
callback: errorCallback,
12-
type: 'error',
13-
});
14-
addInstrumentationHandler({
15-
callback: errorCallback,
16-
type: 'unhandledrejection',
17-
});
10+
addInstrumentationHandler('error', errorCallback);
11+
addInstrumentationHandler('unhandledrejection', errorCallback);
1812
}
1913

2014
/**

packages/tracing/test/browser/browsertracing.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jest.mock('@sentry/utils', () => {
2323
const actual = jest.requireActual('@sentry/utils');
2424
return {
2525
...actual,
26-
addInstrumentationHandler: ({ callback, type }: any): void => {
26+
addInstrumentationHandler: (type, callback): void => {
2727
if (type === 'history') {
2828
// rather than actually add the navigation-change handler, grab a reference to it, so we can trigger it manually
2929
mockChangeHistory = callback;

packages/tracing/test/browser/request.test.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,20 @@ describe('instrumentOutgoingRequests', () => {
2626
it('instruments fetch and xhr requests', () => {
2727
instrumentOutgoingRequests();
2828

29-
expect(addInstrumentationHandler).toHaveBeenCalledWith({
30-
callback: expect.any(Function),
31-
type: 'fetch',
32-
});
33-
expect(addInstrumentationHandler).toHaveBeenCalledWith({
34-
callback: expect.any(Function),
35-
type: 'xhr',
36-
});
29+
expect(addInstrumentationHandler).toHaveBeenCalledWith('fetch', expect.any(Function));
30+
expect(addInstrumentationHandler).toHaveBeenCalledWith('xhr', expect.any(Function));
3731
});
3832

3933
it('does not instrument fetch requests if traceFetch is false', () => {
4034
instrumentOutgoingRequests({ traceFetch: false });
4135

42-
expect(addInstrumentationHandler).not.toHaveBeenCalledWith({ callback: expect.any(Function), type: 'fetch' });
36+
expect(addInstrumentationHandler).not.toHaveBeenCalledWith('fetch', expect.any(Function));
4337
});
4438

4539
it('does not instrument xhr requests if traceXHR is false', () => {
4640
instrumentOutgoingRequests({ traceXHR: false });
4741

48-
expect(addInstrumentationHandler).not.toHaveBeenCalledWith({ callback: expect.any(Function), type: 'xhr' });
42+
expect(addInstrumentationHandler).not.toHaveBeenCalledWith('xhr', expect.any(Function));
4943
});
5044
});
5145

packages/tracing/test/browser/router.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ jest.mock('@sentry/utils', () => {
88
const actual = jest.requireActual('@sentry/utils');
99
return {
1010
...actual,
11-
addInstrumentationHandler: ({ callback, type }: any): void => {
11+
addInstrumentationHandler: (type, callback): void => {
1212
addInstrumentationHandlerType = type;
1313
mockChangeHistory = callback;
1414
},

packages/tracing/test/errors.test.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ jest.mock('@sentry/utils', () => {
1111
const actual = jest.requireActual('@sentry/utils');
1212
return {
1313
...actual,
14-
addInstrumentationHandler: ({ callback, type }: any) => {
14+
addInstrumentationHandler: (type, callback) => {
1515
if (type === 'error') {
1616
mockErrorCallback = callback;
1717
}
1818
if (type === 'unhandledrejection') {
1919
mockUnhandledRejectionCallback = callback;
2020
}
2121
if (typeof mockAddInstrumentationHandler === 'function') {
22-
return mockAddInstrumentationHandler({ callback, type });
22+
return mockAddInstrumentationHandler(type, callback);
2323
}
2424
},
2525
};
@@ -44,11 +44,8 @@ describe('registerErrorHandlers()', () => {
4444
it('registers error instrumentation', () => {
4545
registerErrorInstrumentation();
4646
expect(mockAddInstrumentationHandler).toHaveBeenCalledTimes(2);
47-
expect(mockAddInstrumentationHandler).toHaveBeenNthCalledWith(1, { callback: expect.any(Function), type: 'error' });
48-
expect(mockAddInstrumentationHandler).toHaveBeenNthCalledWith(2, {
49-
callback: expect.any(Function),
50-
type: 'unhandledrejection',
51-
});
47+
expect(mockAddInstrumentationHandler).toHaveBeenNthCalledWith(1, 'error', expect.any(Function));
48+
expect(mockAddInstrumentationHandler).toHaveBeenNthCalledWith(2, 'unhandledrejection', expect.any(Function));
5249
});
5350

5451
it('does not set status if transaction is not on scope', () => {

packages/utils/src/instrument.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import { supportsHistory, supportsNativeFetch } from './supports';
1212

1313
const global = getGlobalObject<Window>();
1414

15-
/** Object describing handler that will be triggered for a given `type` of instrumentation */
16-
interface InstrumentHandler {
17-
type: InstrumentHandlerType;
18-
callback: InstrumentHandlerCallback;
19-
}
2015
type InstrumentHandlerType =
2116
| 'console'
2217
| 'dom'
@@ -82,13 +77,10 @@ function instrument(type: InstrumentHandlerType): void {
8277
* Use at your own risk, this might break without changelog notice, only used internally.
8378
* @hidden
8479
*/
85-
export function addInstrumentationHandler(handler: InstrumentHandler): void {
86-
if (!handler || typeof handler.type !== 'string' || typeof handler.callback !== 'function') {
87-
return;
88-
}
89-
handlers[handler.type] = handlers[handler.type] || [];
90-
(handlers[handler.type] as InstrumentHandlerCallback[]).push(handler.callback);
91-
instrument(handler.type);
80+
export function addInstrumentationHandler(type: InstrumentHandlerType, callback: InstrumentHandlerCallback): void {
81+
handlers[type] = handlers[type] || [];
82+
(handlers[type] as InstrumentHandlerCallback[]).push(callback);
83+
instrument(type);
9284
}
9385

9486
/** JSDoc */

0 commit comments

Comments
 (0)