diff --git a/packages/core/src/tracing/trace.ts b/packages/core/src/tracing/trace.ts index ad1b95345ef9..bdeba55ed9e4 100644 --- a/packages/core/src/tracing/trace.ts +++ b/packages/core/src/tracing/trace.ts @@ -5,6 +5,7 @@ import { dropUndefinedKeys, logger, tracingContextFromHeaders } from '@sentry/ut import { DEBUG_BUILD } from '../debug-build'; import { getCurrentScope, withScope } from '../exports'; import type { Hub } from '../hub'; +import { getIsolationScope } from '../hub'; import { getCurrentHub } from '../hub'; import { handleCallbackErrors } from '../utils/handleCallbackErrors'; import { hasTracingEnabled } from '../utils/hasTracingEnabled'; @@ -172,11 +173,32 @@ export function startInactiveSpan(context: StartSpanOptions): Span | undefined { ? // eslint-disable-next-line deprecation/deprecation context.scope.getSpan() : getActiveSpan(); - return parentSpan - ? // eslint-disable-next-line deprecation/deprecation - parentSpan.startChild(ctx) - : // eslint-disable-next-line deprecation/deprecation - hub.startTransaction(ctx); + + if (parentSpan) { + // eslint-disable-next-line deprecation/deprecation + return parentSpan.startChild(ctx); + } else { + const isolationScope = getIsolationScope(); + const scope = getCurrentScope(); + + const { traceId, dsc, parentSpanId, sampled } = { + ...isolationScope.getPropagationContext(), + ...scope.getPropagationContext(), + }; + + // eslint-disable-next-line deprecation/deprecation + return hub.startTransaction({ + traceId, + parentSpanId, + parentSampled: sampled, + ...ctx, + metadata: { + dynamicSamplingContext: dsc, + // eslint-disable-next-line deprecation/deprecation + ...ctx.metadata, + }, + }); + } } /** @@ -256,11 +278,32 @@ function createChildSpanOrTransaction( if (!hasTracingEnabled()) { return undefined; } - return parentSpan - ? // eslint-disable-next-line deprecation/deprecation - parentSpan.startChild(ctx) - : // eslint-disable-next-line deprecation/deprecation - hub.startTransaction(ctx); + + if (parentSpan) { + // eslint-disable-next-line deprecation/deprecation + return parentSpan.startChild(ctx); + } else { + const isolationScope = getIsolationScope(); + const scope = getCurrentScope(); + + const { traceId, dsc, parentSpanId, sampled } = { + ...isolationScope.getPropagationContext(), + ...scope.getPropagationContext(), + }; + + // eslint-disable-next-line deprecation/deprecation + return hub.startTransaction({ + traceId, + parentSpanId, + parentSampled: sampled, + ...ctx, + metadata: { + dynamicSamplingContext: dsc, + // eslint-disable-next-line deprecation/deprecation + ...ctx.metadata, + }, + }); + } } /** diff --git a/packages/core/test/lib/tracing/trace.test.ts b/packages/core/test/lib/tracing/trace.test.ts index f2103891b4f5..6bca44c6b088 100644 --- a/packages/core/test/lib/tracing/trace.test.ts +++ b/packages/core/test/lib/tracing/trace.test.ts @@ -5,6 +5,7 @@ import { getCurrentScope, makeMain, spanToJSON, + withScope, } from '../../../src'; import { Scope } from '../../../src/scope'; import { @@ -318,6 +319,22 @@ describe('startSpan', () => { expect(getCurrentScope()).toBe(initialScope); expect(getActiveSpan()).toBe(undefined); }); + + it("picks up the trace id off the parent scope's propagation context", () => { + expect.assertions(1); + withScope(scope => { + scope.setPropagationContext({ + traceId: '99999999999999999999999999999999', + spanId: '1212121212121212', + dsc: {}, + parentSpanId: '4242424242424242', + }); + + startSpan({ name: 'span' }, span => { + expect(span?.spanContext().traceId).toBe('99999999999999999999999999999999'); + }); + }); + }); }); describe('startSpanManual', () => { @@ -381,6 +398,23 @@ describe('startSpanManual', () => { expect(start).toEqual(1234); }); + + it("picks up the trace id off the parent scope's propagation context", () => { + expect.assertions(1); + withScope(scope => { + scope.setPropagationContext({ + traceId: '99999999999999999999999999999991', + spanId: '1212121212121212', + dsc: {}, + parentSpanId: '4242424242424242', + }); + + startSpanManual({ name: 'span' }, span => { + expect(span?.spanContext().traceId).toBe('99999999999999999999999999999991'); + span?.end(); + }); + }); + }); }); describe('startInactiveSpan', () => { @@ -429,6 +463,22 @@ describe('startInactiveSpan', () => { const span = startInactiveSpan({ name: 'outer', startTime: [1234, 0] }); expect(spanToJSON(span!).start_timestamp).toEqual(1234); }); + + it("picks up the trace id off the parent scope's propagation context", () => { + expect.assertions(1); + withScope(scope => { + scope.setPropagationContext({ + traceId: '99999999999999999999999999999991', + spanId: '1212121212121212', + dsc: {}, + parentSpanId: '4242424242424242', + }); + + const span = startInactiveSpan({ name: 'span' }); + expect(span?.spanContext().traceId).toBe('99999999999999999999999999999991'); + span?.end(); + }); + }); }); describe('continueTrace', () => {