Skip to content

Commit 45d13cc

Browse files
committed
feat(core): Deprecate span.getTraceContext()
Instead, you can use a new `spanToTraceContext(span)` util function.
1 parent 4c82160 commit 45d13cc

File tree

7 files changed

+37
-25
lines changed

7 files changed

+37
-25
lines changed

MIGRATION.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ In v8, the Span class is heavily reworked. The following properties & methods ar
1414

1515
* `span.toContext()`: Access the fields directly instead.
1616
* `span.updateWithContext(newSpanContext)`: Update the fields directly instead.
17+
* `span.getTraceContext()`: Use `spanToTraceContext(span)` utility function instead.
1718

1819
## Deprecate `pushScope` & `popScope` in favor of `withScope`
1920

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 & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {
1313
import { dropUndefinedKeys, generateSentryTraceHeader, logger, timestampInSeconds, uuid4 } from '@sentry/utils';
1414

1515
import { DEBUG_BUILD } from '../debug-build';
16+
import { spanToTraceContext } from '../utils/spanUtils';
1617
import { ensureTimestampInSeconds } from './utils';
1718

1819
/**
@@ -357,17 +358,7 @@ export class Span implements SpanInterface {
357358
* @inheritDoc
358359
*/
359360
public getTraceContext(): TraceContext {
360-
return dropUndefinedKeys({
361-
data: this._getData(),
362-
description: this.description,
363-
op: this.op,
364-
parent_span_id: this.parentSpanId,
365-
span_id: this.spanId,
366-
status: this.status,
367-
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
368-
trace_id: this.traceId,
369-
origin: this.origin,
370-
});
361+
return spanToTraceContext(this);
371362
}
372363

373364
/**

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';
@@ -272,7 +273,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
272273
contexts: {
273274
...this._contexts,
274275
// We don't want to override trace context
275-
trace: this.getTraceContext(),
276+
trace: spanToTraceContext(this),
276277
},
277278
spans: finishedSpans,
278279
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { Span, TraceContext } from '@sentry/types';
2+
import { dropUndefinedKeys } 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+
}

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';
@@ -233,17 +234,11 @@ export interface Span extends SpanContext {
233234
*/
234235
updateWithContext(spanContext: SpanContext): this;
235236

236-
/** Convert the object to JSON for w. spans array info only */
237-
getTraceContext(): {
238-
data?: { [key: string]: any };
239-
description?: string;
240-
op?: string;
241-
parent_span_id?: string;
242-
span_id: string;
243-
status?: string;
244-
tags?: { [key: string]: Primitive };
245-
trace_id: string;
246-
};
237+
/**
238+
* Convert the object to JSON for w. spans array info only.
239+
* @deprecated Use `spanToTraceContext()` util function instead.
240+
*/
241+
getTraceContext(): TraceContext;
247242

248243
/** Convert the object to JSON */
249244
toJSON(): {
@@ -257,5 +252,6 @@ export interface Span extends SpanContext {
257252
tags?: { [key: string]: Primitive };
258253
timestamp?: number;
259254
trace_id: string;
255+
origin?: SpanOrigin;
260256
};
261257
}

0 commit comments

Comments
 (0)