SpanContext
Immutable identification of a span within a trace.
SpanContext carries the identifying information that makes a span unique within a trace: the trace ID, span ID, and optional parent span ID. It also tracks whether the span originated from a remote process, and includes trace flags and vendor-specific trace state.
Example usage:
$context = SpanContext::create(
TraceId::generate(),
SpanId::generate(),
);
echo $context->traceId->toHex();
echo $context->traceFlags->isSampled() ? 'sampled' : 'not sampled';
Properties
- $isRemote : bool
- $parentSpanId : SpanId|null
- $spanId : SpanId
- $traceFlags : TraceFlags
- $traceId : TraceId
- $traceState : TraceState
Methods
- __construct() : mixed
- create() : self
- Create a local SpanContext.
- createRemote() : self
- Create a remote SpanContext (received from another process).
- fromArray() : self
- Create a SpanContext from a normalized array representation.
- isRoot() : bool
- Check if this is a root span (no parent).
- isValid() : bool
- Check if this SpanContext has valid trace and span IDs.
-
normalize()
: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: array
}} - Normalize the SpanContext to an array representation for serialization.
- withTraceFlags() : self
- Create a new SpanContext with the specified trace flags.
- withTraceState() : self
- Create a new SpanContext with the specified trace state.
Properties
$isRemote
public
bool
$isRemote
= false
$parentSpanId
public
SpanId|null
$parentSpanId
= null
$spanId
public
SpanId
$spanId
$traceFlags
public
TraceFlags
$traceFlags
$traceId
public
TraceId
$traceId
$traceState
public
TraceState
$traceState
Methods
__construct()
public
__construct(TraceId $traceId, SpanId $spanId[, SpanId|null $parentSpanId = null ][, bool $isRemote = false ][, TraceFlags|null $traceFlags = null ][, TraceState|null $traceState = null ]) : mixed
Parameters
- $traceId : TraceId
- $spanId : SpanId
- $parentSpanId : SpanId|null = null
- $isRemote : bool = false
- $traceFlags : TraceFlags|null = null
- $traceState : TraceState|null = null
create()
Create a local SpanContext.
public
static create(TraceId $traceId, SpanId $spanId[, SpanId|null $parentSpanId = null ][, TraceFlags|null $traceFlags = null ][, TraceState|null $traceState = null ]) : self
Parameters
- $traceId : TraceId
- $spanId : SpanId
- $parentSpanId : SpanId|null = null
- $traceFlags : TraceFlags|null = null
- $traceState : TraceState|null = null
Return values
selfcreateRemote()
Create a remote SpanContext (received from another process).
public
static createRemote(TraceId $traceId, SpanId $spanId[, SpanId|null $parentSpanId = null ][, TraceFlags|null $traceFlags = null ][, TraceState|null $traceState = null ]) : self
Parameters
- $traceId : TraceId
- $spanId : SpanId
- $parentSpanId : SpanId|null = null
- $traceFlags : TraceFlags|null = null
- $traceState : TraceState|null = null
Return values
selffromArray()
Create a SpanContext from a normalized array representation.
public
static fromArray(array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags?: array{byte: int}, traceState?: array{entries: array}} $data) : self
Parameters
-
$data
: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags?: array{byte: int}, traceState?: array{entries: array
}} -
Normalized SpanContext data
Return values
selfisRoot()
Check if this is a root span (no parent).
public
isRoot() : bool
Return values
boolisValid()
Check if this SpanContext has valid trace and span IDs.
public
isValid() : bool
A SpanContext is valid when both the trace ID and span ID are non-zero. Note: SpanId and TraceId already reject all-zero values at construction, so any successfully created SpanContext is valid by construction.
Return values
boolnormalize()
Normalize the SpanContext to an array representation for serialization.
public
normalize() : array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: array}}
Return values
array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: arraywithTraceFlags()
Create a new SpanContext with the specified trace flags.
public
withTraceFlags(TraceFlags $traceFlags) : self
Parameters
- $traceFlags : TraceFlags
Return values
selfwithTraceState()
Create a new SpanContext with the specified trace state.
public
withTraceState(TraceState $traceState) : self
Parameters
- $traceState : TraceState