jcchavezs/dd-opentracing

This package is abandoned and no longer maintained. The author suggests using the https://github.com/dataDog/dd-trace-php package instead.

DataDog implementation for OpenTracing in PHP

0.3.1 2017-05-19 18:50 UTC

This package is auto-updated.

Last update: 2022-02-01 13:06:50 UTC


README

Build Status Join the chat at https://gitter.im/dd-opentracing-php/Lobby

DataDog implementation for OpenTracing in PHP

Installation

Execute:

composer require jcchavezs/dd-opentracing

Examples

use DdOpenTracing\Tracer;
use OpenTracing\Carriers\HttpHeaders;
use OpenTracing\GlobalTracer;
use OpenTracing\Propagator;
use OpenTracing\SpanContext;
use OpenTracing\SpanReference\ChildOf;
use OpenTracing\Tag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

$request = Request::createFromGlobals();

$client = new \GuzzleHttp\Client();
$logger = new \Monolog\Logger("log");
$encoderFactory = new \DdTrace\Encoders\JsonFactory;
$transport = new \DdTrace\Transports\Http($client, $logger, $encoderFactory);
$buffer = new \DdTrace\Buffer();
$ddTracer = new \DdTrace\Tracer($buffer, $logger, $transport);

$tracer = new Tracer($ddTracer, $logger);
$tracer->enableDebugLogging();

GlobalTracer::setGlobalTracer($tracer);

$spanContext = GlobalTracer::globalTracer()->extract(
    Propagator::HTTP_HEADERS,
    HttpHeaders::fromRequest($request)
);

usleep(200);

readFromDB($spanContext);

$response = Response::create("", 200);

$response->send();

// This flushes the traces, if the buffer could be persisted, a worker could flush the traces from time to time.
$tracer->tracer()->flushTraces();

function readFromDB(SpanContext $spanContext)
{
    $tracer = GlobalTracer::globalTracer();

    $component = Tag::create("component", "SELECT * FROM test_table");
    $peerService = Tag::create("peer.service", "test_service");

    $span = $tracer->startSpan("Db read", ChildOf::withContext($spanContext), null, $component, $peerService);

    usleep(3000);

    $span->finish();
}