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(otel): Disable node tracing auto instrumentation when using otel #6144

Merged
merged 2 commits into from
Nov 8, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion packages/node/src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export function tracingHandler(): (
const hub = getCurrentHub();
const options = hub.getClient()?.getOptions();

if (!options || req.method?.toUpperCase() === 'OPTIONS' || req.method?.toUpperCase() === 'HEAD') {
if (
!options ||
options.instrumenter !== 'sentry' ||
req.method?.toUpperCase() === 'OPTIONS' ||
req.method?.toUpperCase() === 'HEAD'
) {
return next();
}

Expand Down
7 changes: 7 additions & 0 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,13 @@ export class Http implements Integration {
}

const clientOptions = setupOnceGetCurrentHub().getClient<NodeClient>()?.getOptions();

// Do not auto-instrument for other instrumenter
if (clientOptions && clientOptions.instrumenter !== 'sentry') {
__DEBUG_BUILD__ && logger.log('HTTP Integration is skipped because of instrumenter configuration.');
return;
}

const wrappedHandlerMaker = _createWrappedRequestMethodFactory(this._breadcrumbs, this._tracing, clientOptions);

// eslint-disable-next-line @typescript-eslint/no-var-requires
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/helper/node-client-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export function getDefaultNodeClientOptions(options: Partial<NodeClientOptions>
integrations: [],
transport: () => createTransport({ recordDroppedEvent: () => undefined }, _ => resolvedSyncPromise({})),
stackParser: () => [],
instrumenter: 'sentry',
...options,
};
}
24 changes: 23 additions & 1 deletion packages/node/test/integrations/http.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as sentryCore from '@sentry/core';
import { Hub } from '@sentry/core';
import { addExtensionMethods, Span, TRACEPARENT_REGEXP, Transaction } from '@sentry/tracing';
import { TransactionContext } from '@sentry/types';
import { parseSemver } from '@sentry/utils';
import { logger, parseSemver } from '@sentry/utils';
import * as http from 'http';
import * as https from 'https';
import * as HttpsProxyAgent from 'https-proxy-agent';
Expand Down Expand Up @@ -188,6 +188,28 @@ describe('tracing', () => {
expect(transaction.metadata.propagations).toBe(2);
});

it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');

const options = getDefaultNodeClientOptions({
dsn: 'https://dogsarebadatkeepingsecrets@squirrelchasers.ingest.sentry.io/12312012',
tracesSampleRate: 1.0,
integrations: [new HttpIntegration({ tracing: true })],
release: '1.0.0',
environment: 'production',
instrumenter: 'otel',
});
const hub = new Hub(new NodeClient(options));

const integration = new HttpIntegration();
integration.setupOnce(
() => {},
() => hub,
);

expect(loggerLogSpy).toBeCalledWith('HTTP Integration is skipped because of instrumenter configuration.');
});

