Skip to content

Commit

Permalink
feat(otel): Disable node tracing auto instrumentation when using otel
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Nov 8, 2022
1 parent eb04258 commit 312b948
Show file tree
Hide file tree
Showing 11 changed files with 79 additions and 3 deletions.
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
6 changes: 6 additions & 0 deletions packages/node/src/integrations/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ export class Http implements Integration {
}

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

// Do not auto-instrument for other instrumenter
if (clientOptions && clientOptions.instrumenter !== 'sentry') {
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,
};
}
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';
}

0 comments on commit 312b948

Please sign in to comment.