Skip to content

Commit

Permalink
feat(node): Add disableInstrumentationWarnings option (#13693)
Browse files Browse the repository at this point in the history
Closes #13471

This can be used when you know what you're doing to avoid the warning
log.
  • Loading branch information
mydea authored Sep 18, 2024
1 parent b09a679 commit 18c9ab6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/node/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ export interface BaseNodeOptions {
*/
clientReportFlushInterval?: number;

/**
* By default, the SDK will try to identify problems with your instrumentation setup and warn you about it.
* If you want to disable these warnings, set this to `true`.
*/
disableInstrumentationWarnings?: boolean;

/** Callback that is executed when a fatal global error occurs. */
onFatalError?(this: void, error: Error): void;
}
Expand Down
13 changes: 10 additions & 3 deletions packages/node/src/utils/ensureIsWrapped.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
import { isWrapped } from '@opentelemetry/core';
import { getGlobalScope, hasTracingEnabled, isEnabled } from '@sentry/core';
import { getClient, getGlobalScope, hasTracingEnabled, isEnabled } from '@sentry/core';
import { consoleSandbox } from '@sentry/utils';
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(
maybeWrappedModule: unknown,
maybeWrappedFunction: unknown,
name: 'express' | 'connect' | 'fastify' | 'hapi' | 'koa',
): void {
if (!isWrapped(maybeWrappedModule) && isEnabled() && hasTracingEnabled()) {
const client = getClient<NodeClient>();
if (
!client?.getOptions().disableInstrumentationWarnings &&
!isWrapped(maybeWrappedFunction) &&
isEnabled() &&
hasTracingEnabled()
) {
consoleSandbox(() => {
if (isCjs()) {
// eslint-disable-next-line no-console
Expand Down
71 changes: 71 additions & 0 deletions packages/node/test/utils/ensureIsWrapped.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ensureIsWrapped } from '../../src/utils/ensureIsWrapped';
import { cleanupOtel, mockSdkInit, resetGlobals } from '../helpers/mockSdkInit';

const unwrappedFunction = () => {};

// We simulate a wrapped function
const wrappedfunction = Object.assign(() => {}, {
__wrapped: true,
__original: () => {},
__unwrap: () => {},
});

describe('ensureIsWrapped', () => {
afterEach(() => {
jest.restoreAllMocks();
cleanupOtel();
resetGlobals();
});

it('warns when the method is unwrapped', () => {
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});

mockSdkInit({ tracesSampleRate: 1 });

ensureIsWrapped(unwrappedFunction, 'express');

expect(spyWarn).toHaveBeenCalledTimes(1);
expect(spyWarn).toHaveBeenCalledWith(
'[Sentry] express is not instrumented. This is likely because you required/imported express before calling `Sentry.init()`.',
);
});

it('does not warn when the method is wrapped', () => {
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});

mockSdkInit({ tracesSampleRate: 1 });

ensureIsWrapped(wrappedfunction, 'express');

expect(spyWarn).toHaveBeenCalledTimes(0);
});

it('does not warn without a client', () => {
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});
resetGlobals();

ensureIsWrapped(wrappedfunction, 'express');

expect(spyWarn).toHaveBeenCalledTimes(0);
});

it('does not warn without tracing', () => {
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});

mockSdkInit({});

ensureIsWrapped(unwrappedFunction, 'express');

expect(spyWarn).toHaveBeenCalledTimes(0);
});

it('does not warn if disableInstrumentationWarnings=true', () => {
const spyWarn = jest.spyOn(console, 'warn').mockImplementation(() => {});

mockSdkInit({ tracesSampleRate: 1, disableInstrumentationWarnings: true });

ensureIsWrapped(unwrappedFunction, 'express');

expect(spyWarn).toHaveBeenCalledTimes(0);
});
});

0 comments on commit 18c9ab6

Please sign in to comment.