From e6fdc014cb06fc20f30168c646d8857b411c3748 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 16 Mar 2023 22:49:34 +0000 Subject: [PATCH 1/2] Move `registerErrorInstrumentation` to core and call it from `addTracingExtensions` --- .../src => core/src/tracing}/errors.ts | 12 ++++++++++-- packages/core/src/tracing/hubextensions.ts | 3 +++ packages/tracing-internal/src/extensions.ts | 5 ----- 3 files changed, 13 insertions(+), 7 deletions(-) rename packages/{tracing-internal/src => core/src/tracing}/errors.ts (77%) diff --git a/packages/tracing-internal/src/errors.ts b/packages/core/src/tracing/errors.ts similarity index 77% rename from packages/tracing-internal/src/errors.ts rename to packages/core/src/tracing/errors.ts index 3d41e43830f0..3351f428fecf 100644 --- a/packages/tracing-internal/src/errors.ts +++ b/packages/core/src/tracing/errors.ts @@ -1,11 +1,19 @@ -import type { SpanStatusType } from '@sentry/core'; -import { getActiveTransaction } from '@sentry/core'; import { addInstrumentationHandler, logger } from '@sentry/utils'; +import type { SpanStatusType } from './span'; +import { getActiveTransaction } from './utils'; + +let errorsInstrumented = false; + /** * Configures global error listeners */ export function registerErrorInstrumentation(): void { + if (errorsInstrumented) { + return; + } + + errorsInstrumented = true; addInstrumentationHandler('error', errorCallback); addInstrumentationHandler('unhandledrejection', errorCallback); } diff --git a/packages/core/src/tracing/hubextensions.ts b/packages/core/src/tracing/hubextensions.ts index 9624df4b709e..afcc21c8f704 100644 --- a/packages/core/src/tracing/hubextensions.ts +++ b/packages/core/src/tracing/hubextensions.ts @@ -6,6 +6,7 @@ import { getMainCarrier } from '../hub'; import { hasTracingEnabled } from '../utils/hasTracingEnabled'; import { IdleTransaction } from './idletransaction'; import { Transaction } from './transaction'; +import { registerErrorInstrumentation } from './errors'; /** Returns all trace headers that are currently on the top scope. */ function traceHeaders(this: Hub): { [key: string]: string } { @@ -237,4 +238,6 @@ export function addTracingExtensions(): void { if (!carrier.__SENTRY__.extensions.traceHeaders) { carrier.__SENTRY__.extensions.traceHeaders = traceHeaders; } + + registerErrorInstrumentation(); } diff --git a/packages/tracing-internal/src/extensions.ts b/packages/tracing-internal/src/extensions.ts index e3c2ab511804..555a1451b55e 100644 --- a/packages/tracing-internal/src/extensions.ts +++ b/packages/tracing-internal/src/extensions.ts @@ -2,8 +2,6 @@ import { addTracingExtensions, getMainCarrier } from '@sentry/core'; import type { Integration, IntegrationClass } from '@sentry/types'; import { dynamicRequire, isNodeEnv, loadModule } from '@sentry/utils'; -import { registerErrorInstrumentation } from './errors'; - /** * @private */ @@ -66,7 +64,4 @@ export function addExtensionMethods(): void { if (isNodeEnv()) { _autoloadDatabaseIntegrations(); } - - // If an error happens globally, we should make sure transaction status is set to error. - registerErrorInstrumentation(); } From 307fcaee8b0cc31d0423ede775a4b04b75590c92 Mon Sep 17 00:00:00 2001 From: Tim Fish Date: Thu, 16 Mar 2023 23:25:51 +0000 Subject: [PATCH 2/2] Move test --- packages/core/src/tracing/hubextensions.ts | 2 +- .../test => core/test/lib/tracing}/errors.test.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename packages/{tracing-internal/test => core/test/lib/tracing}/errors.test.ts (94%) diff --git a/packages/core/src/tracing/hubextensions.ts b/packages/core/src/tracing/hubextensions.ts index afcc21c8f704..b5518f95e04b 100644 --- a/packages/core/src/tracing/hubextensions.ts +++ b/packages/core/src/tracing/hubextensions.ts @@ -4,9 +4,9 @@ import { isNaN, logger } from '@sentry/utils'; import type { Hub } from '../hub'; import { getMainCarrier } from '../hub'; import { hasTracingEnabled } from '../utils/hasTracingEnabled'; +import { registerErrorInstrumentation } from './errors'; import { IdleTransaction } from './idletransaction'; import { Transaction } from './transaction'; -import { registerErrorInstrumentation } from './errors'; /** Returns all trace headers that are currently on the top scope. */ function traceHeaders(this: Hub): { [key: string]: string } { diff --git a/packages/tracing-internal/test/errors.test.ts b/packages/core/test/lib/tracing/errors.test.ts similarity index 94% rename from packages/tracing-internal/test/errors.test.ts rename to packages/core/test/lib/tracing/errors.test.ts index 1dba00b825b1..8bf12f675032 100644 --- a/packages/tracing-internal/test/errors.test.ts +++ b/packages/core/test/lib/tracing/errors.test.ts @@ -2,8 +2,8 @@ import { BrowserClient } from '@sentry/browser'; import { addTracingExtensions, Hub, makeMain } from '@sentry/core'; import type { InstrumentHandlerCallback, InstrumentHandlerType } from '@sentry/utils'; -import { getDefaultBrowserClientOptions } from '../../tracing/test/testutils'; -import { registerErrorInstrumentation } from '../src/errors'; +import { getDefaultBrowserClientOptions } from '../../../../tracing/test/testutils'; +import { registerErrorInstrumentation } from '../../../src/tracing/errors'; const mockAddInstrumentationHandler = jest.fn(); let mockErrorCallback: InstrumentHandlerCallback = () => undefined;