Skip to content

Commit 848e6ad

Browse files
authored
feat(core): Deprecate span.getTraceContext() (#10032)
Instead, you can use a new `spanToTraceContext(span)` util function.
1 parent 38eb964 commit 848e6ad

File tree

8 files changed

+42
-33
lines changed

8 files changed

+42
-33
lines changed

MIGRATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ In v8, the Span class is heavily reworked. The following properties & methods ar
1616
* `span.updateWithContext(newSpanContext)`: Update the fields directly instead.
1717
* `span.setName(newName)`: Use `span.updateName(newName)` instead.
1818
* `span.toTraceparent()`: use `spanToTraceHeader(span)` util instead.
19+
* `span.getTraceContext()`: Use `spanToTraceContext(span)` utility function instead.
1920

2021
## Deprecate `pushScope` & `popScope` in favor of `withScope`
2122

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { MetricsAggregator } from './metrics/aggregator';
2121
import type { Scope } from './scope';
2222
import { SessionFlusher } from './sessionflusher';
2323
import { addTracingExtensions, getDynamicSamplingContextFromClient } from './tracing';
24+
import { spanToTraceContext } from './utils/spanUtils';
2425

2526
export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
2627
platform?: string;
@@ -256,7 +257,7 @@ export class ServerRuntimeClient<
256257
const span = scope.getSpan();
257258
if (span) {
258259
const samplingContext = span.transaction ? span.transaction.getDynamicSamplingContext() : undefined;
259-
return [samplingContext, span.getTraceContext()];
260+
return [samplingContext, spanToTraceContext(span)];
260261
}
261262

262263
const { traceId, spanId, parentSpanId, dsc } = scope.getPropagationContext();

packages/core/src/tracing/span.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1414

1515
import { DEBUG_BUILD } from '../debug-build';
16-
import { spanToTraceHeader } from '../utils/spanUtils';
16+
import { spanToTraceContext, spanToTraceHeader } from '../utils/spanUtils';
1717
import { ensureTimestampInSeconds } from './utils';
1818

1919
/**
@@ -366,17 +366,7 @@ export class Span implements SpanInterface {
366366
* @inheritDoc
367367
*/
368368
public getTraceContext(): TraceContext {
369-
return dropUndefinedKeys({
370-
data: this._getData(),
371-
description: this.description,
372-
op: this.op,
373-
parent_span_id: this.parentSpanId,
374-
span_id: this.spanId,
375-
status: this.status,
376-
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
377-
trace_id: this.traceId,
378-
origin: this.origin,
379-
});
369+
return spanToTraceContext(this);
380370
}
381371

382372
/**

packages/core/src/tracing/transaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { dropUndefinedKeys, logger, timestampInSeconds } from '@sentry/utils';
1414
import { DEBUG_BUILD } from '../debug-build';
1515
import type { Hub } from '../hub';
1616
import { getCurrentHub } from '../hub';
17+
import { spanToTraceContext } from '../utils/spanUtils';
1718
import { getDynamicSamplingContextFromClient } from './dynamicSamplingContext';
1819
import { Span as SpanClass, SpanRecorder } from './span';
1920
import { ensureTimestampInSeconds } from './utils';
@@ -283,7 +284,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
283284
contexts: {
284285
...this._contexts,
285286
// We don't want to override trace context
286-
trace: this.getTraceContext(),
287+
trace: spanToTraceContext(this),
287288
},
288289
spans: finishedSpans,
289290
start_timestamp: this.startTimestamp,

packages/core/src/utils/applyScopeDataToEvent.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Breadcrumb, Event, PropagationContext, ScopeData, Span } from '@sentry/types';
22
import { arrayify } from '@sentry/utils';
3+
import { spanToTraceContext } from './spanUtils';
34

45
/**
56
* Applies data from the scope to the event and runs all event processors on it.
@@ -161,7 +162,7 @@ function applySdkMetadataToEvent(
161162
}
162163

163164
function applySpanToEvent(event: Event, span: Span): void {
164-
event.contexts = { trace: span.getTraceContext(), ...event.contexts };
165+
event.contexts = { trace: spanToTraceContext(span), ...event.contexts };
165166
const transaction = span.transaction;
166167
if (transaction) {
167168
event.sdkProcessingMetadata = {

packages/core/src/utils/spanUtils.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
1-
import type { Span } from '@sentry/types';
2-
import { generateSentryTraceHeader } from '@sentry/utils';
1+
import type { Span, TraceContext } from '@sentry/types';
2+
import { dropUndefinedKeys, generateSentryTraceHeader } from '@sentry/utils';
3+
4+
/**
5+
* Convert a span to a trace context, which can be sent as the `trace` context in an event.
6+
*/
7+
export function spanToTraceContext(span: Span): TraceContext {
8+
const { data, description, op, parent_span_id, span_id, status, tags, trace_id, origin } = span.toJSON();
9+
10+
return dropUndefinedKeys({
11+
data,
12+
description,
13+
op,
14+
parent_span_id,
15+
span_id,
16+
status,
17+
tags,
18+
trace_id,
19+
origin,
20+
});
21+
}
322

423
/**
524
* Convert a Span to a Sentry trace header.

packages/hub/test/scope.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -361,20 +361,20 @@ describe('Scope', () => {
361361
const scope = new Scope();
362362
const span = {
363363
fake: 'span',
364-
getTraceContext: () => ({ a: 'b' }),
364+
toJSON: () => ({ origin: 'manual' }),
365365
} as any;
366366
scope.setSpan(span);
367367
const event: Event = {};
368368
const processedEvent = await scope.applyToEvent(event);
369-
expect((processedEvent!.contexts!.trace as any).a).toEqual('b');
369+
expect(processedEvent!.contexts!.trace as any).toEqual({ origin: 'manual' });
370370
});
371371

372372
test('existing trace context in event should take precedence', async () => {
373373
expect.assertions(1);
374374
const scope = new Scope();
375375
const span = {
376376
fake: 'span',
377-
getTraceContext: () => ({ a: 'b' }),
377+
toJSON: () => ({ a: 'b' }),
378378
} as any;
379379
scope.setSpan(span);
380380
const event: Event = {
@@ -392,7 +392,7 @@ describe('Scope', () => {
392392
const scope = new Scope();
393393
const transaction = {
394394
fake: 'span',
395-
getTraceContext: () => ({ a: 'b' }),
395+
toJSON: () => ({ a: 'b' }),
396396
name: 'fake transaction',
397397
getDynamicSamplingContext: () => ({}),
398398
} as any;
@@ -410,7 +410,7 @@ describe('Scope', () => {
410410
const transaction = { name: 'fake transaction', getDynamicSamplingContext: () => ({}) };
411411
const span = {
412412
fake: 'span',
413-
getTraceContext: () => ({ a: 'b' }),
413+
toJSON: () => ({ a: 'b' }),
414414
transaction,
415415
} as any;
416416
scope.setSpan(span);

packages/types/src/span.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { TraceContext } from './context';
12
import type { Instrumenter } from './instrumenter';
23
import type { Primitive } from './misc';
34
import type { Transaction } from './transaction';
@@ -243,17 +244,11 @@ export interface Span extends SpanContext {
243244
*/
244245
updateWithContext(spanContext: SpanContext): this;
245246

246-
/** Convert the object to JSON for w. spans array info only */
247-
getTraceContext(): {
248-
data?: { [key: string]: any };
249-
description?: string;
250-
op?: string;
251-
parent_span_id?: string;
252-
span_id: string;
253-
status?: string;
254-
tags?: { [key: string]: Primitive };
255-
trace_id: string;
256-
};
247+
/**
248+
* Convert the object to JSON for w. spans array info only.
249+
* @deprecated Use `spanToTraceContext()` util function instead.
250+
*/
251+
getTraceContext(): TraceContext;
257252

258253
/** Convert the object to JSON */
259254
toJSON(): {
@@ -267,5 +262,6 @@ export interface Span extends SpanContext {
267262
tags?: { [key: string]: Primitive };
268263
timestamp?: number;
269264
trace_id: string;
265+
origin?: SpanOrigin;
270266
};
271267
}

0 commit comments

Comments
 (0)