Skip to content

Commit f284116

Browse files
authored
fix(opentelemetry): Avoid weakmap for storing data (#11470)
It may happen that we have different modules, different versions etc. so the weakmaps may not be in sync. Just storing a non-enumerable property should be the safer choice here. Partially required for next.js 13.4.19 to work....
1 parent ab23658 commit f284116

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

packages/opentelemetry/src/utils/contextData.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { Context } from '@opentelemetry/api';
22
import type { Scope } from '@sentry/types';
3+
import { addNonEnumerableProperty } from '@sentry/utils';
34

45
import { SENTRY_SCOPES_CONTEXT_KEY } from '../constants';
56
import type { CurrentScopes } from '../types';
67

7-
const SCOPE_CONTEXT_MAP = new WeakMap<Scope, Context>();
8+
const SCOPE_CONTEXT_FIELD = '_scopeContext';
89

910
/**
1011
* Try to get the current scopes from the given OTEL context.
@@ -27,13 +28,13 @@ export function setScopesOnContext(context: Context, scopes: CurrentScopes): Con
2728
* We need this to get the context from the scope in the `trace` functions.
2829
*/
2930
export function setContextOnScope(scope: Scope, context: Context): void {
30-
SCOPE_CONTEXT_MAP.set(scope, context);
31+
addNonEnumerableProperty(scope, SCOPE_CONTEXT_FIELD, context);
3132
}
3233

3334
/**
3435
* Get the context related to a scope.
3536
* TODO v8: Use this for the `trace` functions.
3637
* */
3738
export function getContextFromScope(scope: Scope): Context | undefined {
38-
return SCOPE_CONTEXT_MAP.get(scope);
39+
return (scope as { [SCOPE_CONTEXT_FIELD]?: Context })[SCOPE_CONTEXT_FIELD];
3940
}
Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
import type { Scope } from '@sentry/types';
2+
import { addNonEnumerableProperty } from '@sentry/utils';
23

34
import type { AbstractSpan } from '../types';
45

5-
// We store the parent span, scopes & metadata in separate weakmaps, so we can access them for a given span
6-
// This way we can enhance the data that an OTEL Span natively gives us
7-
// and since we are using weakmaps, we do not need to clean up after ourselves
8-
const SpanScopes = new WeakMap<
9-
AbstractSpan,
10-
{
11-
scope: Scope;
12-
isolationScope: Scope;
13-
}
14-
>();
6+
const SPAN_SCOPES_FIELD = '_spanScopes';
157

168
/**
179
* Set the Sentry scope to be used for finishing a given OTEL span.
@@ -25,7 +17,7 @@ export function setSpanScopes(
2517
isolationScope: Scope;
2618
},
2719
): void {
28-
SpanScopes.set(span, scopes);
20+
addNonEnumerableProperty(span, SPAN_SCOPES_FIELD, scopes);
2921
}
3022

3123
/** Get the Sentry scopes to use for finishing an OTEL span. */
@@ -35,5 +27,5 @@ export function getSpanScopes(span: AbstractSpan):
3527
isolationScope: Scope;
3628
}
3729
| undefined {
38-
return SpanScopes.get(span);
30+
return (span as { [SPAN_SCOPES_FIELD]?: { scope: Scope; isolationScope: Scope } })[SPAN_SCOPES_FIELD];
3931
}

0 commit comments

Comments
 (0)