describe('tracePropagationTargets option', () => {
beforeEach(() => {
// hacky way of restoring monkey patched functions
Expand Down
7 changes: 7 additions & 0 deletions packages/tracing/src/integrations/node/apollo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Hub } from '@sentry/core';
import { EventProcessor, Integration } from '@sentry/types';
import { arrayify, fill, isThenable, loadModule, logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

type ApolloResolverGroup = {
[key: string]: () => unknown;
};
Expand Down Expand Up @@ -39,6 +41,11 @@ export class Apollo implements Integration {
return;
}

if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Apollo Integration is skipped because of instrumenter configuration.');
return;
}

/**
* Iterate over resolvers of the ApolloServer instance before schemas are constructed.
*/
Expand Down
12 changes: 10 additions & 2 deletions packages/tracing/src/integrations/node/express.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* eslint-disable max-lines */
import { Integration, PolymorphicRequest, Transaction } from '@sentry/types';
import { Hub, Integration, PolymorphicRequest, Transaction } from '@sentry/types';
import { extractPathForTransaction, getNumberOfUrlSegments, isRegExp, logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

type Method =
| 'all'
| 'get'
Expand Down Expand Up @@ -105,11 +107,17 @@ export class Express implements Integration {
/**
* @inheritDoc
*/
public setupOnce(): void {
public setupOnce(_: unknown, getCurrentHub: () => Hub): void {
if (!this._router) {
__DEBUG_BUILD__ && logger.error('ExpressIntegration is missing an Express instance');
return;
}

if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Express Integration is skipped because of instrumenter configuration.');
return;
}

instrumentMiddlewares(this._router, this._methods);
instrumentRouter(this._router as ExpressRouter);
}
Expand Down
7 changes: 7 additions & 0 deletions packages/tracing/src/integrations/node/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Hub } from '@sentry/core';
import { EventProcessor, Integration } from '@sentry/types';
import { fill, isThenable, loadModule, logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

/** Tracing integration for graphql package */
export class GraphQL implements Integration {
/**
Expand All @@ -27,6 +29,11 @@ export class GraphQL implements Integration {
return;
}

if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('GraphQL Integration is skipped because of instrumenter configuration.');
return;
}

fill(pkg, 'execute', function (orig: () => void | Promise<unknown>) {
return function (this: unknown, ...args: unknown[]) {
const scope = getCurrentHub().getScope();
Expand Down
7 changes: 7 additions & 0 deletions packages/tracing/src/integrations/node/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Hub } from '@sentry/core';
import { EventProcessor, Integration, SpanContext } from '@sentry/types';
import { fill, isThenable, loadModule, logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

// This allows us to use the same array for both defaults options and the type itself.
// (note `as const` at the end to make it a union of string literal types (i.e. "a" | "b" | ... )
// and not just a string[])
Expand Down Expand Up @@ -125,6 +127,11 @@ export class Mongo implements Integration {
return;
}

if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Mongo Integration is skipped because of instrumenter configuration.');
return;
}

this._instrumentOperations(pkg.Collection, this._operations, getCurrentHub);
}

Expand Down
7 changes: 7 additions & 0 deletions packages/tracing/src/integrations/node/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Hub } from '@sentry/core';
import { EventProcessor, Integration } from '@sentry/types';
import { fill, loadModule, logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

interface MysqlConnection {
createQuery: () => void;
}
Expand Down Expand Up @@ -29,6 +31,11 @@ export class Mysql implements Integration {
return;
}

if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Mysql Integration is skipped because of instrumenter configuration.');
return;
}

// The original function will have one of these signatures:
// function (callback) => void
// function (options, callback) => void
Expand Down
7 changes: 7 additions & 0 deletions packages/tracing/src/integrations/node/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Hub } from '@sentry/core';
import { EventProcessor, Integration } from '@sentry/types';
import { fill, isThenable, loadModule, logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

interface PgClient {
prototype: {
query: () => void | Promise<unknown>;
Expand Down Expand Up @@ -41,6 +43,11 @@ export class Postgres implements Integration {
return;
}

if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Postgres Integration is skipped because of instrumenter configuration.');
return;
}

if (this._usePgNative && !pkg.native?.Client) {
__DEBUG_BUILD__ && logger.error("Postgres Integration was unable to access 'pg-native' bindings.");
return;
Expand Down
7 changes: 7 additions & 0 deletions packages/tracing/src/integrations/node/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Hub } from '@sentry/core';
import { EventProcessor, Integration } from '@sentry/types';
import { isThenable, logger } from '@sentry/utils';

import { shouldDisableAutoInstrumentation } from './utils/node-utils';

type PrismaAction =
| 'findUnique'
| 'findMany'
Expand Down Expand Up @@ -80,6 +82,11 @@ export class Prisma implements Integration {
return;
}

if (shouldDisableAutoInstrumentation(getCurrentHub)) {
__DEBUG_BUILD__ && logger.log('Prisma Integration is skipped because of instrumenter configuration.');
return;
}

this._client.$use((params, next: (params: PrismaMiddlewareParams) => Promise<unknown>) => {
const scope = getCurrentHub().getScope();
const parentSpan = scope?.getSpan();
Expand Down
14 changes: 14 additions & 0 deletions packages/tracing/src/integrations/node/utils/node-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Hub } from '@sentry/types';

/**
* Check if Sentry auto-instrumentation should be disabled.
*
* @param getCurrentHub A method to fetch the current hub
* @returns boolean
*/
export function shouldDisableAutoInstrumentation(getCurrentHub: () => Hub): boolean {
const clientOptions = getCurrentHub().getClient()?.getOptions();
const instrumenter = clientOptions?.instrumenter || 'sentry';

return instrumenter !== 'sentry';
}
17 changes: 17 additions & 0 deletions packages/tracing/test/integrations/apollo.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';

import { Apollo } from '../../src/integrations/node/apollo';
import { Span } from '../../src/span';
import { getTestClient } from '../testutils';

type ApolloResolverGroup = {
[key: string]: () => any;
Expand Down Expand Up @@ -100,4 +102,19 @@ describe('setupOnce', () => {
});
expect(childSpan.finish).toBeCalled();
});

it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');

const client = getTestClient({ instrumenter: 'otel' });
const hub = new Hub(client);

const integration = new Apollo();
integration.setupOnce(
() => {},
() => hub,
);

expect(loggerLogSpy).toBeCalledWith('Apollo Integration is skipped because of instrumenter configuration.');
});
});
17 changes: 17 additions & 0 deletions packages/tracing/test/integrations/graphql.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';

import { GraphQL } from '../../src/integrations/node/graphql';
import { Span } from '../../src/span';
import { getTestClient } from '../testutils';

const GQLExecute = {
execute() {
Expand Down Expand Up @@ -53,4 +55,19 @@ describe('setupOnce', () => {
expect(childSpan.finish).toBeCalled();
expect(scope.setSpan).toHaveBeenCalledTimes(2);
});

it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');

const client = getTestClient({ instrumenter: 'otel' });
const hub = new Hub(client);

const integration = new GraphQL();
integration.setupOnce(
() => {},
() => hub,
);

expect(loggerLogSpy).toBeCalledWith('GraphQL Integration is skipped because of instrumenter configuration.');
});
});
17 changes: 17 additions & 0 deletions packages/tracing/test/integrations/node/mongo.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';

