Skip to content

Commit cd1e4c7

Browse files
committed
ref(node): Only show instrumentation warning when tracing is enabled (#12119)
1 parent ad4e9f7 commit cd1e4c7

File tree

6 files changed

+76
-27
lines changed

6 files changed

+76
-27
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { isWrapped } from '@opentelemetry/core';
22
import { ConnectInstrumentation } from '@opentelemetry/instrumentation-connect';
3-
import { captureException, defineIntegration, isEnabled } from '@sentry/core';
3+
import { captureException, defineIntegration, hasTracingEnabled, isEnabled } from '@sentry/core';
44
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
55
import type { IntegrationFn } from '@sentry/types';
66
import { consoleSandbox } from '@sentry/utils';
7+
import { isCjs } from '../../sdk/init';
78

89
type ConnectApp = {
910
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -30,12 +31,19 @@ function connectErrorMiddleware(err: any, req: any, res: any, next: any): void {
3031
export const setupConnectErrorHandler = (app: ConnectApp): void => {
3132
app.use(connectErrorMiddleware);
3233

33-
if (!isWrapped(app.use) && isEnabled()) {
34+
if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) {
3435
consoleSandbox(() => {
35-
// eslint-disable-next-line no-console
36-
console.warn(
37-
'[Sentry] Connect is not instrumented. This is likely because you required/imported connect before calling `Sentry.init()`.',
38-
);
36+
if (isCjs()) {
37+
// eslint-disable-next-line no-console
38+
console.warn(
39+
'[Sentry] Connect is not instrumented. This is likely because you required/imported connect before calling `Sentry.init()`.',
40+
);
41+
} else {
42+
// eslint-disable-next-line no-console
43+
console.warn(
44+
'[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/.',
45+
);
46+
}
3947
});
4048
}
4149
};

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
SEMANTIC_ATTRIBUTE_SENTRY_OP,
55
defineIntegration,
66
getDefaultIsolationScope,
7+
hasTracingEnabled,
78
isEnabled,
89
spanToJSON,
910
} from '@sentry/core';
@@ -15,6 +16,7 @@ import { isWrapped } from '@opentelemetry/core';
1516
import { consoleSandbox, logger } from '@sentry/utils';
1617
import { DEBUG_BUILD } from '../../debug-build';
1718
import type { NodeClient } from '../../sdk/client';
19+
import { isCjs } from '../../sdk/init';
1820
import { addOriginToSpan } from '../../utils/addOriginToSpan';
1921

2022
const _expressIntegration = (() => {
@@ -139,12 +141,21 @@ export function expressErrorHandler(options?: {
139141
export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void {
140142
app.use(expressErrorHandler());
141143

142-
if (!isWrapped(app.use) && isEnabled()) {
144+
if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) {
143145
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-
);
146+
consoleSandbox(() => {
147+
if (isCjs()) {
148+
// eslint-disable-next-line no-console
149+
console.warn(
150+
'[Sentry] Express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.',
151+
);
152+
} else {
153+
// eslint-disable-next-line no-console
154+
console.warn(
155+
'[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/.',
156+
);
157+
}
158+
});
148159
});
149160
}
150161
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ import {
77
defineIntegration,
88
getClient,
99
getIsolationScope,
10+
hasTracingEnabled,
1011
isEnabled,
1112
spanToJSON,
1213
} from '@sentry/core';
1314
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1415
import type { IntegrationFn, Span } from '@sentry/types';
1516
import { consoleSandbox } from '@sentry/utils';
17+
import { isCjs } from '../../sdk/init';
1618

1719
// We inline the types we care about here
1820
interface Fastify {
@@ -101,12 +103,21 @@ export function setupFastifyErrorHandler(fastify: Fastify): void {
101103
});
102104
}
103105

104-
if (!isWrapped(fastify.addHook) && isEnabled()) {
106+
if (!isWrapped(fastify.addHook) && isEnabled() && hasTracingEnabled()) {
105107
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-
);
108+
consoleSandbox(() => {
109+
if (isCjs()) {
110+
// eslint-disable-next-line no-console
111+
console.warn(
112+
'[Sentry] Fastify is not instrumented. This is likely because you required/imported fastify before calling `Sentry.init()`.',
113+
);
114+
} else {
115+
// eslint-disable-next-line no-console
116+
console.warn(
117+
'[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/',
118+
);
119+
}
120+
});
110121
});
111122
}
112123
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import {
99
getDefaultIsolationScope,
1010
getIsolationScope,
1111
getRootSpan,
12+
hasTracingEnabled,
1213
isEnabled,
1314
} from '@sentry/core';
1415
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1516
import type { IntegrationFn } from '@sentry/types';
1617
import { consoleSandbox, logger } from '@sentry/utils';
1718
import { DEBUG_BUILD } from '../../../debug-build';
19+
import { isCjs } from '../../../sdk/init';
1820
import type { Boom, RequestEvent, ResponseObject, Server } from './types';
1921

2022
const _hapiIntegration = (() => {
@@ -96,12 +98,19 @@ export async function setupHapiErrorHandler(server: Server): Promise<void> {
9698
await server.register(hapiErrorPlugin);
9799

98100
// eslint-disable-next-line @typescript-eslint/unbound-method
99-
if (!isWrapped(server.register) && isEnabled()) {
101+
if (!isWrapped(server.register) && isEnabled() && hasTracingEnabled()) {
100102
consoleSandbox(() => {
101-
// eslint-disable-next-line no-console
102-
console.warn(
103-
'[Sentry] Hapi is not instrumented. This is likely because you required/imported hapi before calling `Sentry.init()`.',
104-
);
103+
if (isCjs()) {
104+
// eslint-disable-next-line no-console
105+
console.warn(
106+
'[Sentry] Hapi is not instrumented. This is likely because you required/imported hapi before calling `Sentry.init()`.',
107+
);
108+
} else {
109+
// eslint-disable-next-line no-console
110+
console.warn(
111+
'[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/',
112+
);
113+
}
105114
});
106115
}
107116
}

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

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ import {
88
defineIntegration,
99
getDefaultIsolationScope,
1010
getIsolationScope,
11+
hasTracingEnabled,
1112
isEnabled,
1213
spanToJSON,
1314
} from '@sentry/core';
1415
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1516
import type { IntegrationFn, Span } from '@sentry/types';
1617
import { consoleSandbox, logger } from '@sentry/utils';
1718
import { DEBUG_BUILD } from '../../debug-build';
19+
import { isCjs } from '../../sdk/init';
1820

1921
function addKoaSpanAttributes(span: Span): void {
2022
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.http.otel.koa');
@@ -76,12 +78,19 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) =>
7678
}
7779
});
7880

79-
if (!isWrapped(app.use) && isEnabled()) {
81+
if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) {
8082
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-
);
83+
if (isCjs()) {
84+
// eslint-disable-next-line no-console
85+
console.warn(
86+
'[Sentry] Koa is not instrumented. This is likely because you required/imported koa before calling `Sentry.init()`.',
87+
);
88+
} else {
89+
// eslint-disable-next-line no-console
90+
console.warn(
91+
'[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/',
92+
);
93+
}
8594
});
8695
}
8796
};

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

0 commit comments

Comments
 (0)