Skip to main content
Trace context contains information about the current trace and span, enabling distributed tracing across services.

Overview

Trace context includes:
  • Trace ID: Unique identifier for the entire trace
  • Span ID: Unique identifier for the current span
  • Parent Span ID: Links spans together
  • Sampling decision: Whether the trace is sampled
  • Trace flags: Metadata about the trace

SpanContextData

From packages/core/src/types-hoist/span.ts:

Getting Trace Context

From Active Span

From Scope

PropagationContext

Stored on the scope for trace propagation:

Setting Propagation Context

Trace Headers

sentry-trace Header

Format: {trace_id}-{span_id}-{sampled}

baggage Header

Contains dynamic sampling context:

Continuing Traces

continueTrace

Continue a trace from incoming headers:

From HTML Meta Tags (Browser)

Dynamic Sampling Context

Additional context for sampling decisions:

Getting DSC

Root Span

Get the root span of the current trace:

Trace ID Generation

Span Relationships

Custom Parent Span

Sampling Decisions

Trace State

OpenTelemetry trace state for vendor-specific data:

Implementation Example

From packages/core/src/tracing/trace.ts:

Best Practices

1. Always Propagate Context

2. Continue Traces in Services

3. Preserve Sampling Decisions