Tracer
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
__construct()
public
__construct(Resource $resource, InstrumentationScope $scope, SpanProcessor $processor, ClockInterface $clock, ContextStorage $contextStorage[, Sampler|null $sampler = null ]) : mixed
Parameters
- $resource : Resource
- $scope : InstrumentationScope
- $processor : SpanProcessor
- $clock : ClockInterface
- $contextStorage : ContextStorage
- $sampler : Sampler|null = null
activeSpan()
Get the currently active span context, or null if none.
public
activeSpan() : SpanContext|null
Return values
SpanContext|nullcomplete()
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
context()
Get the tracer's context.
public
context() : Context
Return values
ContextinstrumentationScope()
Get the instrumentation scope.
public
instrumentationScope() : InstrumentationScope
Return values
InstrumentationScopename()
Get the tracer name.
public
name() : string
Return values
stringprocessor()
Get the processor used by this tracer.
public
processor() : SpanProcessor
Return values
SpanProcessorspan()
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
Spantrace()
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
Return values
T —The callback result
version()
Get the tracer version.
public
version() : string
Return values
stringwithInstrumentationScope()
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