Skip to content

Commit 07a2264

Browse files
author
Luca Forstner
authored
feat(core): Read propagation context off scopes in startSpan APIs (#10300)
1 parent ea85419 commit 07a2264

File tree

2 files changed

+103
-10
lines changed

2 files changed

+103
-10
lines changed

packages/core/src/tracing/trace.ts

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/ut
55
import { DEBUG_BUILD } from '../debug-build';
66
import { getCurrentScope, withScope } from '../exports';
77
import type { Hub } from '../hub';
8+
import { getIsolationScope } from '../hub';
89
import { getCurrentHub } from '../hub';
910
import { handleCallbackErrors } from '../utils/handleCallbackErrors';
1011
import { hasTracingEnabled } from '../utils/hasTracingEnabled';
@@ -172,11 +173,32 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined {
172173
? // eslint-disable-next-line deprecation/deprecation
173174
context.scope.getSpan()
174175
: getActiveSpan();
175-
return parentSpan
176-
? // eslint-disable-next-line deprecation/deprecation
177-
parentSpan.startChild(ctx)
178-
: // eslint-disable-next-line deprecation/deprecation
179-
hub.startTransaction(ctx);
176+
177+
if (parentSpan) {
178+
// eslint-disable-next-line deprecation/deprecation
179+
return parentSpan.startChild(ctx);
180+
} else {
181+
const isolationScope = getIsolationScope();
182+
const scope = getCurrentScope();
183+
184+
const { traceId, dsc, parentSpanId, sampled } = {
185+
...isolationScope.getPropagationContext(),
186+
...scope.getPropagationContext(),
187+
};
188+
189+
// eslint-disable-next-line deprecation/deprecation
190+
return hub.startTransaction({
191+
traceId,
192+
parentSpanId,
193+
parentSampled: sampled,
194+
...ctx,
195+
metadata: {
196+
dynamicSamplingContext: dsc,
197+
// eslint-disable-next-line deprecation/deprecation
198+
...ctx.metadata,
199+
},
200+
});
201+
}
180202
}
181203

182204
/**
@@ -256,11 +278,32 @@ function createChildSpanOrTransaction(
256278
if (!hasTracingEnabled()) {
257279
return undefined;
258280
}
259-
return parentSpan
260-
? // eslint-disable-next-line deprecation/deprecation
261-
parentSpan.startChild(ctx)
262-
: // eslint-disable-next-line deprecation/deprecation
263-
hub.startTransaction(ctx);
281+
282+
if (parentSpan) {
283+
// eslint-disable-next-line deprecation/deprecation
284+
return parentSpan.startChild(ctx);
285+
} else {
286+
const isolationScope = getIsolationScope();
287+
const scope = getCurrentScope();
288+
289+
const { traceId, dsc, parentSpanId, sampled } = {
290+
...isolationScope.getPropagationContext(),
291+
...scope.getPropagationContext(),
292+
};
293+
294+
// eslint-disable-next-line deprecation/deprecation
295+
return hub.startTransaction({
296+
traceId,
297+
parentSpanId,
298+
parentSampled: sampled,
299+
...ctx,
300+
metadata: {
301+
dynamicSamplingContext: dsc,
302+
// eslint-disable-next-line deprecation/deprecation
303+
...ctx.metadata,
304+
},
305+
});
306+
}
264307
}
265308

266309
/**

packages/core/test/lib/tracing/trace.test.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getCurrentScope,
66
makeMain,
77
spanToJSON,
8+
withScope,
89
} from '../../../src';
910
import { Scope } from '../../../src/scope';
1011
import {
@@ -318,6 +319,22 @@ describe('startSpan', () => {
318319
expect(getCurrentScope()).toBe(initialScope);
319320
expect(getActiveSpan()).toBe(undefined);
320321
});
322+
323+
it("picks up the trace id off the parent scope's propagation context", () => {
324+
expect.assertions(1);
325+
withScope(scope => {
326+
scope.setPropagationContext({
327+
traceId: '99999999999999999999999999999999',
328+
spanId: '1212121212121212',
329+
dsc: {},
330+
parentSpanId: '4242424242424242',
331+
});
332+
333+
startSpan({ name: 'span' }, span => {
334+
expect(span?.spanContext().traceId).toBe('99999999999999999999999999999999');
335+
});
336+
});
337+
});
321338
});
322339

323340
describe('startSpanManual', () => {
@@ -381,6 +398,23 @@ describe('startSpanManual', () => {
381398

382399
expect(start).toEqual(1234);
383400
});
401+
402+
it("picks up the trace id off the parent scope's propagation context", () => {
403+
expect.assertions(1);
404+
withScope(scope => {
405+
scope.setPropagationContext({
406+
traceId: '99999999999999999999999999999991',
407+
spanId: '1212121212121212',
408+
dsc: {},
409+
parentSpanId: '4242424242424242',
410+
});
411+
412+
startSpanManual({ name: 'span' }, span => {
413+
expect(span?.spanContext().traceId).toBe('99999999999999999999999999999991');
414+
span?.end();
415+
});
416+
});
417+
});
384418
});
385419

386420
describe('startInactiveSpan', () => {
@@ -429,6 +463,22 @@ describe('startInactiveSpan', () => {
429463
const span = startInactiveSpan({ name: 'outer', startTime: [1234, 0] });
430464
expect(spanToJSON(span!).start_timestamp).toEqual(1234);
431465
});
466+
467+
it("picks up the trace id off the parent scope's propagation context", () => {
468+
expect.assertions(1);
469+
withScope(scope => {
470+
scope.setPropagationContext({
471+
traceId: '99999999999999999999999999999991',
472+
spanId: '1212121212121212',
473+
dsc: {},
474+
parentSpanId: '4242424242424242',
475+
});
476+
477+
const span = startInactiveSpan({ name: 'span' });
478+
expect(span?.spanContext().traceId).toBe('99999999999999999999999999999991');
479+
span?.end();
480+
});
481+
});
432482
});
433483

434484
describe('continueTrace', () => {

0 commit comments

Comments
 (0)