Skip to content

Commit 33449bb

Browse files
committed
ref(node): Only show instrumentation warning when tracing is enabled (#12119)
1 parent 10fbee2 commit 33449bb

File tree

6 files changed

+76
-26
lines changed

6 files changed

+76
-26
lines changed

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

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

1618
type ConnectApp = {
1719
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -48,12 +50,19 @@ export const setupConnectErrorHandler = (app: ConnectApp): void => {
4850
});
4951
}
5052

51-
if (!isWrapped(app.use) && isEnabled()) {
53+
if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) {
5254
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-
);
55+
if (isCjs()) {
56+
// eslint-disable-next-line no-console
57+
console.warn(
58+
'[Sentry] Connect is not instrumented. This is likely because you required/imported connect before calling `Sentry.init()`.',
59+
);
60+
} else {
61+
// eslint-disable-next-line no-console
62+
console.warn(
63+
'[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/.',
64+
);
65+
}
5766
});
5867
}
5968
};

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
@@ -12,13 +12,15 @@ import {
1212
getDefaultIsolationScope,
1313
getIsolationScope,
1414
getRootSpan,
15+
hasTracingEnabled,
1516
isEnabled,
1617
spanToJSON,
1718
} from '@sentry/core';
1819
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1920
import type { IntegrationFn, Span } from '@sentry/types';
2021
import { consoleSandbox, logger } from '@sentry/utils';
2122
import { DEBUG_BUILD } from '../../../debug-build';
23+
import { isCjs } from '../../../sdk/init';
2224
import type { Boom, RequestEvent, ResponseObject, Server } from './types';
2325

2426
const _hapiIntegration = (() => {
@@ -110,12 +112,19 @@ export async function setupHapiErrorHandler(server: Server): Promise<void> {
110112
}
111113

112114
// eslint-disable-next-line @typescript-eslint/unbound-method
113-
if (!isWrapped(server.register) && isEnabled()) {
115+
if (!isWrapped(server.register) && isEnabled() && hasTracingEnabled()) {
114116
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-
);
117+
if (isCjs()) {
118+
// eslint-disable-next-line no-console
119+
console.warn(
120+
'[Sentry] Hapi is not instrumented. This is likely because you required/imported hapi before calling `Sentry.init()`.',
121+
);
122+
} else {
123+
// eslint-disable-next-line no-console
124+
console.warn(
125+
'[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/',
126+
);
127+
}
119128
});
120129
}
121130
}

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)