Skip to content

Commit b31203e

Browse files
committed
move files around
1 parent 464d725 commit b31203e

8 files changed

+80
-76
lines changed

packages/core/src/baseclient.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ import {
5050
} from '@sentry/utils';
5151

5252
import { getEnvelopeEndpointWithUrlEncodedAuth } from './api';
53-
import { getIsolationScope } from './currentScopes';
53+
import { getIsolationScope, getTraceContextFromScopes } from './currentScopes';
5454
import { DEBUG_BUILD } from './debug-build';
5555
import { createEventEnvelope, createSessionEnvelope } from './envelope';
5656
import type { IntegrationIndex } from './integration';
5757
import { afterSetupIntegrations } from './integration';
5858
import { setupIntegration, setupIntegrations } from './integration';
59-
import { getDynamicSamplingContextFromScopes, getTraceContextFromScopes } from './propagationContext';
6059
import type { Scope } from './scope';
6160
import { updateSession } from './session';
61+
import { getDynamicSamplingContextFromScopes } from './tracing/dynamicSamplingContext';
6262
import { parseSampleRate } from './utils/parseSampleRate';
6363
import { prepareEvent } from './utils/prepareEvent';
6464

packages/core/src/currentScopes.ts

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { Scope } from '@sentry/types';
1+
import type { PropagationContext, Scope, TraceContext } from '@sentry/types';
22
import type { Client } from '@sentry/types';
3-
import { getGlobalSingleton } from '@sentry/utils';
3+
import { dropUndefinedKeys, generateSentryTraceHeader, getGlobalSingleton } from '@sentry/utils';
44
import { getAsyncContextStrategy } from './asyncContext';
55
import { getMainCarrier } from './carrier';
66
import { Scope as ScopeClass } from './scope';
@@ -120,3 +120,49 @@ export function withIsolationScope<T>(
120120
export function getClient<C extends Client>(): C | undefined {
121121
return getCurrentScope().getClient<C>();
122122
}
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+
}

packages/core/src/index.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,12 @@ export {
3838
withScope,
3939
withIsolationScope,
4040
getClient,
41+
getTraceContextFromScopes,
4142
} from './currentScopes';
4243
export {
4344
getDefaultCurrentScope,
4445
getDefaultIsolationScope,
4546
} from './defaultScopes';
46-
export {
47-
getDynamicSamplingContextFromScopes,
48-
getTraceContextFromScopes,
49-
} from './propagationContext';
5047
export { setAsyncContextStrategy } from './asyncContext';
5148
export { getMainCarrier } from './carrier';
5249
export { makeSession, closeSession, updateSession } from './session';

packages/core/src/propagationContext.ts

-62
This file was deleted.

packages/core/src/server-runtime-client.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,15 @@ import { eventFromMessage, eventFromUnknownInput, logger, resolvedSyncPromise, u
1515

1616
import { BaseClient } from './baseclient';
1717
import { createCheckInEnvelope } from './checkin';
18-
import { getIsolationScope } from './currentScopes';
18+
import { getIsolationScope, getTraceContextFromScopes } from './currentScopes';
1919
import { DEBUG_BUILD } from './debug-build';
20-
import { getDynamicSamplingContextFromScopes, getTraceContextFromScopes } from './propagationContext';
2120
import type { Scope } from './scope';
2221
import { SessionFlusher } from './sessionflusher';
23-
import { getDynamicSamplingContextFromSpan, registerSpanErrorInstrumentation } from './tracing';
22+
import {
23+
getDynamicSamplingContextFromScopes,
24+
getDynamicSamplingContextFromSpan,
25+
registerSpanErrorInstrumentation,
26+
} from './tracing';
2427
import { _getSpanForScope } from './utils/spanOnScope';
2528
import { spanToTraceContext } from './utils/spanUtils';
2629

packages/core/src/tracing/dynamicSamplingContext.ts

+20-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ import {
77
} from '@sentry/utils';
88

99
import { DEFAULT_ENVIRONMENT } from '../constants';
10-
import { getClient } from '../currentScopes';
10+
import {
11+
getClient,
12+
getCurrentScope,
13+
getGlobalScope,
14+
getIsolationScope,
15+
mergePropagationContexts,
16+
} from '../currentScopes';
1117
import { SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE, SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '../semanticAttributes';
1218
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
1319
import { getRootSpan, spanIsSampled, spanToJSON } from '../utils/spanUtils';
@@ -52,6 +58,19 @@ export function getDynamicSamplingContextFromClient(trace_id: string, client: Cl
5258
return dsc;
5359
}
5460

61+
/**
62+
* Get the dynamic sampling context for the currently active scopes.
63+
*/
64+
export function getDynamicSamplingContextFromScopes(
65+
client: Client,
66+
scope = getCurrentScope(),
67+
isolationScope = getIsolationScope(),
68+
globalScope = getGlobalScope(),
69+
): Partial<DynamicSamplingContext> {
70+
const propagationContext = mergePropagationContexts(scope, isolationScope, globalScope);
71+
return propagationContext.dsc || getDynamicSamplingContextFromClient(propagationContext.traceId, client);
72+
}
73+
5574
/**
5675
* Creates a dynamic sampling context from a span (and client and scope)
5776
*

packages/core/src/tracing/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export {
2222
export {
2323
getDynamicSamplingContextFromClient,
2424
getDynamicSamplingContextFromSpan,
25+
getDynamicSamplingContextFromScopes,
2526
spanToBaggageHeader,
2627
} from './dynamicSamplingContext';
2728
export { setMeasurement, timedEventsToMeasurements } from './measurement';

packages/core/src/tracing/sentryHeaders.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { Client, Scope, Span } from '@sentry/types';
22
import { dynamicSamplingContextToSentryBaggageHeader } from '@sentry/utils';
3-
import { getDynamicSamplingContextFromScopes, scopesToTraceHeader } from '../propagationContext';
3+
import { scopesToTraceHeader } from '../currentScopes';
44
import { spanToTraceHeader } from '../utils/spanUtils';
5-
import { getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
5+
import { getDynamicSamplingContextFromScopes, getDynamicSamplingContextFromSpan } from './dynamicSamplingContext';
66

77
/**
88
* Get the sentry-trace and baggage headers for a given span or scope.

0 commit comments

Comments
 (0)