Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions packages/core/src/tracing/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export function startSpan<T>(options: StartSpanOptions, callback: (span: Span) =

return wrapper(() => {
const scope = getCurrentScope();
const parentSpan = getParentSpan(scope);
const parentSpan = getParentSpan(scope, customParentSpan);

const shouldSkipSpan = options.onlyIfParent && !parentSpan;
const activeSpan = shouldSkipSpan
Expand Down Expand Up @@ -116,7 +116,7 @@ export function startSpanManual<T>(options: StartSpanOptions, callback: (span: S

return wrapper(() => {
const scope = getCurrentScope();
const parentSpan = getParentSpan(scope);
const parentSpan = getParentSpan(scope, customParentSpan);

const shouldSkipSpan = options.onlyIfParent && !parentSpan;
const activeSpan = shouldSkipSpan
Expand Down Expand Up @@ -176,7 +176,7 @@ export function startInactiveSpan(options: StartSpanOptions): Span {

return wrapper(() => {
const scope = getCurrentScope();
const parentSpan = getParentSpan(scope);
const parentSpan = getParentSpan(scope, customParentSpan);

const shouldSkipSpan = options.onlyIfParent && !parentSpan;

Expand Down Expand Up @@ -489,7 +489,17 @@ function _startChildSpan(parentSpan: Span, scope: Scope, spanArguments: SentrySp
return childSpan;
}

function getParentSpan(scope: Scope): SentrySpan | undefined {
function getParentSpan(scope: Scope, customParentSpan: Span | null | undefined): SentrySpan | undefined {
// always use the passed in span directly
if (customParentSpan) {
return customParentSpan as SentrySpan;
}

// This is different from `undefined` as it means the user explicitly wants no parent span
if (customParentSpan === null) {
return undefined;
}

const span = _getSpanForScope(scope) as SentrySpan | undefined;

if (!span) {
Expand Down
116 changes: 116 additions & 0 deletions packages/core/test/lib/tracing/trace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,44 @@ describe('startSpan', () => {
});
});
});

it('explicit parentSpan takes precedence over parentSpanIsAlwaysRootSpan=true', () => {
const options = getDefaultTestClientOptions({
tracesSampleRate: 1,
parentSpanIsAlwaysRootSpan: true,
});
client = new TestClient(options);
setCurrentClient(client);
client.init();

const parentSpan = startInactiveSpan({ name: 'parent span' });

startSpan({ name: 'parent span' }, () => {
startSpan({ name: 'child span' }, () => {
startSpan({ name: 'grand child span', parentSpan }, grandChildSpan => {
expect(spanToJSON(grandChildSpan).parent_span_id).toBe(parentSpan.spanContext().spanId);
});
});
});
});

it('explicit parentSpan=null takes precedence over parentSpanIsAlwaysRootSpan=true', () => {
const options = getDefaultTestClientOptions({
tracesSampleRate: 1,
parentSpanIsAlwaysRootSpan: true,
});
client = new TestClient(options);
setCurrentClient(client);
client.init();

startSpan({ name: 'parent span' }, () => {
startSpan({ name: 'child span' }, () => {
startSpan({ name: 'grand child span', parentSpan: null }, grandChildSpan => {
expect(spanToJSON(grandChildSpan).parent_span_id).toBe(undefined);
});
});
});
});
});

it('samples with a tracesSampler', () => {
Expand Down Expand Up @@ -1174,6 +1212,46 @@ describe('startSpanManual', () => {
span.end();
});
});

it('explicit parentSpan takes precedence over parentSpanIsAlwaysRootSpan=true', () => {
const options = getDefaultTestClientOptions({
tracesSampleRate: 1,
parentSpanIsAlwaysRootSpan: true,
});
client = new TestClient(options);
setCurrentClient(client);
client.init();

const parentSpan = startInactiveSpan({ name: 'parent span' });

startSpan({ name: 'parent span' }, () => {
startSpan({ name: 'child span' }, () => {
startSpanManual({ name: 'grand child span', parentSpan }, grandChildSpan => {
expect(spanToJSON(grandChildSpan).parent_span_id).toBe(parentSpan.spanContext().spanId);
grandChildSpan.end();
});
});
});
});

it('explicit parentSpan=null takes precedence over parentSpanIsAlwaysRootSpan=true', () => {
const options = getDefaultTestClientOptions({
tracesSampleRate: 1,
parentSpanIsAlwaysRootSpan: true,
});
client = new TestClient(options);
setCurrentClient(client);
client.init();

startSpan({ name: 'parent span' }, () => {
startSpan({ name: 'child span' }, () => {
startSpanManual({ name: 'grand child span', parentSpan: null }, grandChildSpan => {
expect(spanToJSON(grandChildSpan).parent_span_id).toBe(undefined);
grandChildSpan.end();
});
});
});
});
});

it('sets a child span reference on the parent span', () => {
Expand Down Expand Up @@ -1543,6 +1621,44 @@ describe('startInactiveSpan', () => {
});
});
});

it('explicit parentSpan takes precedence over parentSpanIsAlwaysRootSpan=true', () => {
const options = getDefaultTestClientOptions({
tracesSampleRate: 1,
parentSpanIsAlwaysRootSpan: true,
});
client = new TestClient(options);
setCurrentClient(client);
client.init();

const parentSpan = startInactiveSpan({ name: 'parent span' });

startSpan({ name: 'parent span' }, () => {
startSpan({ name: 'child span' }, () => {
const grandChildSpan = startInactiveSpan({ name: 'grand child span', parentSpan });
expect(spanToJSON(grandChildSpan).parent_span_id).toBe(parentSpan.spanContext().spanId);
grandChildSpan.end();
});
});
});

it('explicit parentSpan=null takes precedence over parentSpanIsAlwaysRootSpan=true', () => {
const options = getDefaultTestClientOptions({
tracesSampleRate: 1,
parentSpanIsAlwaysRootSpan: true,
});
client = new TestClient(options);
setCurrentClient(client);
client.init();

startSpan({ name: 'parent span' }, () => {
startSpan({ name: 'child span' }, () => {
const grandChildSpan = startInactiveSpan({ name: 'grand child span', parentSpan: null });
expect(spanToJSON(grandChildSpan).parent_span_id).toBe(undefined);
grandChildSpan.end();
});
});
});
});

it('includes the scope at the time the span was started when finished', async () => {
Expand Down
Loading