Skip to content

Commit 31126f3

Browse files
committed
move to dedicated file
1 parent a0efb9b commit 31126f3

File tree

11 files changed

+64
-57
lines changed

11 files changed

+64
-57
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ export {
8181
spanToJSON,
8282
spanIsSampled,
8383
} from './utils/spanUtils';
84+
export { getRootSpan } from './utils/getRootSpan';
8485
export { DEFAULT_ENVIRONMENT } from './constants';
8586
export { ModuleMetadata } from './integrations/metadata';
8687
export { RequestData } from './integrations/requestdata';

packages/core/src/server-runtime-client.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ import {
2626
getDynamicSamplingContextFromClient,
2727
getDynamicSamplingContextFromSpan,
2828
} from './tracing';
29-
import { getRootSpan, spanToTraceContext } from './utils/spanUtils';
29+
import { getRootSpan } from './utils/getRootSpan';
30+
import { spanToTraceContext } from './utils/spanUtils';
3031

3132
export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
3233
platform?: string;

packages/core/src/tracing/dynamicSamplingContext.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { dropUndefinedKeys } from '@sentry/utils';
33

44
import { DEFAULT_ENVIRONMENT } from '../constants';
55
import { getClient, getCurrentScope } from '../exports';
6-
import { getRootSpan, spanIsSampled, spanToJSON } from '../utils/spanUtils';
6+
import { getRootSpan } from '../utils/getRootSpan';
7+
import { spanIsSampled, spanToJSON } from '../utils/spanUtils';
78

89
/**
910
* Creates a dynamic sampling context from a client.

packages/core/src/tracing/span.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ import type {
1616
import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1717

1818
import { DEBUG_BUILD } from '../debug-build';
19+
import { getRootSpan } from '../utils/getRootSpan';
1920
import {
2021
TRACE_FLAG_NONE,
2122
TRACE_FLAG_SAMPLED,
22-
getRootSpan,
2323
spanTimeInputToSeconds,
2424
spanToJSON,
2525
spanToTraceContext,

packages/core/src/utils/applyScopeDataToEvent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Breadcrumb, Event, PropagationContext, ScopeData, Span } from '@sentry/types';
22
import { arrayify } from '@sentry/utils';
33
import { getDynamicSamplingContextFromSpan } from '../tracing/dynamicSamplingContext';
4-
import { getRootSpan, spanToJSON, spanToTraceContext } from './spanUtils';
4+
import { getRootSpan } from './getRootSpan';
5+
import { spanToJSON, spanToTraceContext } from './spanUtils';
56

67
/**
78
* Applies data from the scope to the event and runs all event processors on it.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { Span } from '@sentry/types';
2+
3+
/**
4+
* Returns the root span of a given span.
5+
*
6+
* As long as we use `Transaction`s internally, the returned root span
7+
* will be a `Transaction` but be aware that this might change in the future.
8+
*
9+
* If the given span has no root span or transaction, `undefined` is returned.
10+
*/
11+
export function getRootSpan(span: Span): Span | undefined {
12+
// TODO (v8): Remove this check and just return span
13+
// eslint-disable-next-line deprecation/deprecation
14+
return span.transaction;
15+
}

