-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathcontextData.ts
More file actions
38 lines (33 loc) · 1.36 KB
/
contextData.ts
File metadata and controls
38 lines (33 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import type { Context } from '@opentelemetry/api';
import type { Scope } from '@sentry/core';
import { addNonEnumerableProperty } from '@sentry/core';
import { SENTRY_SCOPES_CONTEXT_KEY } from '../constants';
import type { CurrentScopes } from '../types';
const SCOPE_CONTEXT_FIELD = '_scopeContext';
/**
* Try to get the current scopes from the given OTEL context.
* This requires a Context Manager that was wrapped with getWrappedContextManager.
*/
export function getScopesFromContext(context: Context): CurrentScopes | undefined {
return context.getValue(SENTRY_SCOPES_CONTEXT_KEY) as CurrentScopes | undefined;
}
/**
* Set the current scopes on an OTEL context.
* This will return a forked context with the Propagation Context set.
*/
export function setScopesOnContext(context: Context, scopes: CurrentScopes): Context {
return context.setValue(SENTRY_SCOPES_CONTEXT_KEY, scopes);
}
/**
* Set the context on the scope so we can later look it up.
* We need this to get the context from the scope in the `trace` functions.
*/
export function setContextOnScope(scope: Scope, context: Context): void {
addNonEnumerableProperty(scope, SCOPE_CONTEXT_FIELD, context);
}
/**
* Get the context related to a scope.
*/
export function getContextFromScope(scope: Scope): Context | undefined {
return (scope as { [SCOPE_CONTEXT_FIELD]?: Context })[SCOPE_CONTEXT_FIELD];
}