Skip to content

Commit 6de1805

Browse files
committed
feat: Add setupIntegrations, Update Scope & Hub
1 parent 6c93dc1 commit 6de1805

File tree

20 files changed

+189
-104
lines changed

20 files changed

+189
-104
lines changed

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,7 +455,7 @@ export class Breadcrumbs implements Integration {
455455
* - Fetch API
456456
* - History API
457457
*/
458-
public install(options: BrowserOptions = {}): void {
458+
public setupOnce(options: BrowserOptions = {}): void {
459459
const filterUrl = options.dsn && new API(options.dsn).getStoreEndpoint();
460460

461461
if (this.options.console) {

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class GlobalHandlers implements Integration {
3636
/**
3737
* @inheritDoc
3838
*/
39-
public install(): void {
39+
public setupOnce(): void {
4040
subscribe((stack: TraceKitStackTrace, _: boolean, error: Error) => {
4141
// TODO: use stack.context to get a valuable information from TraceKit, eg.
4242
// [
@@ -55,7 +55,9 @@ export class GlobalHandlers implements Integration {
5555
if (shouldIgnoreOnError()) {
5656
return;
5757
}
58-
getCurrentHub().captureEvent(this.eventFromGlobalHandler(stack), { originalException: error, data: { stack } });
58+
if (getCurrentHub().getIntegration(this.name)) {
59+
getCurrentHub().captureEvent(this.eventFromGlobalHandler(stack), { originalException: error, data: { stack } });
60+
}
5961
});
6062

6163
if (this.options.onerror) {

packages/browser/src/integrations/linkederrors.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configureScope } from '@sentry/core';
1+
import { configureScope, getCurrentHub } from '@sentry/core';
22
import { Integration, SentryEvent, SentryEventHint, SentryException } from '@sentry/types';
33
import { exceptionFromStacktrace } from '../parsers';
44
import { computeStackTrace } from '../tracekit';
@@ -41,8 +41,10 @@ export class LinkedErrors implements Integration {
4141
/**
4242
* @inheritDoc
4343
*/
44-
public install(): void {
45-
configureScope(scope => scope.addEventProcessor(this.handler.bind(this)));
44+
public setupOnce(): void {
45+
if (getCurrentHub().getIntegration(this.name)) {
46+
configureScope(scope => scope.addEventProcessor(this.handler.bind(this)));
47+
}
4648
}
4749

4850
/**

packages/browser/src/integrations/trycatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ export class TryCatch implements Integration {
162162
* Wrap timer functions and event targets to catch errors
163163
* and provide better metadata.
164164
*/
165-
public install(): void {
165+
public setupOnce(): void {
166166
this.ignoreOnError = this.ignoreOnError;
167167

168168
const global = getGlobalObject();
Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configureScope } from '@sentry/core';
1+
import { configureScope, getCurrentHub } from '@sentry/core';
22
import { Integration, SentryEvent } from '@sentry/types';
33
import { getGlobalObject } from '@sentry/utils/misc';
44

@@ -14,24 +14,26 @@ export class UserAgent implements Integration {
1414
/**
1515
* @inheritDoc
1616
*/
17-
public install(): void {
18-
configureScope(scope => {
19-
scope.addEventProcessor(async (event: SentryEvent) => {
20-
if (!global.navigator || !global.location) {
21-
return event;
22-
}
17+
public setupOnce(): void {
18+
if (getCurrentHub().getIntegration(this.name)) {
19+
configureScope(scope => {
20+
scope.addEventProcessor(async (event: SentryEvent) => {
21+
if (!global.navigator || !global.location) {
22+
return event;
23+
}
2324

24-
// HTTP Interface: https://docs.sentry.io/clientdev/interfaces/http/?platform=javascript
25-
const request = event.request || {};
26-
request.url = request.url || global.location.href;
27-
request.headers = request.headers || {};
28-
request.headers['User-Agent'] = global.navigator.userAgent;
25+
// HTTP Interface: https://docs.sentry.io/clientdev/interfaces/http/?platform=javascript
26+
const request = event.request || {};
27+
request.url = request.url || global.location.href;
28+
request.headers = request.headers || {};
29+
request.headers['User-Agent'] = global.navigator.userAgent;
2930

30-
return {
31-
...event,
32-
request,
33-
};
31+
return {
32+
...event,
33+
request,
34+
};
35+
});
3436
});
35-
});
37+
}
3638
}
3739
}

packages/browser/src/sdk.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,11 @@ export const defaultIntegrations = [
6262
*
6363
* @see BrowserOptions for documentation on configuration options.
6464
*/
65-
export function init(options: BrowserOptions): void {
66-
initAndBind(BrowserClient, options, defaultIntegrations);
65+
export function init(options: BrowserOptions = {}): void {
66+
if (options.defaultIntegrations === undefined) {
67+
options.defaultIntegrations = defaultIntegrations;
68+
}
69+
initAndBind(BrowserClient, options);
6770
}
6871

6972
/**

packages/core/src/baseclient.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Scope } from '@sentry/hub';
22
import {
33
Breadcrumb,
4+
Integration,
45
SentryBreadcrumbHint,
56
SentryEvent,
67
SentryEventHint,
@@ -14,6 +15,7 @@ import { getGlobalObject, uuid4 } from '@sentry/utils/misc';
1415
import { truncate } from '@sentry/utils/string';
1516
import { BackendClass } from './basebackend';
1617
import { Dsn } from './dsn';
18+
import { IntegrationIndex, setupIntegrations } from './integrations';
1719
import { Backend, Client, Options } from './interfaces';
1820
import { logger } from './logger';
1921

@@ -127,6 +129,9 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
127129
*/
128130
private installed?: boolean;
129131

132+
/** Array of used integrations. */
133+
private integrations?: IntegrationIndex;
134+
130135
/**
131136
* Initializes this client instance.
132137
*
@@ -140,6 +145,8 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
140145
if (options.dsn) {
141146
this.dsn = new Dsn(options.dsn);
142147
}
148+
149+
this.integrations = setupIntegrations(options);
143150
}
144151

145152
/**
@@ -415,4 +422,11 @@ export abstract class BaseClient<B extends Backend, O extends Options> implement
415422
.getBuffer()
416423
.drain(timeout);
417424
}
425+
426+
/**
427+
* @inheritDoc
428+
*/
429+
public getIntegration(name: string): Integration | null {
430+
return (this.integrations && this.integrations[name]) || null;
431+
}
418432
}

packages/core/src/integrations/dedupe.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configureScope } from '@sentry/minimal';
1+
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/hub';
22
import { Integration, SentryEvent, SentryException, StackFrame } from '@sentry/types';
33
import { getEventDescription } from '@sentry/utils/misc';
44
import { logger } from '../logger';
@@ -18,9 +18,9 @@ export class Dedupe implements Integration {
1818
/**
1919
* @inheritDoc
2020
*/
21-
public install(): void {
22-
configureScope(scope => {
23-
scope.addEventProcessor(async (currentEvent: SentryEvent) => {
21+
public setupOnce(): void {
22+
addGlobalEventProcessor(async (currentEvent: SentryEvent) => {
23+
if (getCurrentHub().getIntegration(this.name)) {
2424
// Juuust in case something goes wrong
2525
try {
2626
if (this.shouldDropEvent(currentEvent, this.previousEvent)) {
@@ -31,7 +31,8 @@ export class Dedupe implements Integration {
3131
}
3232

3333
return (this.previousEvent = currentEvent);
34-
});
34+
}
35+
return currentEvent;
3536
});
3637
}
3738

packages/core/src/integrations/functiontostring.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export class FunctionToString implements Integration {
1111
/**
1212
* @inheritDoc
1313
*/
14-
public install(): void {
14+
public setupOnce(): void {
1515
originalFunctionToString = Function.prototype.toString;
1616

1717
Function.prototype.toString = function(this: SentryWrappedFunction, ...args: any[]): string {

packages/core/src/integrations/inboundfilters.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getCurrentHub } from '@sentry/hub';
12
import { configureScope } from '@sentry/minimal';
23
import { Integration, SentryEvent } from '@sentry/types';
34
import { isRegExp } from '@sentry/utils/is';
@@ -32,17 +33,19 @@ export class InboundFilters implements Integration {
3233
/**
3334
* @inheritDoc
3435
*/
35-
public install(options: InboundFiltersOptions = {}): void {
36-
this.configureOptions(options);
36+
public setupOnce(options: InboundFiltersOptions = {}): void {
37+
if (getCurrentHub().getIntegration(this.name)) {
38+
this.configureOptions(options);
3739

38-
configureScope(scope => {
39-
scope.addEventProcessor(async (event: SentryEvent) => {
40-
if (this.shouldDropEvent(event)) {
41-
return null;
42-
}
43-
return event;
40+
configureScope(scope => {
41+
scope.addEventProcessor(async (event: SentryEvent) => {
42+
if (this.shouldDropEvent(event)) {
43+
return null;
44+
}
45+
return event;
46+
});
4447
});
45-
});
48+
}
4649
}
4750

4851
/** JSDoc */

0 commit comments

Comments
 (0)