Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(node): Add disableInstrumentationWarnings option #13693

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
Loading