Skip to content

Commit 1ae3022

Browse files
committed
perf(core): Remove addNonEnumerableProperty from
`packages/core/src/tracing/utils.ts`
1 parent 38a499a commit 1ae3022

File tree

3 files changed

+51
-14
lines changed

3 files changed

+51
-14
lines changed

dev-packages/rollup-utils/plugins/bundlePlugins.mjs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,6 @@ export function makeTerserPlugin() {
130130
'_sentryRootSpan',
131131
'_sentryChildSpans',
132132
'_sentrySpan',
133-
'_sentryScope',
134-
'_sentryIsolationScope',
135133
// require-in-the-middle calls `Module._resolveFilename`. We cannot mangle this (AWS lambda layer bundle).
136134
'_resolveFilename',
137135
// Set on e.g. the shim feedbackIntegration to be able to detect it

packages/core/src/tracing/utils.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import type { Scope } from '../scope';
22
import type { Span } from '../types-hoist';
3-
import { addNonEnumerableProperty } from '../utils-hoist/object';
43

5-
const SCOPE_ON_START_SPAN_FIELD = '_sentryScope';
6-
const ISOLATION_SCOPE_ON_START_SPAN_FIELD = '_sentryIsolationScope';
7-
8-
type SpanWithScopes = Span & {
9-
[SCOPE_ON_START_SPAN_FIELD]?: Scope;
10-
[ISOLATION_SCOPE_ON_START_SPAN_FIELD]?: Scope;
11-
};
4+
const SPAN_TO_SCOPE_MAP = new WeakMap<Span, Scope>();
5+
const SPAN_TO_ISOLATION_SCOPE_MAP = new WeakMap<Span, Scope>();
126

137
/** Store the scope & isolation scope for a span, which can the be used when it is finished. */
148
export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, isolationScope: Scope): void {
159
if (span) {
16-
addNonEnumerableProperty(span, ISOLATION_SCOPE_ON_START_SPAN_FIELD, isolationScope);
17-
addNonEnumerableProperty(span, SCOPE_ON_START_SPAN_FIELD, scope);
10+
SPAN_TO_SCOPE_MAP.set(span, scope);
11+
SPAN_TO_ISOLATION_SCOPE_MAP.set(span, isolationScope);
1812
}
1913
}
2014

@@ -23,7 +17,7 @@ export function setCapturedScopesOnSpan(span: Span | undefined, scope: Scope, is
2317
*/
2418
export function getCapturedScopesOnSpan(span: Span): { scope?: Scope; isolationScope?: Scope } {
2519
return {
26-
scope: (span as SpanWithScopes)[SCOPE_ON_START_SPAN_FIELD],
27-
isolationScope: (span as SpanWithScopes)[ISOLATION_SCOPE_ON_START_SPAN_FIELD],
20+
scope: SPAN_TO_SCOPE_MAP.get(span),
21+
isolationScope: SPAN_TO_ISOLATION_SCOPE_MAP.get(span),
2822
};
2923
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, it, expect, beforeEach } from 'vitest';
2+
import { setCapturedScopesOnSpan, getCapturedScopesOnSpan } from '../../../src/tracing/utils';
3+
import type { Scope } from '../../../src/scope';
4+
import type { Span } from '../../../src/types-hoist';
5+
6+
describe('tracing utils', () => {
7+
let mockSpan: Span;
8+
let mockScope: Scope;
9+
let mockIsolationScope: Scope;
10+
11+
beforeEach(() => {
12+
mockSpan = {} as Span;
13+
mockScope = {} as Scope;
14+
mockIsolationScope = {} as Scope;
15+
});
16+
17+
describe('setCapturedScopesOnSpan', () => {
18+
it('should store scope and isolation scope on span', () => {
19+
setCapturedScopesOnSpan(mockSpan, mockScope, mockIsolationScope);
20+
const captured = getCapturedScopesOnSpan(mockSpan);
21+
expect(captured.scope).toBe(mockScope);
22+
expect(captured.isolationScope).toBe(mockIsolationScope);
23+
});
24+
25+
it('should handle undefined span', () => {
26+
// Should not throw
27+
setCapturedScopesOnSpan(undefined, mockScope, mockIsolationScope);
28+
});
29+
});
30+
31+
describe('getCapturedScopesOnSpan', () => {
32+
it('should return undefined scopes when no scopes were set', () => {
33+
const captured = getCapturedScopesOnSpan(mockSpan);
34+
expect(captured.scope).toBeUndefined();
35+
expect(captured.isolationScope).toBeUndefined();
36+
});
37+
38+
it('should return stored scopes', () => {
39+
setCapturedScopesOnSpan(mockSpan, mockScope, mockIsolationScope);
40+
const captured = getCapturedScopesOnSpan(mockSpan);
41+
expect(captured.scope).toBe(mockScope);
42+
expect(captured.isolationScope).toBe(mockIsolationScope);
43+
});
44+
});
45+
});

0 commit comments

Comments
 (0)