Tracer
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
Start a new trace session for this request. You should call this as early as possible for the most accurate results.
Instrument a callable by creating a Span that manages the startTime and endTime.
Explicitly start a new Span. You will need to attach the span and handle any thrown exceptions.
Add an attribute to the provided Span
Add an annotation to the provided Span
Add a link to the provided Span
Add an message event to the provided Span
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.
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]);
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();
}
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();
}
at line 234
static
addAttribute(string $attribute, string $value, array $options = [])
Add an attribute to the provided Span
at line 252
static
addAnnotation(string $description, array $options = [])
Add an annotation to the provided Span
at line 273
static
addLink(string $traceId, string $spanId, array $options = [])
Add a link to the provided Span
at line 296
static
addMessageEvent(string $type, string $id, array $options = [])
Add an message event to the provided Span
at line 309
static SpanContext
spanContext()
Returns the current span context.