From 1ae302227662132ffaad8bab45871c408761be72 Mon Sep 17 00:00:00 2001 From: Abhijeet Prasad Date: Mon, 24 Mar 2025 12:54:42 -0400 Subject: [PATCH] perf(core): Remove `addNonEnumerableProperty` from `packages/core/src/tracing/utils.ts` --- .../rollup-utils/plugins/bundlePlugins.mjs | 2 - packages/core/src/tracing/utils.ts | 18 +++----- packages/core/test/lib/tracing/utils.test.ts | 45 +++++++++++++++++++ 3 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 packages/core/test/lib/tracing/utils.test.ts diff --git a/dev-packages/rollup-utils/plugins/bundlePlugins.mjs b/dev-packages/rollup-utils/plugins/bundlePlugins.mjs index 9d6edd3157c0..087c51143795 100644 --- a/dev-packages/rollup-utils/plugins/bundlePlugins.mjs +++ b/dev-packages/rollup-utils/plugins/bundlePlugins.mjs @@ -130,8 +130,6 @@ export function makeTerserPlugin() { '_sentryRootSpan', '_sentryChildSpans', '_sentrySpan', - '_sentryScope', - '_sentryIsolationScope', // require-in-the-middle calls `Module._resolveFilename`. We cannot mangle this (AWS lambda layer bundle). '_resolveFilename', // Set on e.g. the shim feedbackIntegration to be able to detect it diff --git a/packages/core/src/tracing/utils.ts b/packages/core/src/tracing/utils.ts index 61e2dcce2bc1..c5ddd296e7e3 100644 --- a/packages/core/src/tracing/utils.ts +++ b/packages/core/src/tracing/utils.ts @@ -1,20 +1,14 @@ import type { Scope } from '../scope'; import type { Span } from '../types-hoist'; -import { addNonEnumerableProperty } from '../utils-hoist/object'; -const SCOPE_ON_START_SPAN_FIELD = '_sentryScope'; -const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope'; - -type SpanWithScopes = Span & { - [SCOPE_ON_START_SPAN_FIELD]?: Scope; - [ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope; -}; +const SPAN_TO_SCOPE_MAP = new WeakMap(); +const SPAN_TO_ISOLATION_SCOPE_MAP = new WeakMap(); /** Store the scope & isolation scope for a span, which can the be used when it is finished. */ export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void { if (span) { - addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope); - addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope); + SPAN_TO_SCOPE_MAP.set(span, scope); + SPAN_TO_ISOLATION_SCOPE_MAP.set(span, isolationScope); } } @@ -23,7 +17,7 @@ export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, is */ export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } { return { - scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD], - isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD], + scope: SPAN_TO_SCOPE_MAP.get(span), + isolationScope: SPAN_TO_ISOLATION_SCOPE_MAP.get(span), }; } diff --git a/packages/core/test/lib/tracing/utils.test.ts b/packages/core/test/lib/tracing/utils.test.ts new file mode 100644 index 000000000000..80e85b5fc232 --- /dev/null +++ b/packages/core/test/lib/tracing/utils.test.ts @@ -0,0 +1,45 @@ +import { describe, it, expect, beforeEach } from 'vitest'; +import { setCapturedScopesOnSpan, getCapturedScopesOnSpan } from '../../../src/tracing/utils'; +import type { Scope } from '../../../src/scope'; +import type { Span } from '../../../src/types-hoist'; + +describe('tracing utils', () => { + let mockSpan: Span; + let mockScope: Scope; + let mockIsolationScope: Scope; + + beforeEach(() => { + mockSpan = {} as Span; + mockScope = {} as Scope; + mockIsolationScope = {} as Scope; + }); + + describe('setCapturedScopesOnSpan', () => { + it('should store scope and isolation scope on span', () => { + setCapturedScopesOnSpan(mockSpan, mockScope, mockIsolationScope); + const captured = getCapturedScopesOnSpan(mockSpan); + expect(captured.scope).toBe(mockScope); + expect(captured.isolationScope).toBe(mockIsolationScope); + }); + + it('should handle undefined span', () => { + // Should not throw + setCapturedScopesOnSpan(undefined, mockScope, mockIsolationScope); + }); + }); + + describe('getCapturedScopesOnSpan', () => { + it('should return undefined scopes when no scopes were set', () => { + const captured = getCapturedScopesOnSpan(mockSpan); + expect(captured.scope).toBeUndefined(); + expect(captured.isolationScope).toBeUndefined(); + }); + + it('should return stored scopes', () => { + setCapturedScopesOnSpan(mockSpan, mockScope, mockIsolationScope); + const captured = getCapturedScopesOnSpan(mockSpan); + expect(captured.scope).toBe(mockScope); + expect(captured.isolationScope).toBe(mockIsolationScope); + }); + }); +});