Skip to content

Commit 096ef6d

Browse files
committed
feat: Auto-discover @sentry/integration-(common|node)-[a-z] packages in @sentry/node
test: Handle json requires in jest
1 parent afd9ad3 commit 096ef6d

File tree

4 files changed

+603
-649
lines changed

4 files changed

+603
-649
lines changed

packages/gatsby/gatsby-browser.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ exports.onClientEntry = function(_, pluginParams) {
66
return;
77
}
88

9-
pluginParams._metadata = pluginParams._metadata || {};
10-
pluginParams._metadata.sdk = {
9+
pluginParams._internal = pluginParams._internal || {};
10+
pluginParams._internal.sdk = {
1111
name: 'sentry.javascript.gatsby',
1212
packages: [
1313
{

packages/node/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"cookie": "^0.4.1",
2525
"https-proxy-agent": "^5.0.0",
2626
"lru_map": "^0.3.3",
27+
"read-pkg-up": "^7.0.1",
2728
"tslib": "^1.9.3"
2829
},
2930
"devDependencies": {
@@ -73,7 +74,8 @@
7374
},
7475
"moduleFileExtensions": [
7576
"js",
76-
"ts"
77+
"ts",
78+
"json"
7779
],
7880
"testEnvironment": "node",
7981
"testMatch": [

packages/node/src/sdk.ts

+32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { getMainCarrier, setHubOnCarrier } from '@sentry/hub';
33
import { Integration } from '@sentry/types';
44
import { getGlobalObject } from '@sentry/utils';
55
import * as domain from 'domain';
6+
import { cwd } from 'process';
7+
import * as readPkgUp from 'read-pkg-up';
68

79
import { NodeOptions } from './backend';
810
import { NodeClient } from './client';
@@ -88,6 +90,10 @@ export function init(options: NodeOptions = {}): void {
8890
: defaultIntegrations;
8991
}
9092

93+
if (options.discoverIntegrations !== false) {
94+
options._internal.discoveredIntegrations = discoverIntegrations();
95+
}
96+
9197
if (options.dsn === undefined && process.env.SENTRY_DSN) {
9298
options.dsn = process.env.SENTRY_DSN;
9399
}
@@ -118,6 +124,32 @@ export function init(options: NodeOptions = {}): void {
118124
initAndBind(NodeClient, options);
119125
}
120126

127+
/**
128+
* Auto-discover integrations based on package.json entries
129+
*/
130+
function discoverIntegrations(): Integration[] {
131+
const pkg = readPkgUp.sync();
132+
133+
if (!pkg) {
134+
return [];
135+
}
136+
137+
return Object.keys({
138+
...pkg.packageJson.dependencies,
139+
...pkg.packageJson.devDependencies,
140+
})
141+
.filter(name => {
142+
return /^@sentry\/integration-(common|node)-[a-z]/.test(name);
143+
})
144+
.map(name => {
145+
const mod = require(require.resolve(name, { paths: [cwd()] }));
146+
return Object.values(mod) as { new (): Integration }[];
147+
})
148+
.reduce((acc, integrations) => {
149+
return acc.concat(integrations.map(Integration => new Integration()));
150+
}, [] as Integration[]);
151+
}
152+
121153
/**
122154
* This is the getter for lastEventId.
123155
*

0 commit comments

Comments
 (0)