Skip to content

Commit 239817f

Browse files
committed
ref(node): Extract framework wrapping warning into utility function
1 parent 33449bb commit 239817f

File tree

6 files changed

+42
-112
lines changed

6 files changed

+42
-112
lines changed

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

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +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-
hasTracingEnabled,
10-
isEnabled,
118
spanToJSON,
129
} from '@sentry/core';
1310
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1411
import type { IntegrationFn, Span } from '@sentry/types';
15-
import { consoleSandbox } from '@sentry/utils';
16-
import { isCjs } from '../../sdk/init';
12+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
1713

1814
type ConnectApp = {
1915
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -50,21 +46,7 @@ export const setupConnectErrorHandler = (app: ConnectApp): void => {
5046
});
5147
}
5248

53-
if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) {
54-
consoleSandbox(() => {
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-
}
66-
});
67-
}
49+
ensureIsWrapped(app.use, 'connect');
6850
};
6951

7052
function addConnectSpanAttributes(span: Span): void {

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

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +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-
hasTracingEnabled,
8-
isEnabled,
9-
spanToJSON,
10-
} from '@sentry/core';
3+
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, defineIntegration, getDefaultIsolationScope, spanToJSON } from '@sentry/core';
114
import { captureException, getClient, getIsolationScope } from '@sentry/core';
125
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
136
import type { IntegrationFn } from '@sentry/types';
14-
15-
import { isWrapped } from '@opentelemetry/core';
16-
import { consoleSandbox, logger } from '@sentry/utils';
7+
import { logger } from '@sentry/utils';
178
import { DEBUG_BUILD } from '../../debug-build';
189
import type { NodeClient } from '../../sdk/client';
19-
import { isCjs } from '../../sdk/init';
2010
import { addOriginToSpan } from '../../utils/addOriginToSpan';
11+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
2112

2213
const _expressIntegration = (() => {
2314
return {
@@ -140,24 +131,7 @@ export function expressErrorHandler(options?: {
140131
*/
141132
export function setupExpressErrorHandler(app: { use: (middleware: ExpressMiddleware) => unknown }): void {
142133
app.use(expressErrorHandler());
143-
144-
if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) {
145-
consoleSandbox(() => {
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-
});
159-
});
160-
}
134+
ensureIsWrapped(app.use, 'express');
161135
}
162136

163137
function getStatusCodeFromResponse(error: MiddlewareError): number {

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

Lines changed: 2 additions & 22 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,14 +6,11 @@ import {
76
defineIntegration,
87
getClient,
98
getIsolationScope,
10-
hasTracingEnabled,
11-
isEnabled,
129
spanToJSON,
1310
} from '@sentry/core';
1411
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1512
import type { IntegrationFn, Span } from '@sentry/types';
16-
import { consoleSandbox } from '@sentry/utils';
17-
import { isCjs } from '../../sdk/init';
13+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
1814

1915
// We inline the types we care about here
2016
interface Fastify {
@@ -103,23 +99,7 @@ export function setupFastifyErrorHandler(fastify: Fastify): void {
10399
});
104100
}
105101

106-
if (!isWrapped(fastify.addHook) && isEnabled() && hasTracingEnabled()) {
107-
consoleSandbox(() => {
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-
});
121-
});
122-
}
102+
ensureIsWrapped(fastify.addHook, 'fastify');
123103
}
124104

125105
function addFastifySpanAttributes(span: Span): void {

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

Lines changed: 3 additions & 20 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,15 +11,13 @@ import {
1211
getDefaultIsolationScope,
1312
getIsolationScope,
1413
getRootSpan,
15-
hasTracingEnabled,
16-
isEnabled,
1714
spanToJSON,
1815
} from '@sentry/core';
1916
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
2017
import type { IntegrationFn, Span } from '@sentry/types';
21-
import { consoleSandbox, logger } from '@sentry/utils';
18+
import { logger } from '@sentry/utils';
2219
import { DEBUG_BUILD } from '../../../debug-build';
23-
import { isCjs } from '../../../sdk/init';
20+
import { ensureIsWrapped } from '../../../utils/ensureIsWrapped';
2421
import type { Boom, RequestEvent, ResponseObject, Server } from './types';
2522

2623
const _hapiIntegration = (() => {
@@ -112,21 +109,7 @@ export async function setupHapiErrorHandler(server: Server): Promise<void> {
112109
}
113110

114111
// eslint-disable-next-line @typescript-eslint/unbound-method
115-
if (!isWrapped(server.register) && isEnabled() && hasTracingEnabled()) {
116-
consoleSandbox(() => {
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-
}
128-
});
129-
}
112+
ensureIsWrapped(server.register, 'hapi');
130113
}
131114

132115
function addHapiSpanAttributes(span: Span): void {

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

Lines changed: 3 additions & 20 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,15 +7,13 @@ import {
87
defineIntegration,
98
getDefaultIsolationScope,
109
getIsolationScope,
11-
hasTracingEnabled,
12-
isEnabled,
1310
spanToJSON,
1411
} from '@sentry/core';
1512
import { addOpenTelemetryInstrumentation } from '@sentry/opentelemetry';
1613
import type { IntegrationFn, Span } from '@sentry/types';
17-
import { consoleSandbox, logger } from '@sentry/utils';
14+
import { logger } from '@sentry/utils';
1815
import { DEBUG_BUILD } from '../../debug-build';
19-
import { isCjs } from '../../sdk/init';
16+
import { ensureIsWrapped } from '../../utils/ensureIsWrapped';
2017

2118
function addKoaSpanAttributes(span: Span): void {
2219
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.http.otel.koa');
@@ -78,19 +75,5 @@ export const setupKoaErrorHandler = (app: { use: (arg0: (ctx: any, next: any) =>
7875
}
7976
});
8077

81-
if (!isWrapped(app.use) && isEnabled() && hasTracingEnabled()) {
82-
consoleSandbox(() => {
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-
}
94-
});
95-
}
78+
ensureIsWrapped(app.use, 'koa');
9679
};
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)