-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ensureIsWrapped.ts
38 lines (36 loc) · 1.45 KB
/
ensureIsWrapped.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import { isWrapped } from '@opentelemetry/core';
import { getClient, getGlobalScope, hasTracingEnabled, isEnabled } from '@sentry/core';
import { consoleSandbox } from '@sentry/core';
import type { NodeClient } from '../sdk/client';
import { isCjs } from './commonjs';
import { createMissingInstrumentationContext } from './createMissingInstrumentationContext';
/**
* Checks and warns if a framework isn't wrapped by opentelemetry.
*/
export function ensureIsWrapped(
maybeWrappedFunction: unknown,
name: 'express' | 'connect' | 'fastify' | 'hapi' | 'koa',
): void {
const client = getClient<NodeClient>();
if (
!client?.getOptions().disableInstrumentationWarnings &&
!isWrapped(maybeWrappedFunction) &&
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/.`,
);
}
});
getGlobalScope().setContext('missing_instrumentation', createMissingInstrumentationContext(name));
}
}