Skip to content

Commit

Permalink
feat(node): Register ESM patching hooks in init for supported Node.…
Browse files Browse the repository at this point in the history
…js versions
  • Loading branch information
lforst committed May 7, 2024
1 parent 19b5eb3 commit 64bf251
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
2 changes: 2 additions & 0 deletions dev-packages/rollup-utils/code/otelEsmLoaderHookTemplate.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import { getFormat, getSource, load, resolve } from '@opentelemetry/instrumentation/hook.mjs';
export { getFormat, getSource, load, resolve };

globalThis._sentrySkipLoaderHookWarning = true;
3 changes: 3 additions & 0 deletions packages/astro/src/server/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { applySdkMetadata } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import { init as initNodeSdk, setTag } from '@sentry/node';
import { GLOBAL_OBJ } from '@sentry/utils';

/**
*
Expand All @@ -12,6 +13,8 @@ export function init(options: NodeOptions): void {
};
applySdkMetadata(opts, 'astro', ['astro', 'node']);

GLOBAL_OBJ._sentrySkipLoaderHookWarning = true;

initNodeSdk(opts);

setTag('runtime', 'node');
Expand Down
1 change: 1 addition & 0 deletions packages/google-cloud-serverless/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { NodeOptions } from '@sentry/node';
import { SDK_VERSION, getDefaultIntegrations as getDefaultNodeIntegrations, init as initNode } from '@sentry/node';
import type { Integration, Options, SdkMetadata } from '@sentry/types';

import { GLOBAL_OBJ } from '@sentry/utils';
import { googleCloudGrpcIntegration } from './integrations/google-cloud-grpc';
import { googleCloudHttpIntegration } from './integrations/google-cloud-http';

Expand Down
2 changes: 2 additions & 0 deletions packages/nextjs/src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ export function init(options: NodeOptions): void {
return;
}

GLOBAL_OBJ._sentrySkipLoaderHookWarning = true;

const customDefaultIntegrations = [
...getDefaultIntegrations(options).filter(
integration =>
Expand Down
23 changes: 16 additions & 7 deletions packages/node/src/sdk/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { openTelemetrySetupCheck, setOpenTelemetryContextAsyncContextStrategy } from '@sentry/opentelemetry';
import type { Client, Integration, Options } from '@sentry/types';
import {
GLOBAL_OBJ,
consoleSandbox,
dropUndefinedKeys,
logger,
Expand All @@ -25,6 +26,7 @@ import { consoleIntegration } from '../integrations/console';
import { nodeContextIntegration } from '../integrations/context';
import { contextLinesIntegration } from '../integrations/contextlines';

import moduleModule from 'module';
import { httpIntegration } from '../integrations/http';
import { localVariablesIntegration } from '../integrations/local-variables';
import { modulesIntegration } from '../integrations/modules';
Expand Down Expand Up @@ -90,13 +92,20 @@ export function init(options: NodeOptions | undefined = {}): void {
}

if (!isCjs()) {
// We want to make sure users see this warning
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
'[Sentry] You are using the Sentry SDK with an ESM build. This version of the SDK is not compatible with ESM. Please either build your application with CommonJS, or use v7 of the SDK.',
);
});
const [nodeMajor, nodeMinor] = process.versions.node.split('.').map(Number);

// Register hook was added in v20.6.0 and v18.19.0
if (nodeMajor > 22 || (nodeMajor === 20 && nodeMinor >= 6) || (nodeMajor === 18 && nodeMinor >= 19)) {
// @ts-expect-error register is available in these versions & import.meta is allowed
moduleModule.register('@opentelemetry/instrumentation/hook.mjs', import.meta.url);
} else if (!GLOBAL_OBJ._sentrySkipLoaderHookWarning) {
consoleSandbox(() => {
// eslint-disable-next-line no-console
console.warn(
'[Sentry] You are using the Sentry SDK in combination with an older version (pre 18.19.0 or pre 20.6.0) of Node.js in ESM mode. For the Sentry SDK to operate properly, please run your Node.js application with a `--experimental-loader=@sentry/node/loader` flag. For example: `node --experimental-loader=@sentry/node/loader your-app.js`',
);
});
}
}

setOpenTelemetryContextAsyncContextStrategy();
Expand Down
4 changes: 3 additions & 1 deletion packages/remix/src/index.server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { applySdkMetadata, isInitialized } from '@sentry/core';
import type { NodeOptions } from '@sentry/node';
import { init as nodeInit, setTag } from '@sentry/node';
import { logger } from '@sentry/utils';
import { GLOBAL_OBJ, logger } from '@sentry/utils';

import { DEBUG_BUILD } from './utils/debug-build';
import { instrumentServer } from './utils/instrumentServer';
Expand Down Expand Up @@ -131,6 +131,8 @@ export function init(options: RemixOptions): void {
return;
}

GLOBAL_OBJ._sentrySkipLoaderHookWarning = true;

instrumentServer();

nodeInit(options as NodeOptions);
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/worldwide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface InternalGlobal {
* Keys are `error.stack` strings, values are the metadata.
*/
_sentryModuleMetadata?: Record<string, any>;
_sentrySkipLoaderHookWarning?: boolean;
}

/** Get's the global object for the current JavaScript runtime */
Expand Down

0 comments on commit 64bf251

Please sign in to comment.