packages/core/src/utils/spanUtils.ts

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,3 @@ 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-
// TODO (v8): Remove this check and just return span
121-
// eslint-disable-next-line deprecation/deprecation
122-
return span.transaction;
123-
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Span, Transaction, getRootSpan } from '../../../src';
2+
3+
describe('getRootSpan', () => {
4+
it('returns the root span of a span (Span)', () => {
5+
const root = new Span({ name: 'test' });
6+
// @ts-expect-error this is highly illegal and shouldn't happen IRL
7+
// eslint-disable-next-line deprecation/deprecation
8+
root.transaction = root;
9+
10+
// eslint-disable-next-line deprecation/deprecation
11+
const childSpan = root.startChild({ name: 'child' });
12+
expect(getRootSpan(childSpan)).toBe(root);
13+
});
14+
15+
it('returns the root span of a span (Transaction)', () => {
16+
// eslint-disable-next-line deprecation/deprecation
17+
const root = new Transaction({ name: 'test' });
18+
19+
// eslint-disable-next-line deprecation/deprecation
20+
const childSpan = root.startChild({ name: 'child' });
21+
expect(getRootSpan(childSpan)).toBe(root);
22+
});
23+
24+
it('returns the span itself if it is a root span', () => {
25+
// eslint-disable-next-line deprecation/deprecation
26+
const span = new Transaction({ name: 'test' });
27+
28+
expect(getRootSpan(span)).toBe(span);
29+
});
30+
31+
it('returns undefined if span has no root span', () => {
32+
const span = new Span({ name: 'test' });
33+
34+
expect(getRootSpan(span)).toBe(undefined);
35+
});
36+
});

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

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { TRACEPARENT_REGEXP, timestampInSeconds } from '@sentry/utils';
2-
import { Span, Transaction, spanToTraceHeader } from '../../../src';
3-
import { getRootSpan, spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../../../src/utils/spanUtils';
2+
import { Span, spanToTraceHeader } from '../../../src';
3+
import { spanIsSampled, spanTimeInputToSeconds, spanToJSON } from '../../../src/utils/spanUtils';
44

55
describe('spanToTraceHeader', () => {
66
test('simple', () => {
@@ -127,38 +127,3 @@ describe('spanIsSampled', () => {
127127
expect(spanIsSampled(span)).toBe(false);
128128
});
129129
});
130-
131-
describe('getRootSpan', () => {
132-
it('returns the root span of a span (Span)', () => {
133-
const root = new Span({ name: 'test' });
134-
// @ts-expect-error this is highly illegal and shouldn't happen IRL
135-
// eslint-disable-next-line deprecation/deprecation
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-
// eslint-disable-next-line deprecation/deprecation
145-
const root = new Transaction({ name: 'test' });
146-
147-
// eslint-disable-next-line deprecation/deprecation
148-
const childSpan = root.startChild({ name: 'child' });
149-
expect(getRootSpan(childSpan)).toBe(root);
150-
});
151-
152-
it('returns the span itself if it is a root span', () => {
153-
// eslint-disable-next-line deprecation/deprecation
154-
const span = new Transaction({ name: 'test' });
155-
156-
expect(getRootSpan(span)).toBe(span);
157-
});
158-
159-
it('returns undefined if span has no root span', () => {
160-
const span = new Span({ name: 'test' });
161-
162-
expect(getRootSpan(span)).toBe(undefined);
163-
});
164-
});

packages/tracing-internal/src/browser/request.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
stringMatchesSomePattern,
2222
} from '@sentry/utils';
2323

24+
import { getRootSpan } from '@sentry/core/build/types/utils/spanUtils';
2425
import { instrumentFetchRequest } from '../common/fetch';
2526
import { addPerformanceInstrumentationHandler } from './instrument';
2627

@@ -298,7 +299,7 @@ export function xhrCallback(
298299

299300
if (xhr.setRequestHeader && shouldAttachHeaders(sentryXhrData.url)) {
300301
if (span) {
301-
const transaction = span && span.transaction;
302+
const transaction = span && getRootSpan(span);
302303
const dynamicSamplingContext = transaction && getDynamicSamplingContextFromSpan(transaction);
303304
const sentryBaggageHeader = dynamicSamplingContextToSentryBaggageHeader(dynamicSamplingContext);
304305
setHeaderOnXhr(xhr, spanToTraceHeader(span), sentryBaggageHeader);

packages/tracing-internal/src/common/fetch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export function addTracingHeadersToFetchRequest(
134134
// eslint-disable-next-line deprecation/deprecation
135135
const span = requestSpan || scope.getSpan();
136136

137-
const transaction = span && span.transaction;
137+
const transaction = span && getRootSpan(span);
138138

139139
const { traceId, sampled, dsc } = scope.getPropagationContext();
140140

0 commit comments

Comments
 (0)