Skip to content

Commit

Permalink
ref(node): Only show instrumentation warning when tracing is enabled (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiborza committed May 21, 2024
1 parent 99351c3 commit 2855d9b
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 26 deletions.
19 changes: 14 additions & 5 deletions packages/node/src/integrations/tracing/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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/.',
);
}
});
}
};
Expand Down
21 changes: 16 additions & 5 deletions packages/node/src/integrations/tracing/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
defineIntegration,
getDefaultIsolationScope,
hasTracingEnabled,
isEnabled,
spanToJSON,
} from '@sentry/core';
Expand All @@ -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 = (() => {
Expand Down Expand Up @@ -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/.',
);
}
});
});
}
}
Expand Down
21 changes: 16 additions & 5 deletions packages/node/src/integrations/tracing/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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/',
);
}
});
});
}
}
Expand Down
19 changes: 14 additions & 5 deletions packages/node/src/integrations/tracing/hapi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (() => {
Expand Down Expand Up @@ -96,12 +98,19 @@ export async function setupHapiErrorHandler(server: Server): Promise<void> {
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/',
);
}
});
}
}
19 changes: 14 additions & 5 deletions packages/node/src/integrations/tracing/koa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import {
defineIntegration,
getDefaultIsolationScope,
getIsolationScope,
hasTracingEnabled,
isEnabled,
spanToJSON,
} from '@sentry/core';
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');
Expand Down Expand Up @@ -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/',
);
}
});
}
};
3 changes: 2 additions & 1 deletion packages/node/src/sdk/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}

Expand Down

0 comments on commit 2855d9b

Please sign in to comment.