|
1 |
| -import type { Scope } from '@sentry/types'; |
| 1 | +import type { PropagationContext, Scope, TraceContext } from '@sentry/types'; |
2 | 2 | import type { Client } from '@sentry/types';
|
3 |
| -import { getGlobalSingleton } from '@sentry/utils'; |
| 3 | +import { dropUndefinedKeys, generateSentryTraceHeader, getGlobalSingleton } from '@sentry/utils'; |
4 | 4 | import { getAsyncContextStrategy } from './asyncContext';
|
5 | 5 | import { getMainCarrier } from './carrier';
|
6 | 6 | import { Scope as ScopeClass } from './scope';
|
@@ -120,3 +120,49 @@ export function withIsolationScope<T>(
|
120 | 120 | export function getClient<C extends Client>(): C | undefined {
|
121 | 121 | return getCurrentScope().getClient<C>();
|
122 | 122 | }
|
| 123 | + |
| 124 | +/** |
| 125 | + * Get a trace context for the currently active scopes. |
| 126 | + */ |
| 127 | +export function getTraceContextFromScopes( |
| 128 | + scope = getCurrentScope(), |
| 129 | + isolationScope = getIsolationScope(), |
| 130 | + globalScope = getGlobalScope(), |
| 131 | +): TraceContext { |
| 132 | + const propagationContext = mergePropagationContexts(scope, isolationScope, globalScope); |
| 133 | + |
| 134 | + const { traceId, spanId, parentSpanId } = propagationContext; |
| 135 | + |
| 136 | + const traceContext: TraceContext = dropUndefinedKeys({ |
| 137 | + trace_id: traceId, |
| 138 | + span_id: spanId, |
| 139 | + parent_span_id: parentSpanId, |
| 140 | + }); |
| 141 | + |
| 142 | + return traceContext; |
| 143 | +} |
| 144 | + |
| 145 | +/** |
| 146 | + * Get a sentry-trace header value for the currently active scopes. |
| 147 | + */ |
| 148 | +export function scopesToTraceHeader( |
| 149 | + scope = getCurrentScope(), |
| 150 | + isolationScope = getIsolationScope(), |
| 151 | + globalScope = getGlobalScope(), |
| 152 | +): string { |
| 153 | + const { traceId, sampled, spanId } = mergePropagationContexts(scope, isolationScope, globalScope); |
| 154 | + return generateSentryTraceHeader(traceId, spanId, sampled); |
| 155 | +} |
| 156 | + |
| 157 | +/** Get a merged propagationContext for the current scopes. */ |
| 158 | +export function mergePropagationContexts( |
| 159 | + scope = getCurrentScope(), |
| 160 | + isolationScope = getIsolationScope(), |
| 161 | + globalScope = getGlobalScope(), |
| 162 | +): PropagationContext { |
| 163 | + return { |
| 164 | + ...globalScope.getPropagationContext(), |
| 165 | + ...isolationScope.getPropagationContext(), |
| 166 | + ...scope.getPropagationContext(), |
| 167 | + }; |
| 168 | +} |
0 commit comments