Skip to content

fix(opentelemetry): Avoid weakmap for storing data #11470

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/opentelemetry/src/utils/contextData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { Context } from '@opentelemetry/api';
import type { Scope } from '@sentry/types';
import { addNonEnumerableProperty } from '@sentry/utils';

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

const SCOPE_CONTEXT_MAP = new WeakMap<Scope, Context>();
const SCOPE_CONTEXT_FIELD = '_scopeContext';

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

/**
* Get the context related to a scope.
* TODO v8: Use this for the `trace` functions.
* */
export function getContextFromScope(scope: Scope): Context | undefined {
return SCOPE_CONTEXT_MAP.get(scope);
return (scope as { [SCOPE_CONTEXT_FIELD]?: Context })[SCOPE_CONTEXT_FIELD];
}
16 changes: 4 additions & 12 deletions packages/opentelemetry/src/utils/spanData.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import type { Scope } from '@sentry/types';
import { addNonEnumerableProperty } from '@sentry/utils';

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

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

/**
* Set the Sentry scope to be used for finishing a given OTEL span.
Expand All @@ -25,7 +17,7 @@ export function setSpanScopes(
isolationScope: Scope;
},
): void {
SpanScopes.set(span, scopes);
addNonEnumerableProperty(span, SPAN_SCOPES_FIELD, scopes);
}

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