Skip to content
63 changes: 53 additions & 10 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
},
});
}
}

/**
Expand Down Expand Up @@ -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,
},
});
}
}

/**
Expand Down
50 changes: 50 additions & 0 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
getCurrentScope,
makeMain,
spanToJSON,
withScope,
} from '../../../src';
import { Scope } from '../../../src/scope';
import {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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', () => {
Expand Down