Skip to content

Commit 61be37b

Browse files
committed
wip
1 parent 307ec28 commit 61be37b

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed

packages/core/src/tracing/span.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ export class Span implements SpanInterface {
105105

106106
/**
107107
* @inheritDoc
108+
* @deprecated Use top level `Sentry.getRootSpan()` instead
108109
*/
109110
public transaction?: Transaction;
110111

packages/core/src/utils/spanUtils.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,15 @@ export function spanIsSampled(span: Span): boolean {
107107
// eslint-disable-next-line no-bitwise
108108
return Boolean(traceFlags & TRACE_FLAG_SAMPLED);
109109
}
110+
111+
/**
112+
* Returns the root span of a given span.
113+
*
114+
* As long as we use `Transaction`s internally, the returned root span
115+
* will be a `Transaction` but be aware that this might change in the future.
116+
*
117+
* If the given span has no root span or transaction, `undefined` is returned.
118+
*/
119+
export function getRootSpan(span: Span): Span | undefined {
120+
return span.transaction;
121+
}

packages/core/test/lib/utils/spanUtils.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TRACEPARENT_REGEXP, timestampInSeconds } from '@sentry/utils';
2+
import { Transaction } from '../../../build/types';
23
import { Span, spanToTraceHeader } from '../../../src';
3-
import { spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../../../src/utils/spanUtils';
4+
import { getRootSpan, spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../../../src/utils/spanUtils';
45

56
describe('spanToTraceHeader', () => {
67
test('simple', () => {
@@ -127,3 +128,35 @@ describe('spanIsSampled', () => {
127128
expect(spanIsSampled(span)).toBe(false);
128129
});
129130
});
131+
132+
describe('getRootSpan', () => {
133+
it('returns the root span of a span (Span)', () => {
134+
const root = new Span({ name: 'test' });
135+
// @ts-expect-error this is highly illegal and shouldn't happen IRL
136+
root.transaction = root;
137+
138+
// eslint-disable-next-line deprecation/deprecation
139+
const childSpan = root.startChild({ name: 'child' });
140+
expect(getRootSpan(childSpan)).toBe(root);
141+
});
142+
143+
it('returns the root span of a span (Transaction)', () => {
144+
const root = new Transaction({ name: 'test' });
145+
146+
// eslint-disable-next-line deprecation/deprecation
147+
const childSpan = root.startChild({ name: 'child' });
148+
expect(getRootSpan(childSpan)).toBe(root);
149+
});
150+
151+
it('returns the span itself if it is a root span', () => {
152+
const span = new Transaction({ name: 'test' });
153+
154+
expect(getRootSpan(span)).toBe(span);
155+
});
156+
157+
it('returns undefined if span has no root span', () => {
158+
const span = new Span({ name: 'test' });
159+
160+
expect(getRootSpan(span)).toBe(undefined);
161+
});
162+
});

packages/types/src/span.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ export interface Span extends SpanContext {
216216

217217
/**
218218
* The transaction containing this span
219+
* @deprecated Use top level `Sentry.getRootSpan()` instead
219220
*/
220221
transaction?: Transaction;
221222

0 commit comments

Comments
 (0)