SpanContext represents the serializable portion of a Span. It carries the trace ID, span ID, and baggage that must be propagated across process boundaries. Obtain a SpanContext by calling span.context().
SpanContext is safe to use after the parent span has finished.
Methods
toTraceId()
Returns the string representation of the trace ID.
The trace ID as a hex string.
toSpanId()
Returns the string representation of the span ID.
The span ID as a hex string.
toTraceparent()
Returns the W3C traceparent header string representation of this context. This is used for Database Monitoring (DBM) integration.
A W3C traceparent string in the format
00-<traceId>-<spanId>-<flags>.Context propagation
Usetracer.inject() and tracer.extract() to propagate SpanContext across service boundaries:
Internal properties
TheSpanContext interface extends the OpenTracing SpanContext. The internal _traceId and _spanId fields are implementation details and should not be relied upon directly — use toTraceId() and toSpanId() instead.
For OpenTelemetry compatibility, the tracer.opentelemetry namespace provides its own SpanContext interface with traceId, spanId, traceFlags, and traceState fields conforming to the OTel spec. See OpenTelemetry Tracing.