import { Mongo } from '../../../src/integrations/node/mongo';
import { Span } from '../../../src/span';
import { getTestClient } from '../../testutils';

class Collection {
public collectionName: string = 'mockedCollectionName';
Expand Down Expand Up @@ -111,4 +113,19 @@ describe('patchOperation()', () => {
});
expect(childSpan.finish).toBeCalled();
});

it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');

const client = getTestClient({ instrumenter: 'otel' });
const hub = new Hub(client);

const integration = new Mongo();
integration.setupOnce(
() => {},
() => hub,
);

expect(loggerLogSpy).toBeCalledWith('Mongo Integration is skipped because of instrumenter configuration.');
});
});
17 changes: 17 additions & 0 deletions packages/tracing/test/integrations/node/postgres.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
/* eslint-disable @typescript-eslint/unbound-method */
import { Hub, Scope } from '@sentry/core';
import { logger } from '@sentry/utils';

import { Postgres } from '../../../src/integrations/node/postgres';
import { Span } from '../../../src/span';
import { getTestClient } from '../../testutils';

class PgClient {
// https://node-postgres.com/api/client#clientquery
Expand Down Expand Up @@ -94,4 +96,19 @@ describe('setupOnce', () => {
expect(childSpan.finish).toBeCalled();
});
});

it("doesn't attach when using otel instrumenter", () => {
const loggerLogSpy = jest.spyOn(logger, 'log');

const client = getTestClient({ instrumenter: 'otel' });
const hub = new Hub(client);

const integration = new Postgres();
integration.setupOnce(
() => {},
() => hub,
);

expect(loggerLogSpy).toBeCalledWith('Postgres Integration is skipped because of instrumenter configuration.');
});
});