Skip to content

Commit fce854c

Browse files
authored
Update to Sentry v8 SDKs (robertcepa#234)
* Update to Sentry v8 SDKs * Update to v8.0.0 stable * Update to v8.3.0 * Update Sentry SDKs to v8.9.2
1 parent fb3fe6d commit fce854c

File tree

11 files changed

+247
-262
lines changed

11 files changed

+247
-262
lines changed

packages/toucan-js/package.json

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@
2929
"serverless"
3030
],
3131
"dependencies": {
32-
"@sentry/core": "7.112.2",
33-
"@sentry/utils": "7.112.2",
34-
"@sentry/types": "7.112.2",
35-
"@sentry/integrations": "7.112.2"
32+
"@sentry/core": "8.9.2",
33+
"@sentry/utils": "8.9.2",
34+
"@sentry/types": "8.9.2"
3635
},
3736
"devDependencies": {
3837
"@rollup/plugin-commonjs": "25.0.3",

packages/toucan-js/src/client.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ import { setOnOptional } from './utils';
1313
*/
1414
export class ToucanClient extends ServerRuntimeClient<ToucanClientOptions> {
1515
/**
16-
* Some functions need to access the Hub (Toucan instance) this client is bound to,
16+
* Some functions need to access the scope (Toucan instance) this client is bound to,
1717
* but calling 'getCurrentHub()' is unsafe because it uses globals.
1818
* So we store a reference to the Hub after binding to it and provide it to methods that need it.
1919
*/
2020
#sdk: Toucan | null = null;
2121

22+
#integrationsInitialized: boolean = false;
23+
2224
/**
2325
* Creates a new Toucan SDK instance.
2426
* @param options Configuration options for this SDK.
@@ -43,12 +45,12 @@ export class ToucanClient extends ServerRuntimeClient<ToucanClientOptions> {
4345
* By default, integrations are stored in a global. We want to store them in a local instance because they may have contextual data, such as event request.
4446
*/
4547
public setupIntegrations(): void {
46-
if (this._isEnabled() && !this._integrationsInitialized && this.#sdk) {
48+
if (this._isEnabled() && !this.#integrationsInitialized && this.#sdk) {
4749
this._integrations = setupIntegrations(
4850
this._options.integrations,
4951
this.#sdk,
5052
);
51-
this._integrationsInitialized = true;
53+
this.#integrationsInitialized = true;
5254
}
5355
}
5456

packages/toucan-js/src/eventBuilder.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,10 @@ export function eventFromUnknownInput(
9898

9999
const client = sdk?.getClient();
100100
const normalizeDepth = client && client.getOptions().normalizeDepth;
101-
sdk?.configureScope((scope) => {
102-
scope.setExtra(
103-
'__serialized__',
104-
normalizeToSize(exception, normalizeDepth),
105-
);
106-
});
101+
sdk?.setExtra(
102+
'__serialized__',
103+
normalizeToSize(exception, normalizeDepth),
104+
);
107105

108106
ex = (hint && hint.syntheticException) || new Error(message);
109107
ex.message = message;

packages/toucan-js/src/index.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
export {
2-
LinkedErrors,
3-
RequestData,
4-
Dedupe,
5-
ExtraErrorData,
6-
RewriteFrames,
7-
SessionTiming,
8-
Transaction,
2+
linkedErrorsIntegration,
3+
requestDataIntegration,
4+
dedupeIntegration,
5+
extraErrorDataIntegration,
6+
rewriteFramesIntegration,
7+
sessionTimingIntegration,
98
} from './integrations';
109
export type { LinkedErrorsOptions, RequestDataOptions } from './integrations';
1110
export { Toucan } from './sdk';

packages/toucan-js/src/integration.ts

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { IntegrationIndex } from '@sentry/core/types/integration';
2-
import type { EventProcessor, Integration } from '@sentry/types';
2+
import type { EventHint, Event, Integration } from '@sentry/types';
33
import type { Toucan } from './sdk';
44

55
/**
@@ -16,12 +16,41 @@ export function setupIntegrations(
1616
integrations.forEach((integration) => {
1717
integrationIndex[integration.name] = integration;
1818

19-
integration.setupOnce(
20-
(callback: EventProcessor): void => {
21-
sdk.getScope()?.addEventProcessor(callback);
22-
},
23-
() => sdk,
24-
);
19+
// `setupOnce` is only called the first time
20+
if (typeof integration.setupOnce === 'function') {
21+
integration.setupOnce();
22+
}
23+
24+
const client = sdk.getClient();
25+
26+
if (!client) {
27+
return;
28+
}
29+
30+
// `setup` is run for each client
31+
if (typeof integration.setup === 'function') {
32+
integration.setup(client);
33+
}
34+
35+
if (typeof integration.preprocessEvent === 'function') {
36+
const callback = integration.preprocessEvent.bind(integration);
37+
client.on('preprocessEvent', (event, hint) =>
38+
callback(event, hint, client),
39+
);
40+
}
41+
42+
if (typeof integration.processEvent === 'function') {
43+
const callback = integration.processEvent.bind(integration);
44+
45+
const processor = Object.assign(
46+
(event: Event, hint: EventHint) => callback(event, hint, client),
47+
{
48+
id: integration.name,
49+
},
50+
);
51+
52+
client.addEventProcessor(processor);
53+
}
2554
});
2655

2756
return integrationIndex;
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
export * from './linkedErrors';
22
export * from './requestData';
33
export {
4-
Dedupe,
5-
ExtraErrorData,
6-
RewriteFrames,
7-
SessionTiming,
8-
Transaction,
9-
} from '@sentry/integrations';
4+
dedupeIntegration,
5+
extraErrorDataIntegration,
6+
rewriteFramesIntegration,
7+
sessionTimingIntegration,
8+
} from '@sentry/core';

packages/toucan-js/src/integrations/linkedErrors.ts

Lines changed: 16 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,36 @@
11
import {
22
Event,
33
EventHint,
4-
EventProcessor,
54
Exception,
65
ExtendedError,
7-
Integration,
86
StackParser,
97
} from '@sentry/types';
108
import { isInstanceOf } from '@sentry/utils';
9+
import { defineIntegration } from '@sentry/core';
1110

12-
import { ToucanClient } from '../client';
1311
import { exceptionFromError } from '../eventBuilder';
14-
import { Toucan } from '../sdk';
1512

1613
const DEFAULT_LIMIT = 5;
1714

1815
export type LinkedErrorsOptions = {
1916
limit: number;
2017
};
2118

22-
export class LinkedErrors implements Integration {
23-
public static id = 'LinkedErrors';
24-
25-
public readonly name: string = LinkedErrors.id;
26-
27-
private readonly limit: LinkedErrorsOptions['limit'];
28-
29-
public constructor(options: Partial<LinkedErrorsOptions> = {}) {
30-
this.limit = options.limit || DEFAULT_LIMIT;
31-
}
32-
33-
public setupOnce(
34-
addGlobalEventProcessor: (eventProcessor: EventProcessor) => void,
35-
getCurrentHub: () => Toucan,
36-
): void {
37-
const client = getCurrentHub().getClient<ToucanClient>();
38-
39-
if (!client) {
40-
return;
41-
}
42-
43-
addGlobalEventProcessor((event: Event, hint?: EventHint) => {
44-
const self = getCurrentHub().getIntegration(LinkedErrors);
45-
46-
if (!self) {
47-
return event;
48-
}
49-
50-
return handler(client.getOptions().stackParser, self.limit, event, hint);
51-
});
52-
}
53-
}
19+
export const linkedErrorsIntegration = defineIntegration(
20+
(options: LinkedErrorsOptions = { limit: DEFAULT_LIMIT }) => {
21+
return {
22+
name: 'LinkedErrors',
23+
processEvent: (event, hint, client) => {
24+
return handler(
25+
client.getOptions().stackParser,
26+
options.limit,
27+
event,
28+
hint,
29+
);
30+
},
31+
};
32+
},
33+
);
5434

5535
function handler(
5636
parser: StackParser,

packages/toucan-js/src/integrations/requestData.ts

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { Event, EventProcessor, Integration, User } from '@sentry/types';
2-
1+
import { User } from '@sentry/types';
2+
import { defineIntegration } from '@sentry/core';
33
import type { Request as EventRequest } from '@sentry/types';
4-
import { ToucanClient } from '../client';
5-
import { Toucan } from '../sdk';
64

75
type Allowlist = string[] | RegExp | boolean;
86

@@ -17,65 +15,49 @@ const defaultRequestDataOptions: RequestDataOptions = {
1715
allowedHeaders: ['CF-RAY', 'CF-Worker'],
1816
};
1917

20-
export class RequestData implements Integration {
21-
public static id = 'RequestData';
22-
23-
public readonly name: string = RequestData.id;
24-
25-
#options: RequestDataOptions;
26-
27-
public constructor(options: RequestDataOptions = {}) {
28-
this.#options = { ...defaultRequestDataOptions, ...options };
29-
}
30-
31-
public setupOnce(
32-
addGlobalEventProcessor: (eventProcessor: EventProcessor) => void,
33-
getCurrentHub: () => Toucan,
34-
): void {
35-
const client = getCurrentHub().getClient<ToucanClient>();
18+
export const requestDataIntegration = defineIntegration(
19+
(userOptions: RequestDataOptions = {}) => {
20+
const options = { ...defaultRequestDataOptions, ...userOptions };
3621

37-
if (!client) {
38-
return;
39-
}
22+
return {
23+
name: 'RequestData',
24+
preprocessEvent: (event) => {
25+
const { sdkProcessingMetadata } = event;
4026

41-
addGlobalEventProcessor((event: Event) => {
42-
const { sdkProcessingMetadata } = event;
27+
if (!sdkProcessingMetadata) {
28+
return event;
29+
}
4330

44-
const self = getCurrentHub().getIntegration(RequestData);
31+
if (
32+
'request' in sdkProcessingMetadata &&
33+
sdkProcessingMetadata.request instanceof Request
34+
) {
35+
event.request = toEventRequest(
36+
sdkProcessingMetadata.request,
37+
options,
38+
);
39+
event.user = toEventUser(
40+
event.user ?? {},
41+
sdkProcessingMetadata.request,
42+
options,
43+
);
44+
}
4545

46-
if (!self || !sdkProcessingMetadata) {
47-
return event;
48-
}
49-
50-
if (
51-
'request' in sdkProcessingMetadata &&
52-
sdkProcessingMetadata.request instanceof Request
53-
) {
54-
event.request = toEventRequest(
55-
sdkProcessingMetadata.request,
56-
this.#options,
57-
);
58-
event.user = toEventUser(
59-
event.user ?? {},
60-
sdkProcessingMetadata.request,
61-
this.#options,
62-
);
63-
}
64-
65-
if ('requestData' in sdkProcessingMetadata) {
66-
if (event.request) {
67-
event.request.data = sdkProcessingMetadata.requestData as unknown;
68-
} else {
69-
event.request = {
70-
data: sdkProcessingMetadata.requestData as unknown,
71-
};
46+
if ('requestData' in sdkProcessingMetadata) {
47+
if (event.request) {
48+
event.request.data = sdkProcessingMetadata.requestData as unknown;
49+
} else {
50+
event.request = {
51+
data: sdkProcessingMetadata.requestData as unknown,
52+
};
53+
}
7254
}
73-
}
7455

75-
return event;
76-
});
77-
}
78-
}
56+
return event;
57+
},
58+
};
59+
},
60+
);
7961

8062
/**
8163
* Applies allowlists on existing user object.

0 commit comments

Comments
 (0)