GithubHelp home page GithubHelp logo

reimertz / opentracing-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jcchavezs/opentracing-php

0.0 3.0 0.0 29 KB

PHP library for the OpenTracing's API

License: GNU General Public License v3.0

Makefile 0.29% PHP 99.71%

opentracing-php's Introduction

OpenTracing API for PHP

Build Status

PHP library for the OpenTracing's API.

Required Reading

In order to understand the library, one must first be familiar with the OpenTracing project and specification more specifically.

API overview for those adding instrumentation

When consuming this library one really only need to worry about a couple of key abstractions: the Tracer::startSpan method, the Span interface, and binding a Tracer at bootstrap time. Here are code snippets demonstrating some important use cases:

Singleton initialization

The simplest starting point is to set the global tracer. As early as possible, do:

    use OpenTracing\GlobalTracer;
    use OtherOpenTracingImplementation\Tracer;
    
    GlobalTracer::setGlobalTracer(Tracer::create());

Note that the GlobalTracer is a singleton itself but all the rest public methods are static so it can be perfectly injected on any DI approach.

Creating a Span given an existing Request

To start a new Span, you can use the StartSpanFromContext method.

    use Psr\Http\Message\RequestInterface;

    ...

    $spanContext = GlobalTracer::globalTracer()->extract(
        Propagator::HTTP_HEADERS,
        HttpHeaders::withHeaders($request->getHeaders())
    );
    
    function doSomething(SpanContext $spanContext, ...) {
        ...
        
        $span = GlobalTracer::globalTracer()->startSpan('operation_name', ChildOf::withContext($spanContext), null);
        
        ...
        
        $span->logFields(
            Log::asString('event', 'soft error'),
            Log::asString('type', 'cache timeout'),
            Log::asInt('waited.millis', 1500))        
        )
        
        $span->finish();
    }

Starting an empty trace by creating a "root span"

It's always possible to create a "root" Span with no parent or other causal reference.

    $span = $tracer->startSpan('operation_name');
    ...
    $span->finish();

Creating a (child) Span given an existing (parent) Span

    use OpenTracing\SpanReference\ChildOf;

    function xyz(Span $parentSpan, ...) {
        ...
        $span = GlobalTracer::globalTracer()->startSpan(
            'operation_name',
            ChildOf::withContext($span->context())
        );
        
        $span->finish();
        ...
    }

Serializing to the wire

    use OpenTracing\Carriers\HttpHeaders as HttpHeadersCarrier;
    use OpenTracing\Context;
    use OpenTracing\GlobalTracer;
    
    ...
    
    $tracer = GlobalTracer::globalTracer(); 
    
    $spanContext = $tracer->extract(
        Propagator::HTTP_HEADERS,
        HttpHeaders::withHeaders($request->getHeaders())
    );
    
    try {
        $span = $tracer->startSpan('operation_name', ChildOf::withContext($spanContext), null);

        $client = new GuzzleHttp\Client;
        
        $request = new \GuzzleHttp\Psr7\Request('GET', 'http://myservice');
        
        $tracer->inject(
            $span->context(),
            Propagator::HTTP_HEADERS,
            HttpHeadersCarrier::withHeaders($request->getHeaders())
        )

        $client->send($request);
        ...
    } catch (Exception $e) {
        ...
    }
    ...        

opentracing-php's People

Contributors

jcchavezs avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.