Skip to content

Commit a30fbbc

Browse files
authored
ref(node): Only show instrumentation warning when tracing is enabled (#12141)
Also gives a better warning in ESM mode with a helpful link to the docs.
1 parent f7bd662 commit a30fbbc

File tree

7 files changed

+44
-64
lines changed

7 files changed

+44
-64
lines changed

packages/node/src/integrations/tracing/connect.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { isWrapped } from '@opentelemetry/core';
21
import { ConnectInstrumentation } from '@opentelemetry/instrumentation-connect';
32
import {
43
SEMANTIC_ATTRIBUTE_SENTRY_OP,
54
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
65
captureException,
76
defineIntegration,
87
getClient,
9-
isEnabled,
108
spanToJSON,
119
} from '@sentry/core';
1210
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1311
import type { IntegrationFn, Span } from '@sentry/types';
14-
import { consoleSandbox } from '@sentry/utils';
12+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
1513

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

51-
if (!isWrapped(app.use) && isEnabled()) {
52-
consoleSandbox(() => {
53-
// eslint-disable-next-line no-console
54-
console.warn(
55-
'[Sentry] Connect is not instrumented. This is likely because you required/imported connect before calling `Sentry.init()`.',
56-
);
57-
});
58-
}
49+
ensureIsWrapped(app.use, 'connect');
5950
};
6051

6152
function addConnectSpanAttributes(span: Span): void {

packages/node/src/integrations/tracing/express.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
import type * as http from 'node:http';
22
import { ExpressInstrumentation } from '@opentelemetry/instrumentation-express';
3-
import {
4-
SEMANTIC_ATTRIBUTE_SENTRY_OP,
5-
defineIntegration,
6-
getDefaultIsolationScope,
7-
isEnabled,
8-
spanToJSON,
9-
} from '@sentry/core';
3+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, defineIntegration, getDefaultIsolationScope, spanToJSON } from '@sentry/core';
104
import { captureException, getClient, getIsolationScope } from '@sentry/core';
115
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
126
import type { IntegrationFn } from '@sentry/types';
13-
14-
import { isWrapped } from '@opentelemetry/core';
15-
import { consoleSandbox, logger } from '@sentry/utils';
7+
import { logger } from '@sentry/utils';
168
import { DEBUG_BUILD } from '../../debug-build';
179
import type { NodeClient } from '../../sdk/client';
1810
import { addOriginToSpan } from '../../utils/addOriginToSpan';
11+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
1912

