Skip to content

Commit 18c9ab6

Browse files
authored
feat(node): Add disableInstrumentationWarnings option (#13693)
Closes #13471 This can be used when you know what you're doing to avoid the warning log.
1 parent b09a679 commit 18c9ab6

File tree

3 files changed

+87
-3
lines changed

3 files changed

+87
-3
lines changed

packages/node/src/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ export interface BaseNodeOptions {
125125
*/
126126
clientReportFlushInterval?: number;
127127

128+
/**
129+
* By default, the SDK will try to identify problems with your instrumentation setup and warn you about it.
130+
* If you want to disable these warnings, set this to `true`.
131+
*/
132+
disableInstrumentationWarnings?: boolean;
133+
128134
/** Callback that is executed when a fatal global error occurs. */
129135
onFatalError?(this: void, error: Error): void;
130136
}

packages/node/src/utils/ensureIsWrapped.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,24 @@
11
import { isWrapped } from '@opentelemetry/core';
2-
import { getGlobalScope, hasTracingEnabled, isEnabled } from '@sentry/core';
2+
import { getClient, getGlobalScope, hasTracingEnabled, isEnabled } from '@sentry/core';
33
import { consoleSandbox } from '@sentry/utils';
4+
import type { NodeClient } from '../sdk/client';
45
import { isCjs } from './commonjs';
56
import { createMissingInstrumentationContext } from './createMissingInstrumentationContext';
67

78
/**
89
* Checks and warns if a framework isn't wrapped by opentelemetry.
910
*/
1011
export function ensureIsWrapped(
11-
maybeWrappedModule: unknown,
12+
maybeWrappedFunction: unknown,
1213
name: 'express' | 'connect' | 'fastify' | 'hapi' | 'koa',
1314
): void {
14-
if (!isWrapped(maybeWrappedModule) && isEnabled() && hasTracingEnabled()) {
15+
const client = getClient<NodeClient>();
16+
if (
17+
!client?.getOptions().disableInstrumentationWarnings &&
18+
!isWrapped(maybeWrappedFunction) &&
19+
isEnabled() &&
20+
hasTracingEnabled()
21+
) {
1522
consoleSandbox(() => {
1623
if (isCjs()) {
1724
// eslint-disable-next-line no-console
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import { ensureIsWrapped } from '../../src/utils/ensureIsWrapped';
2+
import { cleanupOtel, mockSdkInit, resetGlobals } from '../helpers/mockSdkInit';
3+
4+
const unwrappedFunction = () => {};
5+
6+
// We simulate a wrapped function
7+
const wrappedfunction = Object.assign(() => {}, {
8+
__wrapped: true,
9+
__original: () => {},
10+
__unwrap: () => {},
11+
});
12+
13+
describe('ensureIsWrapped', () => {
14+
afterEach(() => {
15+
jest.restoreAllMocks();
16+
cleanupOtel();
17+
resetGlobals();
18+
});
19+
20+
it('warns when the method is unwrapped', () => {
21+
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
22+
23+
mockSdkInit({ tracesSampleRate: 1 });
24+
25+
ensureIsWrapped(unwrappedFunction, 'express');
26+
27+
expect(spyWarn).toHaveBeenCalledTimes(1);
28+
expect(spyWarn).toHaveBeenCalledWith(
29+
'[Sentry] express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.',
30+
);
31+
});
32+
33+
it('does not warn when the method is wrapped', () => {
34+
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
35+
36+
mockSdkInit({ tracesSampleRate: 1 });
37+
38+
ensureIsWrapped(wrappedfunction, 'express');
39+
40+
expect(spyWarn).toHaveBeenCalledTimes(0);
41+
});
42+
43+
it('does not warn without a client', () => {
44+
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
45+
resetGlobals();
46+
47+
ensureIsWrapped(wrappedfunction, 'express');
48+
49+
expect(spyWarn).toHaveBeenCalledTimes(0);
50+
});
51+
52+
it('does not warn without tracing', () => {
53+
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
54+
55+
mockSdkInit({});
56+
57+
ensureIsWrapped(unwrappedFunction, 'express');
58+
59+
expect(spyWarn).toHaveBeenCalledTimes(0);
60+
});
61+
62+
it('does not warn if disableInstrumentationWarnings=true', () => {
63+
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
64+
65+
mockSdkInit({ tracesSampleRate: 1, disableInstrumentationWarnings: true });
66+
67+
ensureIsWrapped(unwrappedFunction, 'express');
68+
69+
expect(spyWarn).toHaveBeenCalledTimes(0);
70+
});
71+
});

0 commit comments

Comments
 (0)