Flow PHP

Span

FinalYes

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
see
https://opentelemetry.io/docs/specs/otel/trace/api/#span

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

Add a link to another span.

public addLink(SpanLink $link) : $this
Parameters
$link : SpanLink
Return values
$this

attributes()

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
Attributes

duration()

Get the span duration in milliseconds.

public duration() : float|null

Returns null if the span has not ended yet.

Return values
float|null

end()

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
$this

endTime()

Get the span end time, if ended.

public endTime() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

events()

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
self

isEnded()

Check if the span has ended.

public isEnded() : bool
Return values
bool

isRecording()

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
bool

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
string

normalize()

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: 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}

recordEvent()

Record an event in this span.

public recordEvent(SpanEvent $event) : $this
Parameters
$event : SpanEvent
Return values
$this

recordException()

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
$this

rename()

Rename the span.

public rename(string $name) : $this
Parameters
$name : string
Return values
$this

resource()

Get the resource describing the entity producing telemetry.

public resource() : Resource
Return values
Resource

setAttribute()

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
$this

setAttributes()

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
$this

setStatus()

Set the span status.

public setStatus(SpanStatus $status) : $this
Parameters
$status : SpanStatus
Return values
$this

startTime()

Get the span start time.

public startTime() : DateTimeImmutable
Return values
DateTimeImmutable

        
On this page

Search results