2013
const _expressIntegration = (() => {
2114
return {
@@ -138,15 +131,7 @@ export function expressErrorHandler(options?: {
138131
*/
139132
export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void {
140133
app.use(expressErrorHandler());
141-
142-
if (!isWrapped(app.use) && isEnabled()) {
143-
consoleSandbox(() => {
144-
// eslint-disable-next-line no-console
145-
console.warn(
146-
'[Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.',
147-
);
148-
});
149-
}
134+
ensureIsWrapped(app.use, 'express');
150135
}
151136

152137
function getStatusCodeFromResponse(error: MiddlewareError): number {

packages/node/src/integrations/tracing/fastify.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isWrapped } from '@opentelemetry/core';
21
import { FastifyInstrumentation } from '@opentelemetry/instrumentation-fastify';
32
import {
43
SEMANTIC_ATTRIBUTE_SENTRY_OP,
@@ -7,12 +6,11 @@ import {
76
defineIntegration,
87
getClient,
98
getIsolationScope,
10-
isEnabled,
119
spanToJSON,
1210
} from '@sentry/core';
1311
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1412
import type { IntegrationFn, Span } from '@sentry/types';
15-
import { consoleSandbox } from '@sentry/utils';
13+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
1614

1715
// We inline the types we care about here
1816
interface Fastify {
@@ -101,14 +99,7 @@ export function setupFastifyErrorHandler(fastify: Fastify): void {
10199
});
102100
}
103101

104-
if (!isWrapped(fastify.addHook) && isEnabled()) {
105-
consoleSandbox(() => {
106-
// eslint-disable-next-line no-console
107-
console.warn(
108-
'[Sentry] Fastify is not instrumented. This is likely because you required/imported fastify before calling `Sentry.init()`.',
109-
);
110-
});
111-
}
102+
ensureIsWrapped(fastify.addHook, 'fastify');
112103
}
113104

114105
function addFastifySpanAttributes(span: Span): void {

packages/node/src/integrations/tracing/hapi/index.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isWrapped } from '@opentelemetry/core';
21
import { HapiInstrumentation } from '@opentelemetry/instrumentation-hapi';
32
import {
43
SDK_VERSION,
@@ -12,13 +11,13 @@ import {
1211
getDefaultIsolationScope,
1312
getIsolationScope,
1413
getRootSpan,
15-
isEnabled,
1614
spanToJSON,
1715
} from '@sentry/core';
1816
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1917
import type { IntegrationFn, Span } from '@sentry/types';
20-
import { consoleSandbox, logger } from '@sentry/utils';
18+
import { logger } from '@sentry/utils';
2119
import { DEBUG_BUILD } from '../../../debug-build';
20+
import { ensureIsWrapped } from '../../../utils/ensureIsWrapped';
2221
import type { Boom, RequestEvent, ResponseObject, Server } from './types';
2322

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

112111
// eslint-disable-next-line @typescript-eslint/unbound-method
113-
if (!isWrapped(server.register) && isEnabled()) {
114-
consoleSandbox(() => {
115-
// eslint-disable-next-line no-console
116-
console.warn(
117-
'[Sentry] Hapi is not instrumented. This is likely because you required/imported hapi before calling `Sentry.init()`.',
118-
);
119-
});
120-
}
112+
ensureIsWrapped(server.register, 'hapi');
121113
}
122114

123115
function addHapiSpanAttributes(span: Span): void {

packages/node/src/integrations/tracing/koa.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { isWrapped } from '@opentelemetry/core';
21
import { KoaInstrumentation } from '@opentelemetry/instrumentation-koa';
32
import { SEMATTRS_HTTP_ROUTE } from '@opentelemetry/semantic-conventions';
43
import {
@@ -8,13 +7,13 @@ import {
87
defineIntegration,
98
getDefaultIsolationScope,
109
getIsolationScope,
11-
isEnabled,
1210
spanToJSON,
1311
} from '@sentry/core';
1412
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1513
import type { IntegrationFn, Span } from '@sentry/types';
16-
import { consoleSandbox, logger } from '@sentry/utils';
14+
import { logger } from '@sentry/utils';
1715
import { DEBUG_BUILD } from '../../debug-build';
16+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
1817

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

79-
if (!isWrapped(app.use) && isEnabled()) {
80-
consoleSandbox(() => {
81-
// eslint-disable-next-line no-console
82-
console.warn(
83-
'[Sentry] Koa is not instrumented. This is likely because you required/imported koa before calling `Sentry.init()`.',
84-
);
85-
});
86-
}
78+
ensureIsWrapped(app.use, 'koa');
8779
};

packages/node/src/sdk/init.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ import { defaultStackParser, getSentryRelease } from './api';
4141
import { NodeClient } from './client';
4242
import { initOpenTelemetry } from './initOtel';
4343

44-
function isCjs(): boolean {
44+
/** Detect CommonJS. */
45+
export function isCjs(): boolean {
4546
return typeof require !== 'undefined';
4647
}
4748

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { isWrapped } from '@opentelemetry/core';
2+
import { hasTracingEnabled, isEnabled } from '@sentry/core';
3+
import { consoleSandbox } from '@sentry/utils';
4+
import { isCjs } from '../sdk/init';
5+
6+
/**
7+
* Checks and warns if a framework isn't wrapped by opentelemetry.
8+
*/
9+
export function ensureIsWrapped(
10+
maybeWrappedModule: unknown,
11+
name: 'express' | 'connect' | 'fastify' | 'hapi' | 'koa',
12+
): void {
13+
if (!isWrapped(maybeWrappedModule) && isEnabled() && hasTracingEnabled()) {
14+
consoleSandbox(() => {
15+
if (isCjs()) {
16+
// eslint-disable-next-line no-console
17+
console.warn(
18+
`[Sentry] ${name} is not instrumented. This is likely because you required/imported ${name} before calling \`Sentry.init()\`.`,
19+
);
20+
} else {
21+
// eslint-disable-next-line no-console
22+
console.warn(
23+
`[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/.`,
24+
);
25+
}
26+
});
27+
}
28+
}

0 commit comments

Comments
 (0)