Skip to content

Commit 7aadd1c

Browse files
committed
fixes...
1 parent a7fc486 commit 7aadd1c

File tree

8 files changed

+30
-12
lines changed

8 files changed

+30
-12
lines changed

packages/core/src/currentScopes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ export function getClient<C extends Client>(): C | undefined {
128128
export function getTraceContextFromScope(scope: Scope): TraceContext {
129129
const propagationContext = scope.getPropagationContext();
130130

131-
const { traceId, parentSpanId } = propagationContext;
131+
const { traceId, parentSpanId, propagationSpanId } = propagationContext;
132132

133133
const traceContext: TraceContext = dropUndefinedKeys({
134134
trace_id: traceId,
135-
span_id: generateSpanId(),
135+
span_id: propagationSpanId || generateSpanId(),
136136
parent_span_id: parentSpanId,
137137
});
138138

packages/core/src/types-hoist/tracing.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,29 @@ export interface PropagationContext {
1515
* Either represents the incoming `traceId` or the `traceId` generated by the current SDK, if there was no incoming trace.
1616
*/
1717
traceId: string;
18+
1819
/**
1920
* Represents the sampling decision of the incoming trace.
2021
*
2122
* The current SDK should not modify this value!
2223
*/
2324
sampled?: boolean;
25+
2426
/**
2527
* The `parentSpanId` denotes the ID of the incoming client span. If there is no `parentSpanId` on the propagation
2628
* context, it means that the the incoming trace didn't come from a span.
2729
*
2830
* The current SDK should not modify this value!
2931
*/
3032
parentSpanId?: string;
33+
34+
/**
35+
* A span ID to be used when using tracing without performance (without an active span).
36+
* This is only set/used when the SDK wants to ensure to use the same span ID for propagation.
37+
* If this is empty, a random span ID will be generated.
38+
*/
39+
propagationSpanId?: string;
40+
3141
/**
3242
* An undefined dsc in the propagation context means that the current SDK invocation is the head of trace and still free to modify and set the DSC for outgoing requests.
3343
*

packages/core/src/utils-hoist/tracing.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ export function propagationContextFromHeaders(
7272
* Create sentry-trace header from span context values.
7373
*/
7474
export function generateSentryTraceHeader(
75-
traceId: string = generateTraceId(),
76-
spanId: string = generateSpanId(),
75+
traceId: string | undefined = generateTraceId(),
76+
spanId: string | undefined = generateSpanId(),
7777
sampled?: boolean,
7878
): string {
7979
let sampledString = '';

packages/core/src/utils/spanUtils.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
88
SEMANTIC_ATTRIBUTE_SENTRY_SOURCE,
99
} from '../semanticAttributes';
10+
import { getCapturedScopesOnSpan } from '../tracing';
1011
import type { SentrySpan } from '../tracing/sentrySpan';
1112
import { SPAN_STATUS_OK, SPAN_STATUS_UNSET } from '../tracing/spanstatus';
1213
import type {
@@ -61,7 +62,9 @@ export function spanToTraceContext(span: Span): TraceContext {
6162
// If the span is remote, we use a random/virtual span as span_id to the trace context,
6263
// and the remote span as parent_span_id
6364
const parent_span_id = isRemote ? spanId : spanToJSON(span).parent_span_id;
64-
const span_id = isRemote ? generateSpanId() : spanId;
65+
const scope = getCapturedScopesOnSpan(span).scope;
66+
67+
const span_id = isRemote ? scope?.getPropagationContext().propagationSpanId || generateSpanId() : spanId;
6568

6669
return dropUndefinedKeys({
6770
parent_span_id,

packages/core/src/utils/traceData.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { isEnabled } from '../exports';
55
import type { Scope } from '../scope';
66
import { getDynamicSamplingContextFromScope, getDynamicSamplingContextFromSpan } from '../tracing';
77
import type { SerializedTraceData, Span } from '../types-hoist';
8-
import { generateSpanId } from '../utils-hoist';
98
import { dynamicSamplingContextToSentryBaggageHeader } from '../utils-hoist/baggage';
109
import { logger } from '../utils-hoist/logger';
1110
import { TRACEPARENT_REGEXP, generateSentryTraceHeader } from '../utils-hoist/tracing';
@@ -56,6 +55,6 @@ export function getTraceData(options: { span?: Span } = {}): SerializedTraceData
5655
* Get a sentry-trace header value for the given scope.
5756
*/
5857
function scopeToTraceHeader(scope: Scope): string {
59-
const { traceId, sampled } = scope.getPropagationContext();
60-
return generateSentryTraceHeader(traceId, generateSpanId(), sampled);
58+
const { traceId, sampled, propagationSpanId } = scope.getPropagationContext();
59+
return generateSentryTraceHeader(traceId, propagationSpanId, sampled);
6160
}

packages/node/src/integrations/anr/worker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,11 @@ function applyScopeToEvent(event: Event, scope: ScopeData): void {
127127
applyScopeDataToEvent(event, scope);
128128

129129
if (!event.contexts?.trace) {
130-
const { traceId, parentSpanId } = scope.propagationContext;
130+
const { traceId, parentSpanId, propagationSpanId } = scope.propagationContext;
131131
event.contexts = {
132132
trace: {
133133
trace_id: traceId,
134-
span_id: generateSpanId(),
134+
span_id: propagationSpanId || generateSpanId(),
135135
parent_span_id: parentSpanId,
136136
},
137137
...event.contexts,

packages/node/src/integrations/http/SentryHttpInstrumentation.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opent
99
import type { AggregationCounts, Client, RequestEventData, SanitizedRequestData, Scope } from '@sentry/core';
1010
import {
1111
addBreadcrumb,
12+
generateSpanId,
1213
getBreadcrumbLogLevelFromHttpStatusCode,
1314
getClient,
1415
getIsolationScope,
@@ -18,6 +19,7 @@ import {
1819
parseUrl,
1920
stripUrlQueryAndFragment,
2021
withIsolationScope,
22+
withScope,
2123
} from '@sentry/core';
2224
import { DEBUG_BUILD } from '../../debug-build';
2325
import { getRequestUrl } from '../../utils/getRequestUrl';
@@ -187,7 +189,11 @@ export class SentryHttpInstrumentation extends InstrumentationBase<SentryHttpIns
187189
}
188190

189191
return withIsolationScope(isolationScope, () => {
190-
return original.apply(this, [event, ...args]);
192+
return withScope(scope => {
193+
// Set a new propagationSpanId for this request
194+
scope.getPropagationContext().propagationSpanId = generateSpanId();
195+
return original.apply(this, [event, ...args]);
196+
});
191197
});
192198
};
193199
};

packages/opentelemetry/src/propagator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export function getInjectionData(context: Context): {
233233
return {
234234
dynamicSamplingContext,
235235
traceId: propagationContext.traceId,
236-
spanId: undefined,
236+
spanId: propagationContext.propagationSpanId,
237237
sampled: propagationContext.sampled,
238238
};
239239
}

0 commit comments

Comments
 (0)