Skip to content

Commit 1d3e208

Browse files
authored
feat: Expose exclude and include options for ESM loader (#12910)
Closes #12878 I added this feature to `import-in-the-middle` which was released in v1.9.0: - nodejs/import-in-the-middle#124 This PR changes the hook from `@opentelemetry/instrumentation/hook.mjs` to `import-in-the-middle/hook.mjs` as it was only pasing though anyway and the otel hook doesn't pass the `initialize` export.
1 parent 05b2754 commit 1d3e208

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

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();
135+
maybeInitializeEsmLoader(options.registerEsmLoaderHooks === true ? undefined : options.registerEsmLoaderHooks);
136136
}
137137

138138
setOpenTelemetryContextAsyncContextStrategy();

packages/node/src/sdk/initOtel.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { GLOBAL_OBJ, consoleSandbox, logger } from '@sentry/utils';
1313

1414
import { getOpenTelemetryInstrumentationToPreload } from '../integrations/tracing';
1515
import { SentryContextManager } from '../otel/contextManager';
16+
import type { EsmLoaderHookOptions } from '../types';
1617
import { isCjs } from '../utils/commonjs';
1718
import type { NodeClient } from './client';
1819

@@ -31,7 +32,7 @@ export function initOpenTelemetry(client: NodeClient): void {
3132
}
3233

3334
/** Initialize the ESM loader. */
34-
export function maybeInitializeEsmLoader(): void {
35+
export function maybeInitializeEsmLoader(esmHookConfig?: EsmLoaderHookOptions): void {
3536
const [nodeMajor = 0, nodeMinor = 0] = process.versions.node.split('.').map(Number);
3637

3738
// Register hook was added in v20.6.0 and v18.19.0
@@ -43,7 +44,7 @@ export function maybeInitializeEsmLoader(): void {
4344
if (!GLOBAL_OBJ._sentryEsmLoaderHookRegistered && importMetaUrl) {
4445
try {
4546
// @ts-expect-error register is available in these versions
46-
moduleModule.register('@opentelemetry/instrumentation/hook.mjs', importMetaUrl);
47+
moduleModule.register('import-in-the-middle/hook.mjs', importMetaUrl, { data: esmHookConfig });
4748
GLOBAL_OBJ._sentryEsmLoaderHookRegistered = true;
4849
} catch (error) {
4950
logger.warn('Failed to register ESM hook', error);

packages/node/src/types.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import type { ClientOptions, Options, SamplingContext, Scope, Span, TracePropaga
44

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

7+
export interface EsmLoaderHookOptions {
8+
include?: string[];
9+
exclude?: string[];
10+
}
11+
712
export interface BaseNodeOptions {
813
/**
914
* List of strings/regex controlling to which outgoing requests
@@ -87,13 +92,22 @@ export interface BaseNodeOptions {
8792

8893
/**
8994
* Whether to register ESM loader hooks to automatically instrument libraries.
90-
* This is necessary to auto instrument libraries that are loaded via ESM imports, but might it can cause issues
95+
* This is necessary to auto instrument libraries that are loaded via ESM imports, but it can cause issues
9196
* with certain libraries. If you run into problems running your app with this enabled,
9297
* please raise an issue in https://github.com/getsentry/sentry-javascript.
9398
*
99+
* You can optionally exclude specific modules or only include specific modules from being instrumented by providing
100+
* an object with `include` or `exclude` properties.
101+
*
102+
* ```js
103+
* registerEsmLoaderHooks: {
104+
* exclude: ['openai'],
105+
* }
106+
* ```
107+
*
94108
* Defaults to `true`.
95109
*/
96-
registerEsmLoaderHooks?: boolean;
110+
registerEsmLoaderHooks?: boolean | EsmLoaderHookOptions;
97111

98112
/** Callback that is executed when a fatal global error occurs. */
99113
onFatalError?(this: void, error: Error): void;

0 commit comments

Comments
 (0)