Skip to content

Commit e585da5

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

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
@@ -15,6 +15,7 @@ In v8, the Span class is heavily reworked. The following properties & methods ar
1515
* `span.toContext()`: Access the fields directly instead.
1616
* `span.updateWithContext(newSpanContext)`: Update the fields directly instead.
1717
* `span.setName(newName)`: Use `span.updateName(newName)` instead.
18+
* `span.getTraceContext()`: Use `spanToTraceContext(span)` utility function instead.
1819

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

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
/**
@@ -365,17 +366,7 @@ export class Span implements SpanInterface {
365366
* @inheritDoc
366367
*/
367368
public getTraceContext(): TraceContext {
368-
return dropUndefinedKeys({
369-
data: this._getData(),
370-
description: this.description,
371-
op: this.op,
372-
parent_span_id: this.parentSpanId,
373-
span_id: this.spanId,
374-
status: this.status,
375-
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
376-
trace_id: this.traceId,
377-
origin: this.origin,
378-
});
369+
return spanToTraceContext(this);
379370
}
380371

381372
/**

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 & 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';
@@ -240,17 +241,11 @@ export interface Span extends SpanContext {
240241
*/
241242
updateWithContext(spanContext: SpanContext): this;
242243

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

255250
/** Convert the object to JSON */
256251
toJSON(): {
@@ -264,5 +259,6 @@ export interface Span extends SpanContext {
264259
tags?: { [key: string]: Primitive };
265260
timestamp?: number;
266261
trace_id: string;
262+
origin?: SpanOrigin;
267263
};
268264
}

0 commit comments

Comments
 (0)