Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Deprecate span.getTraceContext() #10032

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ In v8, the Span class is heavily reworked. The following properties & methods ar
* `span.updateWithContext(newSpanContext)`: Update the fields directly instead.
* `span.setName(newName)`: Use `span.updateName(newName)` instead.
* `span.toTraceparent()`: use `spanToTraceHeader(span)` util instead.
* `span.getTraceContext()`: Use `spanToTraceContext(span)` utility function instead.

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

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { MetricsAggregator } from './metrics/aggregator';
import type { Scope } from './scope';
import { SessionFlusher } from './sessionflusher';
import { addTracingExtensions, getDynamicSamplingContextFromClient } from './tracing';
import { spanToTraceContext } from './utils/spanUtils';

export interface ServerRuntimeClientOptions extends ClientOptions<BaseTransportOptions> {
platform?: string;
Expand Down Expand Up @@ -256,7 +257,7 @@ export class ServerRuntimeClient<
const span = scope.getSpan();
if (span) {
const samplingContext = span.transaction ? span.transaction.getDynamicSamplingContext() : undefined;
return [samplingContext, span.getTraceContext()];
return [samplingContext, spanToTraceContext(span)];
}

const { traceId, spanId, parentSpanId, dsc } = scope.getPropagationContext();
Expand Down
14 changes: 2 additions & 12 deletions packages/core/src/tracing/span.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
import { dropUndefinedKeys, logger, timestampInSeconds, uuid4 } from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
import { spanToTraceHeader } from '../utils/spanUtils';
import { spanToTraceContext, spanToTraceHeader } from '../utils/spanUtils';
import { ensureTimestampInSeconds } from './utils';

/**
Expand Down Expand Up @@ -366,17 +366,7 @@ export class Span implements SpanInterface {
* @inheritDoc
*/
public getTraceContext(): TraceContext {
return dropUndefinedKeys({
data: this._getData(),
description: this.description,
op: this.op,
parent_span_id: this.parentSpanId,
span_id: this.spanId,
status: this.status,
tags: Object.keys(this.tags).length > 0 ? this.tags : undefined,
trace_id: this.traceId,
origin: this.origin,
});
return spanToTraceContext(this);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/tracing/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { dropUndefinedKeys, logger, timestampInSeconds } from '@sentry/utils';
import { DEBUG_BUILD } from '../debug-build';
import type { Hub } from '../hub';
import { getCurrentHub } from '../hub';
import { spanToTraceContext } from '../utils/spanUtils';
import { getDynamicSamplingContextFromClient } from './dynamicSamplingContext';
import { Span as SpanClass, SpanRecorder } from './span';
import { ensureTimestampInSeconds } from './utils';
Expand Down Expand Up @@ -283,7 +284,7 @@ export class Transaction extends SpanClass implements TransactionInterface {
contexts: {
...this._contexts,
// We don't want to override trace context
trace: this.getTraceContext(),
trace: spanToTraceContext(this),
},
spans: finishedSpans,
start_timestamp: this.startTimestamp,
Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/utils/applyScopeDataToEvent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Breadcrumb, Event, PropagationContext, ScopeData, Span } from '@sentry/types';
import { arrayify } from '@sentry/utils';
import { spanToTraceContext } from './spanUtils';

/**
* Applies data from the scope to the event and runs all event processors on it.
Expand Down Expand Up @@ -161,7 +162,7 @@ function applySdkMetadataToEvent(
}

function applySpanToEvent(event: Event, span: Span): void {
event.contexts = { trace: span.getTraceContext(), ...event.contexts };
event.contexts = { trace: spanToTraceContext(span), ...event.contexts };
const transaction = span.transaction;
if (transaction) {
event.sdkProcessingMetadata = {
Expand Down
23 changes: 21 additions & 2 deletions packages/core/src/utils/spanUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import type { Span } from '@sentry/types';
import { generateSentryTraceHeader } from '@sentry/utils';
import type { Span, TraceContext } from '@sentry/types';
import { dropUndefinedKeys, generateSentryTraceHeader } from '@sentry/utils';

/**
* Convert a span to a trace context, which can be sent as the `trace` context in an event.
*/
export function spanToTraceContext(span: Span): TraceContext {
const { data, description, op, parent_span_id, span_id, status, tags, trace_id, origin } = span.toJSON();

return dropUndefinedKeys({
data,
description,
op,
parent_span_id,
span_id,
status,
tags,
trace_id,
origin,
});
}

/**
* Convert a Span to a Sentry trace header.
Expand Down
10 changes: 5 additions & 5 deletions packages/hub/test/scope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,20 +361,20 @@ describe('Scope', () => {
const scope = new Scope();
const span = {
fake: 'span',
getTraceContext: () => ({ a: 'b' }),
toJSON: () => ({ origin: 'manual' }),
} as any;
scope.setSpan(span);
const event: Event = {};
const processedEvent = await scope.applyToEvent(event);
expect((processedEvent!.contexts!.trace as any).a).toEqual('b');
expect(processedEvent!.contexts!.trace as any).toEqual({ origin: 'manual' });
});

test('existing trace context in event should take precedence', async () => {
expect.assertions(1);
const scope = new Scope();
const span = {
fake: 'span',
getTraceContext: () => ({ a: 'b' }),
toJSON: () => ({ a: 'b' }),
} as any;
scope.setSpan(span);
const event: Event = {
Expand All @@ -392,7 +392,7 @@ describe('Scope', () => {
const scope = new Scope();
const transaction = {
fake: 'span',
getTraceContext: () => ({ a: 'b' }),
toJSON: () => ({ a: 'b' }),
name: 'fake transaction',
getDynamicSamplingContext: () => ({}),
} as any;
Expand All @@ -410,7 +410,7 @@ describe('Scope', () => {
const transaction = { name: 'fake transaction', getDynamicSamplingContext: () => ({}) };
const span = {
fake: 'span',
getTraceContext: () => ({ a: 'b' }),
toJSON: () => ({ a: 'b' }),
transaction,
} as any;
scope.setSpan(span);
Expand Down
18 changes: 7 additions & 11 deletions packages/types/src/span.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { TraceContext } from './context';
import type { Instrumenter } from './instrumenter';
import type { Primitive } from './misc';
import type { Transaction } from './transaction';
Expand Down Expand Up @@ -243,17 +244,11 @@ export interface Span extends SpanContext {
*/
updateWithContext(spanContext: SpanContext): this;

/** Convert the object to JSON for w. spans array info only */
getTraceContext(): {
data?: { [key: string]: any };
description?: string;
op?: string;
parent_span_id?: string;
span_id: string;
status?: string;
tags?: { [key: string]: Primitive };
trace_id: string;
};
/**
* Convert the object to JSON for w. spans array info only.
* @deprecated Use `spanToTraceContext()` util function instead.
*/
getTraceContext(): TraceContext;

/** Convert the object to JSON */
toJSON(): {
Expand All @@ -267,5 +262,6 @@ export interface Span extends SpanContext {
tags?: { [key: string]: Primitive };
timestamp?: number;
trace_id: string;
origin?: SpanOrigin;
};
}