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
…12141)

Also gives a better warning in ESM mode with a helpful link to the docs.
  • Loading branch information
andreiborza authored May 22, 2024
1 parent f7bd662 commit a30fbbc
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 64 deletions.
13 changes: 2 additions & 11 deletions packages/node/src/integrations/tracing/connect.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import { isWrapped } from '@opentelemetry/core';
import { ConnectInstrumentation } from '@opentelemetry/instrumentation-connect';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
captureException,
defineIntegration,
getClient,
isEnabled,
spanToJSON,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn, Span } from '@sentry/types';
import { consoleSandbox } from '@sentry/utils';
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';

type ConnectApp = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -48,14 +46,7 @@ export const setupConnectErrorHandler = (app: ConnectApp): void => {
});
}

if (!isWrapped(app.use) && isEnabled()) {
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()`.',
);
});
}
ensureIsWrapped(app.use, 'connect');
};

function addConnectSpanAttributes(span: Span): void {
Expand Down
23 changes: 4 additions & 19 deletions packages/node/src/integrations/tracing/express.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,14 @@
import type * as http from 'node:http';
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
defineIntegration,
getDefaultIsolationScope,
isEnabled,
spanToJSON,
} from '@sentry/core';
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, defineIntegration, getDefaultIsolationScope, spanToJSON } from '@sentry/core';
import { captureException, getClient, getIsolationScope } from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn } from '@sentry/types';

import { isWrapped } from '@opentelemetry/core';
import { consoleSandbox, logger } from '@sentry/utils';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../../debug-build';
import type { NodeClient } from '../../sdk/client';
import { addOriginToSpan } from '../../utils/addOriginToSpan';
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';

const _expressIntegration = (() => {
return {
Expand Down Expand Up @@ -138,15 +131,7 @@ export function expressErrorHandler(options?: {
*/
export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void {
app.use(expressErrorHandler());

if (!isWrapped(app.use) && isEnabled()) {
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()`.',
);
});
}
ensureIsWrapped(app.use, 'express');
}

function getStatusCodeFromResponse(error: MiddlewareError): number {
Expand Down
13 changes: 2 additions & 11 deletions packages/node/src/integrations/tracing/fastify.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isWrapped } from '@opentelemetry/core';
import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
Expand All @@ -7,12 +6,11 @@ import {
defineIntegration,
getClient,
getIsolationScope,
isEnabled,
spanToJSON,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn, Span } from '@sentry/types';
import { consoleSandbox } from '@sentry/utils';
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';

// We inline the types we care about here
interface Fastify {
Expand Down Expand Up @@ -101,14 +99,7 @@ export function setupFastifyErrorHandler(fastify: Fastify): void {
});
}

if (!isWrapped(fastify.addHook) && isEnabled()) {
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()`.',
);
});
}
ensureIsWrapped(fastify.addHook, 'fastify');
}

function addFastifySpanAttributes(span: Span): void {
Expand Down
14 changes: 3 additions & 11 deletions packages/node/src/integrations/tracing/hapi/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isWrapped } from '@opentelemetry/core';
import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';
import {
SDK_VERSION,
Expand All @@ -12,13 +11,13 @@ import {
getDefaultIsolationScope,
getIsolationScope,
getRootSpan,
isEnabled,
spanToJSON,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn, Span } from '@sentry/types';
import { consoleSandbox, logger } from '@sentry/utils';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../../../debug-build';
import { ensureIsWrapped } from '../../../utils/ensureIsWrapped';
import type { Boom, RequestEvent, ResponseObject, Server } from './types';

const _hapiIntegration = (() => {
Expand Down Expand Up @@ -110,14 +109,7 @@ export async function setupHapiErrorHandler(server: Server): Promise<void> {
}

// eslint-disable-next-line @typescript-eslint/unbound-method
if (!isWrapped(server.register) && isEnabled()) {
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()`.',
);
});
}
ensureIsWrapped(server.register, 'hapi');
}

function addHapiSpanAttributes(span: Span): void {
Expand Down
14 changes: 3 additions & 11 deletions packages/node/src/integrations/tracing/koa.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { isWrapped } from '@opentelemetry/core';
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
import {
Expand All @@ -8,13 +7,13 @@ import {
defineIntegration,
getDefaultIsolationScope,
getIsolationScope,
isEnabled,
spanToJSON,
} from '@sentry/core';
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
import type { IntegrationFn, Span } from '@sentry/types';
import { consoleSandbox, logger } from '@sentry/utils';
import { logger } from '@sentry/utils';
import { DEBUG_BUILD } from '../../debug-build';
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';

function addKoaSpanAttributes(span: Span): void {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.http.otel.koa');
Expand Down Expand Up @@ -76,12 +75,5 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) =>
}
});

if (!isWrapped(app.use) && isEnabled()) {
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()`.',
);
});
}
ensureIsWrapped(app.use, 'koa');
};
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
28 changes: 28 additions & 0 deletions packages/node/src/utils/ensureIsWrapped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { isWrapped } from '@opentelemetry/core';
import { hasTracingEnabled, isEnabled } from '@sentry/core';
import { consoleSandbox } from '@sentry/utils';
import { isCjs } from '../sdk/init';

/**
* Checks and warns if a framework isn't wrapped by opentelemetry.
*/
export function ensureIsWrapped(
maybeWrappedModule: unknown,
name: 'express' | 'connect' | 'fastify' | 'hapi' | 'koa',
): void {
if (!isWrapped(maybeWrappedModule) && isEnabled() && hasTracingEnabled()) {
consoleSandbox(() => {
if (isCjs()) {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] ${name} is not instrumented. This is likely because you required/imported ${name} before calling \`Sentry.init()\`.`,
);
} else {
// eslint-disable-next-line no-console
console.warn(
`[Sentry] ${name} 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/${name}/install/esm/.`,
);
}
});
}
}

0 comments on commit a30fbbc

Please sign in to comment.