dkx / google-tracer
This package is abandoned and no longer maintained.
No replacement package was suggested.
Wrapper for Google Trace library
1.0.0
2020-01-19 16:04 UTC
Requires
- php: >=7.3
- google/cloud-trace: ^0.16
- psr/http-message: ^1.0
- psr/http-server-middleware: ^1.0
- psr/simple-cache: ^1.0
- ramsey/uuid: ^3.9
Requires (Dev)
- mockery/mockery: ^1.3
- phpstan/extension-installer: >=1.0
- phpstan/phpstan: >=0.11
- phpstan/phpstan-phpunit: >=0.11
- phpstan/phpstan-strict-rules: >=0.11
- phpunit/phpunit: >=8.0
README
Wrapper for Google Trace library
Only for psr7 applications
Installation
$ composer require dkx/google-tracer
Basic usage
<?php
use DKX\GoogleTracer\Exporter\GoogleTraceExporter;
use DKX\GoogleTracer\Sampler\AlwaysSampler;
use DKX\GoogleTracer\Tracer;
use Google\Cloud\Trace\TraceClient;
$projectName = 'my-app/production';
$projectVersion = '0.0.1';
$traceClient = new TraceClient();
$exporter = new GoogleTraceExporter($traceClient);
$sampler = new AlwaysSampler();
$tracer = new Tracer($exporter, $sampler, $projectName, $projectVersion);
$trace = $tracer->start();
$request = getCurrentHttpRequestSomehow();
try {
$response = getCurrentHttpResponseSomehow($request);
$tracer->finishSuccessfully($trace, $request, $response);
} catch(\Throwable $e) {
$tracer->finishWithError($trace, $request, $e);
}
Create sub-spans
<?php
use DKX\GoogleTracer\Span;
$trace->span('controller', function (Span $span): void {
$users = $span->span('users.load', function (Span $span): array {
$users = $span->span('users.load.fromCache', function (Span $span): ?array {
return tryToLoadUsersFromCache();
});
if ($users === null) {
$users = $span->span('users.load.fromDatabase', function (Span $span): array {
return loadUsersFromDatabase();
});
$span->span('users.load.saveCache', function (Span $span) use ($users): void {
saveUsersToCache($users);
});
}
return $users;
});
var_dump($users);
});
PSR15 middleware
<?php
use DKX\GoogleTracer\Psr15\TracerControllerSpanMiddleware;
$middleware = new TracerControllerSpanMiddleware($trace);
This will automatically register a controller
span into $trace
(can be replaced with span). Created span is added
into http request's attributes.
<?php
use DKX\GoogleTracer\Psr15\TracerControllerSpanMiddleware;
/** @var \DKX\GoogleTracer\Span $span */
$span = $request->getAttribute(TracerControllerSpanMiddleware::CONTROLLER_SPAN);
$span->span('inside-of-controller', function (): void {
// todo: watched span in a controller
});
Or you can use dynamic middleware which will load parent trace or span from http request attribute:
<?php
use DKX\GoogleTracer\Psr15\TracerControllerSpanDynamicMiddleware;
$request = getCurrentHttpRequestSomehow();
$request->withAttribute('controllerSpanParent', $trace);
$middleware = new TracerControllerSpanDynamicMiddleware('controllerSpanParent');
Exporters
DKX\GoogleTracer\Exporter\GoogleTraceExporter
: save trace data into Google Cloud TraceDKX\GoogleTracer\Exporter\VoidExporter
: "do nothing" exporter
Samplers
DKX\GoogleTracer\Sampler\AlwaysSampler
: save traces for all requestsDKX\GoogleTracer\Sampler\NeverSampler
: don't save any tracesDKX\GoogleTracer\Sampler\ProbabilitySampler($rate)
: save traces based on probability rate (0 <> 1)DKX\GoogleTracer\Sampler\QpsSampler($cache, $cacheKey, $rate)
: save traces based on queries per second rate (0 <> 1)