Skip to content

Commit 7c4e3f5

Browse files
kamilogorekHazAT
authored andcommitted
feat: Update all integrations, Update tests
1 parent 1e9bd9f commit 7c4e3f5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+858
-658
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
- [browser] fix: Make `addBreadcrumb` sync internally, `beforeBreadcrumb` is now only sync
66
- [browser] fix: Remove internal `console` guard in `beforeBreadcrumb`
7+
- [core]: Integrations now live on the `Client`. This means that when binding a new Client to the `Hub` the client
8+
itself can decide which integration should run. It shouldn't break anything, Integrations just internally work
9+
differently.
710

811
## 4.1.1
912

packages/browser/src/integrations/breadcrumbs.ts

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,6 @@ export interface SentryWrappedXMLHttpRequest extends XMLHttpRequest {
2121
};
2222
}
2323

24-
/** JSDoc */
25-
function addSentryBreadcrumb(serializedData: string): void {
26-
// There's always something that can go wrong with deserialization...
27-
try {
28-
const event: { [key: string]: any } = deserialize(serializedData);
29-
30-
Breadcrumbs.addBreadcrumb(
31-
{
32-
category: 'sentry',
33-
event_id: event.event_id,
34-
level: event.level || Severity.fromString('error'),
35-
message: getEventDescription(event),
36-
},
37-
{
38-
event,
39-
},
40-
);
41-
} catch (_oO) {
42-
logger.error('Error while adding sentry type breadcrumb');
43-
}
44-
}
45-
4624
/** JSDoc */
4725
interface BreadcrumbIntegrations {
4826
beacon?: boolean;
@@ -61,6 +39,11 @@ export class Breadcrumbs implements Integration {
6139
*/
6240
public name: string = 'Breadcrumbs';
6341

42+
/**
43+
* @inheritDoc
44+
*/
45+
public static id: string = 'Breadcrumbs';
46+
6447
/** JSDoc */
6548
private readonly options: BreadcrumbIntegrations;
6649

@@ -448,8 +431,13 @@ export class Breadcrumbs implements Integration {
448431
);
449432
}
450433

434+
/**
435+
* Helper that checks if integration is enabled on the client.
436+
* @param breadcrumb Breadcrumb
437+
* @param hint SentryBreadcrumbHint
438+
*/
451439
public static addBreadcrumb(breadcrumb: Breadcrumb, hint?: SentryBreadcrumbHint): void {
452-
if (getCurrentHub().getIntegration('Breadcrumbs')) {
440+
if (getCurrentHub().getIntegration(Breadcrumbs)) {
453441
getCurrentHub().addBreadcrumb(breadcrumb, hint);
454442
}
455443
}
@@ -485,3 +473,25 @@ export class Breadcrumbs implements Integration {
485473
}
486474
}
487475
}
476+
477+
/** JSDoc */
478+
function addSentryBreadcrumb(serializedData: string): void {
479+
// There's always something that can go wrong with deserialization...
480+
try {
481+
const event: { [key: string]: any } = deserialize(serializedData);
482+
483+
Breadcrumbs.addBreadcrumb(
484+
{
485+
category: 'sentry',
486+
event_id: event.event_id,
487+
level: event.level || Severity.fromString('error'),
488+
message: getEventDescription(event),
489+
},
490+
{
491+
event,
492+
},
493+
);
494+
} catch (_oO) {
495+
logger.error('Error while adding sentry type breadcrumb');
496+
}
497+
}

