diff --git a/packages/core/src/exports.ts b/packages/core/src/exports.ts index 8571e3ce1be0..d9fec50bdcf4 100644 --- a/packages/core/src/exports.ts +++ b/packages/core/src/exports.ts @@ -242,6 +242,12 @@ export function isInitialized(): boolean { return !!getClient(); } +/** If the SDK is initialized & enabled. */ +export function isEnabled(): boolean { + const client = getClient(); + return !!client && client.getOptions().enabled !== false && !!client.getTransport(); +} + /** * Add an event processor. * This will be added to the current isolation scope, ensuring any event that is processed in the current execution diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 194b35b27153..e1bd05018031 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -25,6 +25,7 @@ export { setTags, setUser, isInitialized, + isEnabled, startSession, endSession, captureSession, diff --git a/packages/node/src/integrations/tracing/connect.ts b/packages/node/src/integrations/tracing/connect.ts index 1711073361e1..9846b6c3dc7e 100644 --- a/packages/node/src/integrations/tracing/connect.ts +++ b/packages/node/src/integrations/tracing/connect.ts @@ -1,6 +1,6 @@ import { isWrapped } from '@opentelemetry/core'; import { ConnectInstrumentation } from '@opentelemetry/instrumentation-connect'; -import { captureException, defineIntegration } from '@sentry/core'; +import { captureException, defineIntegration, isEnabled } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn } from '@sentry/types'; import { consoleSandbox } from '@sentry/utils'; @@ -30,7 +30,7 @@ function connectErrorMiddleware(err: any, req: any, res: any, next: any): void { export const setupConnectErrorHandler = (app: ConnectApp): void => { app.use(connectErrorMiddleware); - if (!isWrapped(app.use)) { + if (!isWrapped(app.use) && isEnabled()) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( diff --git a/packages/node/src/integrations/tracing/express.ts b/packages/node/src/integrations/tracing/express.ts index d9ef43f42d0e..31abf92aca58 100644 --- a/packages/node/src/integrations/tracing/express.ts +++ b/packages/node/src/integrations/tracing/express.ts @@ -1,6 +1,6 @@ import type * as http from 'http'; import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express'; -import { defineIntegration, getDefaultIsolationScope } from '@sentry/core'; +import { defineIntegration, getDefaultIsolationScope, isEnabled } from '@sentry/core'; import { captureException, getClient, getIsolationScope } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn } from '@sentry/types'; @@ -119,7 +119,7 @@ export function expressErrorHandler(options?: { export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void { app.use(expressErrorHandler()); - if (!isWrapped(app.use)) { + if (!isWrapped(app.use) && isEnabled()) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( diff --git a/packages/node/src/integrations/tracing/fastify.ts b/packages/node/src/integrations/tracing/fastify.ts index bf6e683f3fb6..55ccc7b4b72d 100644 --- a/packages/node/src/integrations/tracing/fastify.ts +++ b/packages/node/src/integrations/tracing/fastify.ts @@ -1,6 +1,6 @@ import { isWrapped } from '@opentelemetry/core'; import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify'; -import { captureException, defineIntegration, getIsolationScope } from '@sentry/core'; +import { captureException, defineIntegration, getIsolationScope, isEnabled } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn } from '@sentry/types'; import { consoleSandbox } from '@sentry/utils'; @@ -84,7 +84,7 @@ export function setupFastifyErrorHandler(fastify: Fastify): void { fastify.register(plugin); - if (!isWrapped(fastify.addHook)) { + if (!isWrapped(fastify.addHook) && isEnabled()) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( diff --git a/packages/node/src/integrations/tracing/hapi/index.ts b/packages/node/src/integrations/tracing/hapi/index.ts index 595c23cfda4c..7f55867c8bf5 100644 --- a/packages/node/src/integrations/tracing/hapi/index.ts +++ b/packages/node/src/integrations/tracing/hapi/index.ts @@ -9,6 +9,7 @@ import { getDefaultIsolationScope, getIsolationScope, getRootSpan, + isEnabled, } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; import type { IntegrationFn } from '@sentry/types'; @@ -95,7 +96,7 @@ export async function setupHapiErrorHandler(server: Server): Promise { await server.register(hapiErrorPlugin); // eslint-disable-next-line @typescript-eslint/unbound-method - if (!isWrapped(server.register)) { + if (!isWrapped(server.register) && isEnabled()) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn( diff --git a/packages/node/src/integrations/tracing/koa.ts b/packages/node/src/integrations/tracing/koa.ts index 163f59990965..ba6c136c5486 100644 --- a/packages/node/src/integrations/tracing/koa.ts +++ b/packages/node/src/integrations/tracing/koa.ts @@ -6,6 +6,7 @@ import { defineIntegration, getDefaultIsolationScope, getIsolationScope, + isEnabled, spanToJSON, } from '@sentry/core'; import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry'; @@ -50,7 +51,7 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) => } }); - if (!isWrapped(app.use)) { + if (!isWrapped(app.use) && isEnabled()) { consoleSandbox(() => { // eslint-disable-next-line no-console console.warn(