Flow PHP

Tracer

FinalYes

Creates and manages spans within a trace.

Tracer is the primary API for distributed tracing. It creates spans, manages parent-child relationships through a span stack, and delegates span lifecycle events to a SpanProcessor.

Example usage:

$span = $tracer->span('process-order');
try {
    // do work
    $span->setStatus(SpanStatus::ok());
} catch (\Throwable $e) {
    $span->recordException($e)->setStatus(SpanStatus::error($e->getMessage()));
    throw $e;
} finally {
    $tracer->complete($span);
}

Or use the trace() helper:

$result = $tracer->trace('process-order', function() {
    // do work
    return $result;
});

Methods

__construct()  : mixed
activeSpan()  : SpanContext|null
Get the currently active span context, or null if none.
complete()  : void
Complete a span and pass it to the processor.
context()  : Context
Get the tracer's context.
instrumentationScope()  : InstrumentationScope
Get the instrumentation scope.
name()  : string
Get the tracer name.
processor()  : SpanProcessor
Get the processor used by this tracer.
span()  : Span
Start a new span.
trace()  : T
Execute a callable within a span, automatically completing it.
version()  : string
Get the tracer version.
withInstrumentationScope()  : self
Change the instrumentation scope for this tracer.

Methods

complete()

Complete a span and pass it to the processor.

public complete(Span $span) : void

This ends the span (if not already ended), removes it from the active span stack, and notifies the processor.

Parameters
$span : Span

name()

Get the tracer name.

public name() : string
Return values
string

span()

Start a new span.

public span(string $name[, SpanKind $kind = SpanKind::INTERNAL ][, array<string, array<string|int, bool|float|int|string>|bool|float|int|string> $attributes = [] ][, array<string|int, SpanLink$links = [] ]) : Span

If there's an active span, it becomes the parent of the new span. The new span is pushed onto the stack and becomes the active span.

Parameters
$name : string

The span name

$kind : SpanKind = SpanKind::INTERNAL

The span kind

$attributes : array<string, array<string|int, bool|float|int|string>|bool|float|int|string> = []

Initial attributes

$links : array<string|int, SpanLink> = []

Links to other spans

Return values
Span

trace()

Execute a callable within a span, automatically completing it.

public trace(string $name, callable(): T $callback[, SpanKind $kind = SpanKind::INTERNAL ]) : T

The span is automatically completed after the callback finishes, regardless of whether it throws an exception. If an exception is thrown, it is recorded as an event and the status is set to error.

Parameters
$name : string

The span name

$callback : callable(): T

The callable to execute

$kind : SpanKind = SpanKind::INTERNAL

The span kind

Tags
template
throws
Throwable

Rethrows any exception from the callback

Return values
T

The callback result

version()

Get the tracer version.

public version() : string
Return values
string

withInstrumentationScope()

Change the instrumentation scope for this tracer.

public withInstrumentationScope(InstrumentationScope $scope) : self

This mutates the tracer instance and returns it for method chaining.

Parameters
$scope : InstrumentationScope
Return values
self

        
On this page

Search results