packages/browser/src/integrations/globalhandlers.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ export class GlobalHandlers implements Integration {
2222
*/
2323
public name: string = 'GlobalHandlers';
2424

25+
/**
26+
* @inheritDoc
27+
*/
28+
public static id: string = 'GlobalHandlers';
29+
2530
/** JSDoc */
2631
private readonly options: GlobalHandlersIntegrations;
2732

@@ -55,8 +60,9 @@ export class GlobalHandlers implements Integration {
5560
if (shouldIgnoreOnError()) {
5661
return;
5762
}
58-
if (getCurrentHub().getIntegration(this.name)) {
59-
getCurrentHub().captureEvent(this.eventFromGlobalHandler(stack), { originalException: error, data: { stack } });
63+
const self = getCurrentHub().getIntegration(GlobalHandlers);
64+
if (self) {
65+
getCurrentHub().captureEvent(self.eventFromGlobalHandler(stack), { originalException: error, data: { stack } });
6066
}
6167
});
6268

packages/browser/src/integrations/linkederrors.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configureScope, getCurrentHub } from '@sentry/core';
1+
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
22
import { Integration, SentryEvent, SentryEventHint, SentryException } from '@sentry/types';
33
import { exceptionFromStacktrace } from '../parsers';
44
import { computeStackTrace } from '../tracekit';
@@ -20,6 +20,11 @@ export class LinkedErrors implements Integration {
2020
*/
2121
public readonly name: string = 'LinkedErrors';
2222

23+
/**
24+
* @inheritDoc
25+
*/
26+
public static id: string = 'LinkedErrors';
27+
2328
/**
2429
* @inheritDoc
2530
*/
@@ -42,9 +47,13 @@ export class LinkedErrors implements Integration {
4247
* @inheritDoc
4348
*/
4449
public setupOnce(): void {
45-
if (getCurrentHub().getIntegration(this.name)) {
46-
configureScope(scope => scope.addEventProcessor(this.handler.bind(this)));
47-
}
50+
addGlobalEventProcessor(async (event: SentryEvent, hint?: SentryEventHint) => {
51+
const self = getCurrentHub().getIntegration(LinkedErrors);
52+
if (self) {
53+
return self.handler(event, hint);
54+
}
55+
return event;
56+
});
4857
}
4958

5059
/**

packages/browser/src/integrations/pluggable/ember.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ export class Ember implements Integration {
88
* @inheritDoc
99
*/
1010
public name: string = 'Ember';
11+
/**
12+
* @inheritDoc
13+
*/
14+
public static id: string = 'Ember';
1115

1216
/**
1317
* @inheritDoc
@@ -28,18 +32,20 @@ export class Ember implements Integration {
2832
/**
2933
* @inheritDoc
3034
*/
31-
public install(): void {
35+
public setupOnce(): void {
3236
if (!this.Ember) {
3337
return;
3438
}
3539

3640
const oldOnError = this.Ember.onerror;
3741

3842
this.Ember.onerror = (error: Error): void => {
39-
withScope(scope => {
40-
this.addIntegrationToSdkInfo(scope);
41-
getCurrentHub().captureException(error, { originalException: error });
42-
});
43+
if (getCurrentHub().getIntegration(Ember)) {
44+
withScope(scope => {
45+
this.addIntegrationToSdkInfo(scope);
46+
getCurrentHub().captureException(error, { originalException: error });
47+
});
48+
}
4349

4450
if (typeof oldOnError === 'function') {
4551
oldOnError.call(this.Ember, error);
@@ -49,17 +55,19 @@ export class Ember implements Integration {
4955
this.Ember.RSVP.on(
5056
'error',
5157
(reason: any): void => {
52-
const scope = getCurrentHub().pushScope();
53-
if (reason instanceof Error) {
54-
scope.setExtra('context', 'Unhandled Promise error detected');
55-
this.addIntegrationToSdkInfo(scope);
56-
getCurrentHub().captureException(reason, { originalException: reason });
57-
} else {
58-
scope.setExtra('reason', reason);
59-
this.addIntegrationToSdkInfo(scope);
60-
captureMessage('Unhandled Promise error detected');
58+
if (getCurrentHub().getIntegration(Ember)) {
59+
const scope = getCurrentHub().pushScope();
60+
if (reason instanceof Error) {
61+
scope.setExtra('context', 'Unhandled Promise error detected');
62+
this.addIntegrationToSdkInfo(scope);
63+
getCurrentHub().captureException(reason, { originalException: reason });
64+
} else {
65+
scope.setExtra('reason', reason);
66+
this.addIntegrationToSdkInfo(scope);
67+
captureMessage('Unhandled Promise error detected');
68+
}
69+
getCurrentHub().popScope();
6170
}
62-
getCurrentHub().popScope();
6371
},
6472
);
6573
}

packages/browser/src/integrations/pluggable/vue.ts

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export class Vue implements Integration {
1919
* @inheritDoc
2020
*/
2121
public name: string = 'Vue';
22+
/**
23+
* @inheritDoc
24+
*/
25+
public static id: string = 'Vue';
2226

2327
/**
2428
* @inheritDoc
@@ -51,7 +55,7 @@ export class Vue implements Integration {
5155
/**
5256
* @inheritDoc
5357
*/
54-
public install(): void {
58+
public setupOnce(): void {
5559
if (!this.Vue || !this.Vue.config) {
5660
return;
5761
}
@@ -70,24 +74,26 @@ export class Vue implements Integration {
7074
metadata.lifecycleHook = info;
7175
}
7276

73-
withScope(scope => {
74-
Object.keys(metadata).forEach(key => {
75-
scope.setExtra(key, metadata[key]);
76-
});
77+
if (getCurrentHub().getIntegration(Vue)) {
78+
withScope(scope => {
79+
Object.keys(metadata).forEach(key => {
80+
scope.setExtra(key, metadata[key]);
81+
});
7782

78-
scope.addEventProcessor(async (event: SentryEvent) => {
79-
if (event.sdk) {
80-
const integrations = event.sdk.integrations || [];
81-
event.sdk = {
82-
...event.sdk,
83-
integrations: [...integrations, 'vue'],
84-
};
85-
}
86-
return event;
87-
});
83+
scope.addEventProcessor(async (event: SentryEvent) => {
84+
if (event.sdk) {
85+
const integrations = event.sdk.integrations || [];
86+
event.sdk = {
87+
...event.sdk,
88+
integrations: [...integrations, 'vue'],
89+
};
90+
}
91+
return event;
92+
});
8893

89-
getCurrentHub().captureException(error, { originalException: error });
90-
});
94+
getCurrentHub().captureException(error, { originalException: error });
95+
});
96+
}
9197

9298
if (typeof oldOnError === 'function') {
9399
oldOnError.call(this.Vue, error, vm, info);

packages/browser/src/integrations/reportingobserver.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export class ReportingObserver implements Integration {
5858
* @inheritDoc
5959
*/
6060
public readonly name: string = 'ReportingObserver';
61+
/**
62+
* @inheritDoc
63+
*/
64+
public static id: string = 'ReportingObserver';
6165

6266
/**
6367
* @inheritDoc
@@ -73,7 +77,7 @@ export class ReportingObserver implements Integration {
7377
/**
7478
* @inheritDoc
7579
*/
76-
public install(): void {
80+
public setupOnce(): void {
7781
if (!supportsReportingObserver()) {
7882
return;
7983
}
@@ -92,7 +96,7 @@ export class ReportingObserver implements Integration {
9296
* @inheritDoc
9397
*/
9498
public handler(reports: Report[]): void {
95-
if (!getCurrentHub().getIntegration(this.name)) {
99+
if (!getCurrentHub().getIntegration(ReportingObserver)) {
96100
return;
97101
}
98102
for (const report of reports) {

packages/browser/src/integrations/trycatch.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ export class TryCatch implements Integration {
1313
*/
1414
public name: string = 'TryCatch';
1515

16+
/**
17+
* @inheritDoc
18+
*/
19+
public static id: string = 'TryCatch';
20+
1621
/** JSDoc */
1722
private wrapTimeFunction(original: () => void): () => number {
1823
return function(this: any, ...args: any[]): number {
Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { configureScope, getCurrentHub } from '@sentry/core';
1+
import { addGlobalEventProcessor, getCurrentHub } from '@sentry/core';
22
import { Integration, SentryEvent } from '@sentry/types';
33
import { getGlobalObject } from '@sentry/utils/misc';
44

@@ -11,30 +11,33 @@ export class UserAgent implements Integration {
1111
*/
1212
public name: string = 'UserAgent';
1313

14+
/**
15+
* @inheritDoc
16+
*/
17+
public static id: string = 'UserAgent';
18+
1419
/**
1520
* @inheritDoc
1621
*/
1722
public setupOnce(): void {
18-
configureScope(scope => {
19-
scope.addEventProcessor(async (event: SentryEvent) => {
20-
if (getCurrentHub().getIntegration(this.name)) {
21-
if (!global.navigator || !global.location) {
22-
return event;
23-
}
23+
addGlobalEventProcessor(async (event: SentryEvent) => {
24+
if (getCurrentHub().getIntegration(UserAgent)) {
25+
if (!global.navigator || !global.location) {
26+
return event;
27+
}
2428

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;
29+
// HTTP Interface: https://docs.sentry.io/clientdev/interfaces/http/?platform=javascript
30+
const request = event.request || {};
31+
request.url = request.url || global.location.href;
32+
request.headers = request.headers || {};
33+
request.headers['User-Agent'] = global.navigator.userAgent;
3034

31-
return {
32-
...event,
33-
request,
34-
};
35-
}
36-
return event;
37-
});
35+
return {
36+
...event,
37+
request,
38+
};
39+
}
40+
return event;
3841
});
3942
}
4043
}

0 commit comments

Comments
 (0)