Skip to content

Commit d5be7ee

Browse files
committed
[v7] Auto-discover node integrations and cleanup default client options
1 parent ba701d1 commit d5be7ee

File tree

4 files changed

+66
-29
lines changed

4 files changed

+66
-29
lines changed

packages/browser/src/client.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { BaseClient, SDK_VERSION } from '@sentry/core';
22
import { CaptureContext, SentryEvent, Options } from '@sentry/types';
33
import { eventFromException, eventFromMessage } from '@sentry/eventbuilder-browser';
4-
import { getGlobalObject } from '@sentry/utils';
54

65
/**
76
* Configuration options for the Sentry Browser SDK.

packages/browser/src/sdk.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,26 @@ export function init(options: BrowserOptions = {}): ClientLike {
3030
}
3131

3232
export function initClient(options: BrowserOptions = {}): ClientLike {
33-
// Injected by sentry-webpack-plugin
34-
options.release = options.release ?? getGlobalObject<Window>().SENTRY_RELEASE?.id;
35-
options.autoSessionTracking = options.autoSessionTracking ?? true;
36-
options.transport = options.transport ?? (supportsFetch() ? FetchTransport : XHRTransport);
37-
38-
options._internal = options._internal || {};
39-
options._internal.defaultIntegrations = options.defaultIntegrations
40-
? options._internal.defaultIntegrations || getDefaultIntegrations()
41-
: [];
42-
options._internal.discoveredIntegrations = options.discoverIntegrations ? discoverIntegrations() : [];
43-
44-
return new BrowserClient(options);
33+
const opts: BrowserOptions = {
34+
// Injected by sentry-webpack-plugin
35+
release: getGlobalObject<Window>().SENTRY_RELEASE?.id,
36+
transport: supportsFetch() ? FetchTransport : XHRTransport,
37+
autoSessionTracking: true,
38+
defaultIntegrations: true,
39+
discoverIntegrations: true,
40+
...options,
41+
_internal: {
42+
defaultIntegrations:
43+
options.defaultIntegrations === false ? [] : options._internal?.defaultIntegrations || getDefaultIntegrations(),
44+
discoveredIntegrations:
45+
options.discoverIntegrations === false
46+
? []
47+
: options._internal?.discoveredIntegrations || discoverIntegrations(),
48+
...options._internal,
49+
},
50+
};
51+
52+
return new BrowserClient(opts);
4553
}
4654

4755
export const getDefaultIntegrations = (): Integration[] => [
@@ -61,6 +69,7 @@ export const getDefaultIntegrations = (): Integration[] => [
6169
];
6270

6371
function discoverIntegrations(): Integration[] {
72+
// TODO: Discover integrations from window.__SENTRY_V7_INTEGRATIONS__?
6473
return [];
6574
}
6675

packages/node/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"@sentry/types": "6.2.3",
3232
"@sentry/utils": "6.2.3",
3333
"cookie": "^0.4.1",
34-
"https-proxy-agent": "^5.0.0"
34+
"https-proxy-agent": "^5.0.0",
35+
"read-pkg-up": "^7.0.1"
3536
},
3637
"devDependencies": {
3738
"@types/cookie": "0.3.2",

packages/node/src/sdk.ts

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { OnUncaughtException, OnUnhandledRejection } from '@sentry/integration-n
88
import { ConsoleBreadcrumbs, HTTPBreadcrumbs } from '@sentry/integration-node-breadcrumbs';
99
import { HTTPTransport } from '@sentry/transport-http';
1010
import { ClientLike, Integration } from '@sentry/types';
11+
import { sync as readPkgUp } from 'read-pkg-up';
1112

1213
import { NodeClient, NodeOptions } from './client';
1314

@@ -21,26 +22,33 @@ export function init(options: NodeOptions = {}): ClientLike {
2122
}
2223

2324
export function initClient(options: NodeOptions = {}): ClientLike {
24-
options.dsn = options.dsn ?? process.env.SENTRY_DSN;
25-
options.release = options.release ?? process.env.SENTRY_RELEASE;
26-
options.environment = options.environment ?? process.env.SENTRY_ENVIRONMENT;
25+
const opts: NodeOptions = {
26+
dsn: process.env.SENTRY_DSN,
27+
release: process.env.SENTRY_RELEASE,
28+
environment: process.env.SENTRY_ENVIRONMENT,
29+
transport: HTTPTransport,
30+
defaultIntegrations: true,
31+
discoverIntegrations: true,
32+
...options,
33+
_internal: {
34+
defaultIntegrations:
35+
options.defaultIntegrations === false ? [] : options._internal?.defaultIntegrations || getDefaultIntegrations(),
36+
discoveredIntegrations:
37+
options.discoverIntegrations === false
38+
? []
39+
: options._internal?.discoveredIntegrations || discoverIntegrations(),
40+
...options._internal,
41+
},
42+
};
2743

28-
if (options.tracesSampleRate === undefined && process.env.SENTRY_TRACES_SAMPLE_RATE) {
44+
if (!('tracesSampleRate' in opts) && process.env.SENTRY_TRACES_SAMPLE_RATE) {
2945
const tracesSampleRate = parseFloat(process.env.SENTRY_TRACES_SAMPLE_RATE);
3046
if (isFinite(tracesSampleRate)) {
31-
options.tracesSampleRate = tracesSampleRate;
47+
opts.tracesSampleRate = tracesSampleRate;
3248
}
3349
}
3450

35-
options.transport = options.transport ?? HTTPTransport;
36-
37-
options._internal = options._internal || {};
38-
options._internal.defaultIntegrations = options.defaultIntegrations
39-
? options._internal.defaultIntegrations || getDefaultIntegrations()
40-
: [];
41-
options._internal.discoveredIntegrations = options.discoverIntegrations ? discoverIntegrations() : [];
42-
43-
return new NodeClient(options);
51+
return new NodeClient(opts);
4452
}
4553

4654
export const getDefaultIntegrations = (): Integration[] => [
@@ -54,5 +62,25 @@ export const getDefaultIntegrations = (): Integration[] => [
5462
];
5563

5664
function discoverIntegrations(): Integration[] {
57-
return [];
65+
const pkg = readPkgUp();
66+
67+
if (!pkg) {
68+
return [];
69+
}
70+
71+
return Object.keys({
72+
...pkg.packageJson.dependencies,
73+
...pkg.packageJson.devDependencies,
74+
})
75+
.filter(name => {
76+
return /^@sentry\/integration-(common|node)-[a-z]/.test(name);
77+
})
78+
.map(name => {
79+
// eslint-disable-next-line @typescript-eslint/no-var-requires
80+
const mod = require(name);
81+
return Object.values(mod) as { new (): Integration }[];
82+
})
83+
.reduce((acc, integrations) => {
84+
return acc.concat(integrations.map(Integration => new Integration()));
85+
}, [] as Integration[]);
5886
}

0 commit comments

Comments
 (0)