class Tracer

This class provides static functions to give you access to the current request's singleton tracer. You should use this class to instrument your code.

The first step, is to configure and start your Tracer. Calling start will collect trace data during your request and report the results at the request using the provided reporter.

Example:

use OpenCensus\Trace\Tracer;
use OpenCensus\Trace\Exporter\EchoExporter;

$reporter = new EchoExporter();
Tracer::start($reporter);

In the above example, every request is traced. This is not advised as it will add some latency to each request. We provide a sampling mechanism via the OpenCensus\Trace\Sampler\SamplerInterface. To add sampling to your request tracer, provide the "sampler" option:

// $cache is a PSR-6 cache implementation
$sampler = new QpsSampler($cache, ['rate' => 0.1]);
Tracer::start($reporter, [
    'sampler' => $sampler
]);

The above uses a query-per-second sampler at 0.1 requests/second. The implementation requires a PSR-6 cache. See OpenCensus\Trace\Sampler\QpsSampler for more information. You may provide your own implementation of OpenCensus\Trace\Sampler\SamplerInterface or use one of the provided.

To trace code, you can use static OpenCensus\Trace\Tracer::inSpan() helper function:

Tracer::start($reporter);
Tracer::inSpan(['name' => 'outer'], function () {
    // some code
    Tracer::inSpan(['name' => 'inner'], function () {
        // some code
    });
    // some code
});

You can also start and finish spans independently throughout your code.

Explicitly tracing spans:

// Creates a detached span
$span = Tracer::startSpan(['name' => 'expensive-operation']);

// Opens a scope that attaches the span to the current context
$scope = Tracer::withSpan($span);
try {
    $pi = calculatePi(1000);
} finally {
    // Closes the scope (ends the span)
    $scope->close();
}

It is recommended that you use the OpenCensus\Trace\Tracer::inSpan() method where you can.

Methods

static RequestHandler
start(ExporterInterface $reporter, array $options = [])

Start a new trace session for this request. You should call this as early as possible for the most accurate results.

static mixed
inSpan(array $spanOptions, callable $callable, array $arguments = [])

Instrument a callable by creating a Span that manages the startTime and endTime.

static Span
startSpan(array $spanOptions = [])

Explicitly start a new Span. You will need to attach the span and handle any thrown exceptions.

static Scope
withSpan(Span $span)

Attaches the provided span as the current span and returns a Scope object which must be closed.

static 
addAttribute(string $attribute, string $value, array $options = [])

Add an attribute to the provided Span

static 
addAnnotation(string $description, array $options = [])

Add an annotation to the provided Span

static 
addLink(string $traceId, string $spanId, array $options = [])

Add a link to the provided Span

static 
addMessageEvent(string $type, string $id, array $options = [])

Add an message event to the provided Span

static SpanContext
spanContext()

Returns the current span context.

Details

at line 120
static RequestHandler start(ExporterInterface $reporter, array $options = [])

Start a new trace session for this request. You should call this as early as possible for the most accurate results.

Parameters

ExporterInterface $reporter
array $options Configuration options. See OpenCensus\Trace\Span::__construct() for the other available options.

 @type SamplerInterface $sampler Sampler that defines the sampling rules.
       **Defaults to** a new `AlwaysSampleSampler`.
 @type PropagatorInterface $propagator SpanContext propagator. **Defaults to**
       a new `HttpHeaderPropagator` instance
 @type array $headers Optional array of headers to use in place of $_SERVER

Return Value

RequestHandler

at line 162
static mixed inSpan(array $spanOptions, callable $callable, array $arguments = [])

Instrument a callable by creating a Span that manages the startTime and endTime.

If an exception is thrown while executing the callable, the exception will be caught, the span will be closed, and the exception will be re-thrown.

Example:

// Instrumenting code as a closure
Tracer::inSpan(['name' => 'some-closure'], function () {
  // do something expensive
});
// Instrumenting code as a callable (parameters optional)
function fib($n) {
  // do something expensive
}
$number = Tracer::inSpan(['name' => 'some-callable'], 'fib', [10]);

Parameters

array $spanOptions Options for the span. See OpenCensus\Trace\Span::__construct()
callable $callable The callable to instrument.
array $arguments [optional] Arguments to the callable.

Return Value

mixed Returns whatever the callable returns

at line 190
static Span startSpan(array $spanOptions = [])

Explicitly start a new Span. You will need to attach the span and handle any thrown exceptions.

Example:

$span = Tracer::startSpan(['name' => 'expensive-operation']);
$scope = Tracer::withSpan($span);
try {
    // do something expensive
} catch (\Exception $e) {
} finally {
    $scope->close();
}

Parameters

array $spanOptions [optional] Options for the span. See OpenCensus\Trace\Span::__construct()

Return Value

Span

at line 216
static Scope withSpan(Span $span)

Attaches the provided span as the current span and returns a Scope object which must be closed.

Example:

$span = Tracer::startSpan(['name' => 'expensive-operation']);
$scope = Tracer::withSpan($span);
try {
    // do something expensive
} finally {
    $scope->close();
}

Parameters

Span $span

Return Value

Scope

at line 234
static addAttribute(string $attribute, string $value, array $options = [])

Add an attribute to the provided Span

Parameters

string $attribute
string $value
array $options [optional] Configuration options.

 @type Span $span The span to add the attribute to.

at line 252
static addAnnotation(string $description, array $options = [])

Add an annotation to the provided Span

Parameters

string $description
array $options [optional] Configuration options.

 @type Span $span The span to add the annotation to.
 @type array $attributes Attributes for this annotation.
 @type \DateTimeInterface|int|float $time The time of this event.

Add a link to the provided Span

Parameters

string $traceId
string $spanId
array $options [optional] Configuration options.

 @type Span $span The span to add the link to.
 @type string $type The relationship of the current span relative to
       the linked span: child, parent, or unspecified.
 @type array $attributes Attributes for this annotation.
 @type \DateTimeInterface|int|float $time The time of this event.

at line 296
static addMessageEvent(string $type, string $id, array $options = [])

Add an message event to the provided Span

Parameters

string $type
string $id
array $options [optional] Configuration options.

 @type Span $span The span to add the message event to.
 @type int $uncompressedSize The number of uncompressed bytes sent or
       received.
 @type int $compressedSize The number of compressed bytes sent or
       received. If missing assumed to be the same size as
       uncompressed.
 @type \DateTimeInterface|int|float $time The time of this event.

at line 309
static SpanContext spanContext()

Returns the current span context.

Return Value

SpanContext