From 2855d9b01fce0e06bb5c21b5e3fcd9c161d5cc04 Mon Sep 17 00:00:00 2001 From: Andrei Borza Date: Tue, 21 May 2024 15:16:26 +0200 Subject: [PATCH] ref(node): Only show instrumentation warning when tracing is enabled (#12119) --- .../node/src/integrations/tracing/connect.ts | 19 ++++++++++++----- .../node/src/integrations/tracing/express.ts | 21 ++++++++++++++----- .../node/src/integrations/tracing/fastify.ts | 21 ++++++++++++++----- .../src/integrations/tracing/hapi/index.ts | 19 ++++++++++++----- packages/node/src/integrations/tracing/koa.ts | 19 ++++++++++++----- packages/node/src/sdk/init.ts | 3 ++- 6 files changed, 76 insertions(+), 26 deletions(-) diff --git a/packages/node/src/integrations/tracing/connect.ts b/packages/node/src/integrations/tracing/connect.ts index 7dfecef3a482..e28338678168 100644 --- a/packages/node/src/integrations/tracing/connect.ts +++ b/packages/node/src/integrations/tracing/connect.ts @@ -6,12 +6,14 @@ import { captureException, defineIntegration, getClient, + hasTracingEnabled, isEnabled, spanToJSON, } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn, Span } from '@sentry/types'; import { consoleSandbox } from '@sentry/utils'; +import { isCjs } from '../../sdk/init'; type ConnectApp = { // eslint-disable-next-line @typescript-eslint/no-explicit-any @@ -48,12 +50,19 @@ export const setupConnectErrorHandler = (app: ConnectApp): void => { }); } - if (!isWrapped(app.use) && isEnabled()) { + if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) { consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - '[Sentry] Connect is not instrumented. This is likely because you required/imported connect before calling `Sentry.init()`.', - ); + if (isCjs()) { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Connect is not instrumented. This is likely because you required/imported connect before calling `Sentry.init()`.', + ); + } else { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Connect is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/connect/install/esm/.', + ); + } }); } }; diff --git a/packages/node/src/integrations/tracing/express.ts b/packages/node/src/integrations/tracing/express.ts index 58164f97d5ea..bc6b397d6e5e 100644 --- a/packages/node/src/integrations/tracing/express.ts +++ b/packages/node/src/integrations/tracing/express.ts @@ -4,6 +4,7 @@ import { SEMANTIC_ATTRIBUTE_SENTRY_OP, defineIntegration, getDefaultIsolationScope, + hasTracingEnabled, isEnabled, spanToJSON, } from '@sentry/core'; @@ -15,6 +16,7 @@ import { isWrapped } from '@opentelemetry/core'; import { consoleSandbox, logger } from '@sentry/utils'; import { DEBUG_BUILD } from '../../debug-build'; import type { NodeClient } from '../../sdk/client'; +import { isCjs } from '../../sdk/init'; import { addOriginToSpan } from '../../utils/addOriginToSpan'; const _expressIntegration = (() => { @@ -139,12 +141,21 @@ export function expressErrorHandler(options?: { export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void { app.use(expressErrorHandler()); - if (!isWrapped(app.use) && isEnabled()) { + if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) { consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - '[Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.', - ); + consoleSandbox(() => { + if (isCjs()) { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.', + ); + } else { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Express is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/express/install/esm/.', + ); + } + }); }); } } diff --git a/packages/node/src/integrations/tracing/fastify.ts b/packages/node/src/integrations/tracing/fastify.ts index 33f2af3084bd..e659160d5b79 100644 --- a/packages/node/src/integrations/tracing/fastify.ts +++ b/packages/node/src/integrations/tracing/fastify.ts @@ -7,12 +7,14 @@ import { defineIntegration, getClient, getIsolationScope, + hasTracingEnabled, isEnabled, spanToJSON, } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn, Span } from '@sentry/types'; import { consoleSandbox } from '@sentry/utils'; +import { isCjs } from '../../sdk/init'; // We inline the types we care about here interface Fastify { @@ -101,12 +103,21 @@ export function setupFastifyErrorHandler(fastify: Fastify): void { }); } - if (!isWrapped(fastify.addHook) && isEnabled()) { + if (!isWrapped(fastify.addHook) && isEnabled() && hasTracingEnabled()) { consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - '[Sentry] Fastify is not instrumented. This is likely because you required/imported fastify before calling `Sentry.init()`.', - ); + consoleSandbox(() => { + if (isCjs()) { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Fastify is not instrumented. This is likely because you required/imported fastify before calling `Sentry.init()`.', + ); + } else { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Fastify is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/fastify/install/esm/', + ); + } + }); }); } } diff --git a/packages/node/src/integrations/tracing/hapi/index.ts b/packages/node/src/integrations/tracing/hapi/index.ts index 7f55867c8bf5..7ad64c75c9d9 100644 --- a/packages/node/src/integrations/tracing/hapi/index.ts +++ b/packages/node/src/integrations/tracing/hapi/index.ts @@ -9,12 +9,14 @@ import { getDefaultIsolationScope, getIsolationScope, getRootSpan, + hasTracingEnabled, isEnabled, } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn } from '@sentry/types'; import { consoleSandbox, logger } from '@sentry/utils'; import { DEBUG_BUILD } from '../../../debug-build'; +import { isCjs } from '../../../sdk/init'; import type { Boom, RequestEvent, ResponseObject, Server } from './types'; const _hapiIntegration = (() => { @@ -96,12 +98,19 @@ export async function setupHapiErrorHandler(server: Server): Promise { await server.register(hapiErrorPlugin); // eslint-disable-next-line @typescript-eslint/unbound-method - if (!isWrapped(server.register) && isEnabled()) { + if (!isWrapped(server.register) && isEnabled() && hasTracingEnabled()) { consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - '[Sentry] Hapi is not instrumented. This is likely because you required/imported hapi before calling `Sentry.init()`.', - ); + if (isCjs()) { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Hapi is not instrumented. This is likely because you required/imported hapi before calling `Sentry.init()`.', + ); + } else { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Hapi is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/hapi/install/esm/', + ); + } }); } } diff --git a/packages/node/src/integrations/tracing/koa.ts b/packages/node/src/integrations/tracing/koa.ts index d6be349e60de..531cbf18550f 100644 --- a/packages/node/src/integrations/tracing/koa.ts +++ b/packages/node/src/integrations/tracing/koa.ts @@ -8,6 +8,7 @@ import { defineIntegration, getDefaultIsolationScope, getIsolationScope, + hasTracingEnabled, isEnabled, spanToJSON, } from '@sentry/core'; @@ -15,6 +16,7 @@ import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn, Span } from '@sentry/types'; import { consoleSandbox, logger } from '@sentry/utils'; import { DEBUG_BUILD } from '../../debug-build'; +import { isCjs } from '../../sdk/init'; function addKoaSpanAttributes(span: Span): void { span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.http.otel.koa'); @@ -76,12 +78,19 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) => } }); - if (!isWrapped(app.use) && isEnabled()) { + if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) { consoleSandbox(() => { - // eslint-disable-next-line no-console - console.warn( - '[Sentry] Koa is not instrumented. This is likely because you required/imported koa before calling `Sentry.init()`.', - ); + if (isCjs()) { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Koa is not instrumented. This is likely because you required/imported koa before calling `Sentry.init()`.', + ); + } else { + // eslint-disable-next-line no-console + console.warn( + '[Sentry] Koa is not instrumented. Please make sure to initialize Sentry in a separate file that you `--import` when running node, see: https://docs.sentry.io/platforms/javascript/guides/koa/install/esm/', + ); + } }); } }; diff --git a/packages/node/src/sdk/init.ts b/packages/node/src/sdk/init.ts index 469c9e9e12f7..be78aa54efa3 100644 --- a/packages/node/src/sdk/init.ts +++ b/packages/node/src/sdk/init.ts @@ -41,7 +41,8 @@ import { defaultStackParser, getSentryRelease } from './api'; import { NodeClient } from './client'; import { initOpenTelemetry } from './initOtel'; -function isCjs(): boolean { +/** Detect CommonJS. */ +export function isCjs(): boolean { return typeof require !== 'undefined'; }