Skip to content

feat(core): Read propagation context off scopes in startSpan APIs #10300

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jan 30, 2024
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