Span
Represents a single operation within a trace.
A Span is the primary building block of a trace, representing a single operation with a start time, end time, attributes, events, and links. Spans are mutable during their lifetime and can accumulate state.
Example usage:
$span = new Span('process-order', $context, SpanKind::INTERNAL, new \DateTimeImmutable());
$span->setAttribute('order.id', '12345')
->recordEvent(GenericEvent::create('validation.passed', new \DateTimeImmutable()))
->setStatus(SpanStatus::ok())
->end();
Tags
Methods
- __construct() : mixed
- addLink() : $this
- Add a link to another span.
- attributes() : array<string, array<string|int, bool|float|int|string>|bool|float|int|string>
- Get all span attributes as array.
- attributesObject() : Attributes
- Get the span attributes as an Attributes object.
- context() : SpanContext
- Get the span context.
- duration() : float|null
- Get the span duration in milliseconds.
- end() : $this
- End the span, optionally with a specific end time.
- endTime() : DateTimeImmutable|null
- Get the span end time, if ended.
- events() : array<string|int, SpanEvent>
- Get all recorded events.
- fromArray() : self
- Create a Span from a normalized array representation.
- isEnded() : bool
- Check if the span has ended.
- isRecording() : bool
- Check if this span is recording events, attributes, and links.
- kind() : SpanKind
- Get the span kind.
- links() : array<string|int, SpanLink>
- Get all span links.
- name() : string
- Get the span name.
-
normalize()
: array{name: string, context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: array
}}, kind: string, startTime: string, endTime: null|string, resource: array{attributes: array }, scope: array{name: string, version: string, schemaUrl: null|string, attributes: array }, attributes: array , events: array{name: string, timestamp: string, attributes: array }[], links: array{context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool}, attributes: array }[], status: null|array{code: int, description: null|string}, isRecording: bool} - Normalize the Span to an array representation for serialization.
- recordEvent() : $this
- Record an event in this span.
- recordException() : $this
- Record an exception as an event.
- rename() : $this
- Rename the span.
- resource() : Resource
- Get the resource describing the entity producing telemetry.
- scope() : InstrumentationScope
- Get the instrumentation scope that created this span.
- setAttribute() : $this
- Set a single attribute.
- setAttributes() : $this
- Set multiple attributes at once.
- setStatus() : $this
- Set the span status.
- startTime() : DateTimeImmutable
- Get the span start time.
- status() : SpanStatus|null
- Get the span status.
Methods
__construct()
public
__construct(string $name, SpanContext $context, SpanKind $kind, DateTimeImmutable $startTime, Resource $resource, InstrumentationScope $scope[, bool $isRecording = true ]) : mixed
Parameters
- $name : string
- $context : SpanContext
- $kind : SpanKind
- $startTime : DateTimeImmutable
- $resource : Resource
- $scope : InstrumentationScope
- $isRecording : bool = true
addLink()
Add a link to another span.
public
addLink(SpanLink $link) : $this
Parameters
- $link : SpanLink
Return values
$thisattributes()
Get all span attributes as array.
public
attributes() : array<string, array<string|int, bool|float|int|string>|bool|float|int|string>
Return values
array<string, array<string|int, bool|float|int|string>|bool|float|int|string>attributesObject()
Get the span attributes as an Attributes object.
public
attributesObject() : Attributes
Return values
Attributescontext()
Get the span context.
public
context() : SpanContext
Return values
SpanContextduration()
Get the span duration in milliseconds.
public
duration() : float|null
Returns null if the span has not ended yet.
Return values
float|nullend()
End the span, optionally with a specific end time.
public
end([DateTimeImmutable|null $endTime = null ]) : $this
If no end time is provided, the current time is used.
Parameters
- $endTime : DateTimeImmutable|null = null
Return values
$thisendTime()
Get the span end time, if ended.
public
endTime() : DateTimeImmutable|null
Return values
DateTimeImmutable|nullevents()
Get all recorded events.
public
events() : array<string|int, SpanEvent>
Return values
array<string|int, SpanEvent>fromArray()
Create a Span from a normalized array representation.
public
static fromArray(array{name: string, context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: array}}, kind: string, startTime: string, endTime: null|string, resource: array{attributes: array}, scope: array{name: string, version: string, schemaUrl: null|string, attributes: array}, attributes: array, events: array{name: string, timestamp: string, attributes: array}[], links: array{context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool}, attributes: array}[], status: null|array{code: int, description: null|string}, isRecording: bool} $data) : self
Note: The returned span is always in an ended state (isRecording = false) since it represents a completed span that was serialized.
Parameters
-
$data
: array{name: string, context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: array
}}, kind: string, startTime: string, endTime: null|string, resource: array{attributes: array }, scope: array{name: string, version: string, schemaUrl: null|string, attributes: array }, attributes: array , events: array{name: string, timestamp: string, attributes: array }[], links: array{context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool}, attributes: array }[], status: null|array{code: int, description: null|string}, isRecording: bool} -
Normalized Span data
Return values
selfisEnded()
Check if the span has ended.
public
isEnded() : bool
Return values
boolisRecording()
Check if this span is recording events, attributes, and links.
public
isRecording() : bool
Non-recording spans are created when sampling decisions indicate the span should not be exported. Non-recording spans still propagate context but don't record any data.
Return values
boolkind()
Get the span kind.
public
kind() : SpanKind
Return values
SpanKindlinks()
Get all span links.
public
links() : array<string|int, SpanLink>
Return values
array<string|int, SpanLink>name()
Get the span name.
public
name() : string
Return values
stringnormalize()
Normalize the Span to an array representation for serialization.
public
normalize() : array{name: string, context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: array}}, kind: string, startTime: string, endTime: null|string, resource: array{attributes: array}, scope: array{name: string, version: string, schemaUrl: null|string, attributes: array}, attributes: array, events: array{name: string, timestamp: string, attributes: array}[], links: array{context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool}, attributes: array}[], status: null|array{code: int, description: null|string}, isRecording: bool}
Return values
array{name: string, context: array{traceId: array{hex: string}, spanId: array{hex: string}, parentSpanId: null|array{hex: string}, isRemote: bool, traceFlags: array{byte: int}, traceState: array{entries: arrayrecordEvent()
Record an event in this span.
public
recordEvent(SpanEvent $event) : $this
Parameters
- $event : SpanEvent
Return values
$thisrecordException()
Record an exception as an event.
public
recordException(Throwable $exception, DateTimeImmutable $timestamp[, array<string, array<string|int, bool|float|int|string>|bool|float|int|string> $attributes = [] ]) : $this
Creates an event with OpenTelemetry semantic conventions for exceptions.
Parameters
- $exception : Throwable
-
The exception to record
- $timestamp : DateTimeImmutable
-
The timestamp when the exception occurred
- $attributes : array<string, array<string|int, bool|float|int|string>|bool|float|int|string> = []
-
Additional attributes
Return values
$thisrename()
Rename the span.
public
rename(string $name) : $this
Parameters
- $name : string
Return values
$thisresource()
Get the resource describing the entity producing telemetry.
public
resource() : Resource
Return values
Resourcescope()
Get the instrumentation scope that created this span.
public
scope() : InstrumentationScope
Return values
InstrumentationScopesetAttribute()
Set a single attribute.
public
setAttribute(string $key, array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable $value) : $this
Parameters
- $key : string
- $value : array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable
Return values
$thissetAttributes()
Set multiple attributes at once.
public
setAttributes(array<string, array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable> $attributes) : $this
Parameters
- $attributes : array<string, array<string|int, bool|DateTimeInterface|float|int|string|Throwable>|bool|DateTimeInterface|float|int|string|Throwable>
Return values
$thissetStatus()
Set the span status.
public
setStatus(SpanStatus $status) : $this
Parameters
- $status : SpanStatus
Return values
$thisstartTime()
Get the span start time.
public
startTime() : DateTimeImmutable
Return values
DateTimeImmutablestatus()
Get the span status.
public
status() : SpanStatus|null