Skip to content

Commit e190793

Browse files
authored
feat(node)!: Remove fine grained registerEsmLoaderHooks option and addOpenTelemetryInstrumentation() (#14606)
1 parent 90fad5a commit e190793

File tree

18 files changed

+11
-179
lines changed

18 files changed

+11
-179
lines changed

dev-packages/e2e-tests/test-applications/node-express-esm-loader/src/instrument.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,4 @@ Sentry.init({
55
dsn: process.env.E2E_TEST_DSN,
66
tunnel: `http://localhost:3031/`, // proxy server
77
tracesSampleRate: 1,
8-
registerEsmLoaderHooks: { onlyIncludeInstrumentedModules: true },
98
});

dev-packages/node-integration-tests/suites/esm/import-in-the-middle/app.mjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Sentry.init({
1313
release: '1.0',
1414
autoSessionTracking: false,
1515
transport: loggingTransport,
16-
registerEsmLoaderHooks: { onlyIncludeInstrumentedModules: true },
1716
});
1817

1918
await import('./sub-module.mjs');

dev-packages/node-integration-tests/suites/esm/import-in-the-middle/test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ afterAll(() => {
88
});
99

1010
conditionalTest({ min: 18 })('import-in-the-middle', () => {
11-
test('onlyIncludeInstrumentedModules', () => {
11+
test('should only instrument modules that we have instrumentation for', () => {
1212
const result = spawnSync('node', [join(__dirname, 'app.mjs')], { encoding: 'utf-8' });
1313
expect(result.stderr).not.toMatch('should be the only hooked modules but we just hooked');
1414
});

packages/astro/src/index.server.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export {
1212
addEventProcessor,
1313
addIntegration,
1414
// eslint-disable-next-line deprecation/deprecation
15-
addOpenTelemetryInstrumentation,
16-
// eslint-disable-next-line deprecation/deprecation
1715
addRequestDataToEvent,
1816
amqplibIntegration,
1917
anrIntegration,

packages/aws-serverless/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ export {
119119
spanToTraceHeader,
120120
spanToBaggageHeader,
121121
trpcMiddleware,
122-
// eslint-disable-next-line deprecation/deprecation
123-
addOpenTelemetryInstrumentation,
124122
zodErrorsIntegration,
125123
profiler,
126124
amqplibIntegration,

packages/bun/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ export {
141141
spanToTraceHeader,
142142
spanToBaggageHeader,
143143
trpcMiddleware,
144-
// eslint-disable-next-line deprecation/deprecation
145-
addOpenTelemetryInstrumentation,
146144
zodErrorsIntegration,
147145
profiler,
148146
amqplibIntegration,

packages/google-cloud-serverless/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@ export {
118118
spanToTraceHeader,
119119
spanToBaggageHeader,
120120
trpcMiddleware,
121-
// eslint-disable-next-line deprecation/deprecation
122-
addOpenTelemetryInstrumentation,
123121
zodErrorsIntegration,
124122
profiler,
125123
amqplibIntegration,

packages/node/src/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,6 @@ export type { NodeOptions } from './types';
6060
export { addRequestDataToEvent, DEFAULT_USER_INCLUDES, extractRequestData } from '@sentry/core';
6161

6262
export {
63-
// eslint-disable-next-line deprecation/deprecation
64-
addOpenTelemetryInstrumentation,
6563
// These are custom variants that need to be used instead of the core one
6664
// As they have slightly different implementations
6765
continueTrace,

packages/node/src/sdk/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ function _init(
132132
}
133133

134134
if (!isCjs() && options.registerEsmLoaderHooks !== false) {
135-
maybeInitializeEsmLoader(options.registerEsmLoaderHooks === true ? undefined : options.registerEsmLoaderHooks);
135+
maybeInitializeEsmLoader();
136136
}
137137

138138
setOpenTelemetryContextAsyncContextStrategy();

packages/node/src/sdk/initOtel.ts

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { createAddHookMessageChannel } from 'import-in-the-middle';
1313
import { DEBUG_BUILD } from '../debug-build';
1414
import { getOpenTelemetryInstrumentationToPreload } from '../integrations/tracing';
1515
import { SentryContextManager } from '../otel/contextManager';
16-
import type { EsmLoaderHookOptions } from '../types';
1716
import { isCjs } from '../utils/commonjs';
1817
import type { NodeClient } from './client';
1918

@@ -34,30 +33,8 @@ export function initOpenTelemetry(client: NodeClient): void {
3433
client.traceProvider = provider;
3534
}
3635

37-
type ImportInTheMiddleInitData = Pick<EsmLoaderHookOptions, 'include' | 'exclude'> & {
38-
addHookMessagePort?: unknown;
39-
};
40-
41-
interface RegisterOptions {
42-
data?: ImportInTheMiddleInitData;
43-
transferList?: unknown[];
44-
}
45-
46-
function getRegisterOptions(esmHookConfig?: EsmLoaderHookOptions): RegisterOptions {
47-
// TODO(v9): Make onlyIncludeInstrumentedModules: true the default behavior.
48-
if (esmHookConfig?.onlyIncludeInstrumentedModules) {
49-
const { addHookMessagePort } = createAddHookMessageChannel();
50-
// If the user supplied include, we need to use that as a starting point or use an empty array to ensure no modules
51-
// are wrapped if they are not hooked
52-
// eslint-disable-next-line deprecation/deprecation
53-
return { data: { addHookMessagePort, include: esmHookConfig.include || [] }, transferList: [addHookMessagePort] };
54-
}
55-
56-
return { data: esmHookConfig };
57-
}
58-
5936
/** Initialize the ESM loader. */
60-
export function maybeInitializeEsmLoader(esmHookConfig?: EsmLoaderHookOptions): void {
37+
export function maybeInitializeEsmLoader(): void {
6138
const [nodeMajor = 0, nodeMinor = 0] = process.versions.node.split('.').map(Number);
6239

6340
// Register hook was added in v20.6.0 and v18.19.0
@@ -68,8 +45,12 @@ export function maybeInitializeEsmLoader(esmHookConfig?: EsmLoaderHookOptions):
6845

6946
if (!GLOBAL_OBJ._sentryEsmLoaderHookRegistered && importMetaUrl) {
7047
try {
48+
const { addHookMessagePort } = createAddHookMessageChannel();
7149
// @ts-expect-error register is available in these versions
72-
moduleModule.register('import-in-the-middle/hook.mjs', importMetaUrl, getRegisterOptions(esmHookConfig));
50+
moduleModule.register('import-in-the-middle/hook.mjs', importMetaUrl, {
51+
data: { addHookMessagePort, include: [] },
52+
transferList: [addHookMessagePort],
53+
});
7354
GLOBAL_OBJ._sentryEsmLoaderHookRegistered = true;
7455
} catch (error) {
7556
logger.warn('Failed to register ESM hook', error);
@@ -88,7 +69,6 @@ export function maybeInitializeEsmLoader(esmHookConfig?: EsmLoaderHookOptions):
8869
interface NodePreloadOptions {
8970
debug?: boolean;
9071
integrations?: string[];
91-
registerEsmLoaderHooks?: EsmLoaderHookOptions;
9272
}
9373

9474
/**
@@ -105,7 +85,7 @@ export function preloadOpenTelemetry(options: NodePreloadOptions = {}): void {
10585
}
10686

10787
if (!isCjs()) {
108-
maybeInitializeEsmLoader(options.registerEsmLoaderHooks);
88+
maybeInitializeEsmLoader();
10989
}
11090

11191
// These are all integrations that we need to pre-load to ensure they are set up before any other code runs

packages/node/src/types.ts

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,6 @@ import type { ClientOptions, Options, SamplingContext, Scope, Span, TracePropaga
55

66
import type { NodeTransportOptions } from './transports';
77

8-
/**
9-
* Note: In the next major version of the Sentry SDK this interface will be removed and the SDK will by default only wrap
10-
* ESM modules that are required to be wrapped by OpenTelemetry Instrumentation.
11-
*/
12-
export interface EsmLoaderHookOptions {
13-
/**
14-
* Provide a list of modules to wrap with `import-in-the-middle`.
15-
*
16-
* @deprecated It is recommended to use `onlyIncludeInstrumentedModules: true` instead of manually defining modules to include and exclude.
17-
*/
18-
include?: Array<string | RegExp>;
19-
20-
/**
21-
* Provide a list of modules to prevent them from being wrapped with `import-in-the-middle`.
22-
*
23-
* @deprecated It is recommended to use `onlyIncludeInstrumentedModules: true` instead of manually defining modules to include and exclude.
24-
*/
25-
exclude?: Array<string | RegExp>;
26-
27-
/**
28-
* When set to `true`, `import-in-the-middle` will only wrap ESM modules that are specifically instrumented by
29-
* OpenTelemetry plugins. This is useful to avoid issues where `import-in-the-middle` is not compatible with some of
30-
* your dependencies.
31-
*
32-
* **Note**: This feature will only work if you `Sentry.init()` the SDK before the instrumented modules are loaded.
33-
* This can be achieved via the Node `--import` CLI flag or by loading your app via async `import()` after calling
34-
* `Sentry.init()`.
35-
*
36-
* Defaults to `false`.
37-
*
38-
* Note: In the next major version of the Sentry SDK this option will be removed and the SDK will by default only wrap
39-
* ESM modules that are required to be wrapped by OpenTelemetry Instrumentation.
40-
*/
41-
// TODO(v9): Make `onlyIncludeInstrumentedModules: true` the default behavior.
42-
onlyIncludeInstrumentedModules?: boolean;
43-
}
44-
458
export interface BaseNodeOptions {
469
/**
4710
* List of strings/regex controlling to which outgoing requests
@@ -138,22 +101,9 @@ export interface BaseNodeOptions {
138101
* with certain libraries. If you run into problems running your app with this enabled,
139102
* please raise an issue in https://github.com/getsentry/sentry-javascript.
140103
*
141-
* You can optionally exclude specific modules or only include specific modules from being instrumented by providing
142-
* an object with `include` or `exclude` properties.
143-
*
144-
* ```js
145-
* registerEsmLoaderHooks: {
146-
* exclude: ['openai'],
147-
* }
148-
* ```
149-
*
150104
* Defaults to `true`.
151-
*
152-
* Note: In the next major version of the SDK, the possibility to provide fine-grained control will be removed from this option.
153-
* This means that it will only be possible to pass `true` or `false`. The default value will continue to be `true`.
154105
*/
155-
// TODO(v9): Only accept true | false | undefined.
156-
registerEsmLoaderHooks?: boolean | EsmLoaderHookOptions;
106+
registerEsmLoaderHooks?: boolean;
157107

158108
/**
159109
* Configures in which interval client reports will be flushed. Defaults to `60_000` (milliseconds).

packages/nuxt/src/server/sdk.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import type { SentryNuxtServerOptions } from '../common/types';
1818
export function init(options: SentryNuxtServerOptions): Client | undefined {
1919
const sentryOptions = {
2020
...options,
21-
registerEsmLoaderHooks: mergeRegisterEsmLoaderHooks(options),
2221
defaultIntegrations: getNuxtDefaultIntegrations(options),
2322
};
2423

@@ -92,28 +91,6 @@ function getNuxtDefaultIntegrations(options: NodeOptions): Integration[] {
9291
];
9392
}
9493

95-
/**
96-
* Adds /vue/ to the registerEsmLoaderHooks options and merges it with the old values in the array if one is defined.
97-
* If the registerEsmLoaderHooks option is already a boolean, nothing is changed.
98-
*
99-
* Only exported for Testing purposes.
100-
*/
101-
export function mergeRegisterEsmLoaderHooks(
102-
options: SentryNuxtServerOptions,
103-
): SentryNuxtServerOptions['registerEsmLoaderHooks'] {
104-
if (typeof options.registerEsmLoaderHooks === 'object' && options.registerEsmLoaderHooks !== null) {
105-
return {
106-
// eslint-disable-next-line deprecation/deprecation
107-
exclude: Array.isArray(options.registerEsmLoaderHooks.exclude)
108-
? // eslint-disable-next-line deprecation/deprecation
109-
[...options.registerEsmLoaderHooks.exclude, /vue/]
110-
: // eslint-disable-next-line deprecation/deprecation
111-
options.registerEsmLoaderHooks.exclude ?? [/vue/],
112-
};
113-
}
114-
return options.registerEsmLoaderHooks ?? { exclude: [/vue/] };
115-
}
116-
11794
/**
11895
* Flushes pending Sentry events with a 2-second timeout and in a way that cannot create unhandled promise rejections.
11996
*/

packages/nuxt/test/server/sdk.test.ts

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ import { Scope } from '@sentry/node';
55
import { getGlobalScope } from '@sentry/node';
66
import { SDK_VERSION } from '@sentry/node';
77
import { beforeEach, describe, expect, it, vi } from 'vitest';
8-
import type { SentryNuxtServerOptions } from '../../src/common/types';
98
import { init } from '../../src/server';
10-
import { clientSourceMapErrorFilter, mergeRegisterEsmLoaderHooks } from '../../src/server/sdk';
9+
import { clientSourceMapErrorFilter } from '../../src/server/sdk';
1110

1211
const nodeInit = vi.spyOn(SentryNode, 'init');
1312

@@ -163,42 +162,4 @@ describe('Nuxt Server SDK', () => {
163162
});
164163
});
165164
});
166-
167-
describe('mergeRegisterEsmLoaderHooks', () => {
168-
it('merges exclude array when registerEsmLoaderHooks is an object with an exclude array', () => {
169-
const options: SentryNuxtServerOptions = {
170-
registerEsmLoaderHooks: { exclude: [/test/] },
171-
};
172-
const result = mergeRegisterEsmLoaderHooks(options);
173-
expect(result).toEqual({ exclude: [/test/, /vue/] });
174-
});
175-
176-
it('sets exclude array when registerEsmLoaderHooks is an object without an exclude array', () => {
177-
const options: SentryNuxtServerOptions = {
178-
registerEsmLoaderHooks: {},
179-
};
180-
const result = mergeRegisterEsmLoaderHooks(options);
181-
expect(result).toEqual({ exclude: [/vue/] });
182-
});
183-
184-
it('returns boolean when registerEsmLoaderHooks is a boolean', () => {
185-
const options1: SentryNuxtServerOptions = {
186-
registerEsmLoaderHooks: true,
187-
};
188-
const result1 = mergeRegisterEsmLoaderHooks(options1);
189-
expect(result1).toBe(true);
190-
191-
const options2: SentryNuxtServerOptions = {
192-
registerEsmLoaderHooks: false,
193-
};
194-
const result2 = mergeRegisterEsmLoaderHooks(options2);
195-
expect(result2).toBe(false);
196-
});
197-
198-
it('sets exclude array when registerEsmLoaderHooks is undefined', () => {
199-
const options: SentryNuxtServerOptions = {};
200-
const result = mergeRegisterEsmLoaderHooks(options);
201-
expect(result).toEqual({ exclude: [/vue/] });
202-
});
203-
});
204165
});

packages/opentelemetry/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,5 @@ export {
5858

5959
export { openTelemetrySetupCheck } from './utils/setupCheck';
6060

61-
// eslint-disable-next-line deprecation/deprecation
62-
export { addOpenTelemetryInstrumentation } from './instrumentation';
63-
6461
// Legacy
6562
export { getClient } from '@sentry/core';

packages/opentelemetry/src/instrumentation.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/remix/src/index.server.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ export {
1616
addEventProcessor,
1717
addIntegration,
1818
// eslint-disable-next-line deprecation/deprecation
19-
addOpenTelemetryInstrumentation,
20-
// eslint-disable-next-line deprecation/deprecation
2119
addRequestDataToEvent,
2220
amqplibIntegration,
2321
anrIntegration,

packages/solidstart/src/server/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export {
88
addEventProcessor,
99
addIntegration,
1010
// eslint-disable-next-line deprecation/deprecation
11-
addOpenTelemetryInstrumentation,
12-
// eslint-disable-next-line deprecation/deprecation
1311
addRequestDataToEvent,
1412
amqplibIntegration,
1513
anrIntegration,

packages/sveltekit/src/server/index.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ export {
88
addEventProcessor,
99
addIntegration,
1010
// eslint-disable-next-line deprecation/deprecation
11-
addOpenTelemetryInstrumentation,
12-
// eslint-disable-next-line deprecation/deprecation
1311
addRequestDataToEvent,
1412
amqplibIntegration,
1513
anrIntegration,

0 commit comments

Comments
 (0)