2017 © Pedro PelΓ‘ez
 

library zipkin

A Zipkin instrumentation for PHP

image

jcchavezs/zipkin

A Zipkin instrumentation for PHP

  • Wednesday, July 25, 2018
  • by jcchavezs
  • Repository
  • 3 Watchers
  • 49 Stars
  • 10,969 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 12 Forks
  • 12 Open issues
  • 32 Versions
  • 68 % Grown

The README.md

Zipkin PHP

CI Latest Stable Version Coverage Status Minimum PHP Version Total Downloads License, (*1)

Zipkin PHP is the official PHP Tracer implementation for Zipkin, supported by the OpenZipkin community., (*2)

Installation

composer require openzipkin/zipkin

Setup

use Zipkin\Annotation;
use Zipkin\Endpoint;
use Zipkin\Samplers\BinarySampler;
use Zipkin\TracingBuilder;
use Zipkin\Reporters\Http;

// First we create the endpoint that describes our service
$endpoint = Endpoint::create('my_service');

$reporter = new Http(['endpoint_url' => 'http://myzipkin:9411/api/v2/spans']);
$sampler = BinarySampler::createAsAlwaysSample();
$tracing = TracingBuilder::create()
    ->havingLocalEndpoint($endpoint)
    ->havingSampler($sampler)
    ->havingReporter($reporter)
    ->build();

$tracer = $tracing->getTracer();

...

$tracer->flush();

Obs. for a more complete frontend/backend example, check this repository., (*3)

Tracing

The tracer creates and joins spans that model the latency of potentially distributed work. It can employ sampling to reduce overhead in process or to reduce the amount of data sent to Zipkin., (*4)

Spans returned by a tracer report data to Zipkin when finished, or do nothing if unsampled. After starting a span, you can annotate events of interest or add tags containing details or lookup keys., (*5)

Spans have a context which includes trace identifiers that place it at the correct spot in the tree representing the distributed operation., (*6)

Local Tracing

When tracing local code, just run it inside a span, (*7)

$span = $tracer->newTrace();
$span->setName('encode');
$span->start();

try {
  doSomethingExpensive();
} finally {
  $span->finish();
}

In the above example, the span is the root of the trace. In many cases, you will be a part of an existing trace. When this is the case, call newChild instead of newTrace, (*8)

$span = $tracer->newChild($root->getContext());
$span->setName('encode');
$span->start();
try {
  doSomethingExpensive();
} finally {
  $span->finish();
}

Customizing spans

Once you have a span, you can add tags to it, which can be used as lookup keys or details. For example, you might add a tag with your runtime version., (*9)

$span->tag('http.status_code', '200');

RPC tracing

RPC tracing is often done automatically by interceptors. Under the scenes, they add tags and events that relate to their role in an RPC operation., (*10)

Here's an example of a client span:, (*11)

// before you send a request, add metadata that describes the operation
$span = $tracer->newTrace();
$span->setName('get');
$span->setKind(Kind\CLIENT);
$span->tag('http.status_code', '200');
$span->tag(Tags\HTTP_PATH, '/api');
$span->setRemoteEndpoint(Endpoint::create('backend', 127 << 24 | 1, null, 8080));

// when the request is scheduled, start the span
$span->start();

// if you have callbacks for when data is on the wire, note those events
$span->annotate(Annotation\WIRE_SEND);
$span->annotate(Annotation\WIRE_RECV);

// when the response is complete, finish the span
$span->finish();

Sampling

Sampling may be employed to reduce the data collected and reported out of process. When a span isn't sampled, it adds no overhead (noop)., (*12)

Sampling is an up-front decision, meaning that the decision to report data is made at the first operation in a trace, and that decision is propagated downstream., (*13)

By default, there's a global sampler that applies a single rate to all traced operations. Sampler is how you indicate this, and it defaults to trace every request., (*14)

Custom sampling

You may want to apply different policies depending on what the operation is. For example, you might not want to trace requests to static resources such as images, or you might want to trace all requests to a new api., (*15)

Most users will use a framework interceptor which automates this sort of policy. Here's how they might work internally., (*16)

private function newTrace(Request $request) {
  $flags = SamplingFlags::createAsEmpty();
  if (strpos($request->getUri(), '/experimental') === 0) {
    $flags = DefaultSamplingFlags::createAsSampled();
  } else if (strpos($request->getUri(), '/static') === 0) {
    $flags = DefaultSamplingFlags::createAsSampled();
  }
  return $tracer->newTrace($flags);
}

Propagation

Propagation is needed to ensure activity originating from the same root are collected together in the same trace. The most common propagation approach is to copy a trace context from a client sending an RPC request to a server receiving it., (*17)

For example, when an downstream Http call is made, its trace context is sent along with it, encoded as request headers:, (*18)

   Client Span                                                Server Span
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”                                       β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                  β”‚                                       β”‚                  β”‚
β”‚   TraceContext   β”‚           Http Request Headers        β”‚   TraceContext   β”‚
β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”        β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚
β”‚ β”‚ TraceId      β”‚ β”‚          β”‚ X-B3-TraceId      β”‚        β”‚ β”‚ TraceId      β”‚ β”‚
β”‚ β”‚              β”‚ β”‚          β”‚                   β”‚        β”‚ β”‚              β”‚ β”‚
β”‚ β”‚ ParentSpanId β”‚ β”‚ Extract  β”‚ X-B3-ParentSpanId β”‚ Inject β”‚ β”‚ ParentSpanId β”‚ β”‚
β”‚ β”‚              β”œβ”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€>β”‚                   β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”Ό>β”‚              β”‚ β”‚
β”‚ β”‚ SpanId       β”‚ β”‚          β”‚ X-B3-SpanId       β”‚        β”‚ β”‚ SpanId       β”‚ β”‚
β”‚ β”‚              β”‚ β”‚          β”‚                   β”‚        β”‚ β”‚              β”‚ β”‚
β”‚ β”‚ Sampled      β”‚ β”‚          β”‚ X-B3-Sampled      β”‚        β”‚ β”‚ Sampled      β”‚ β”‚
β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜        β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚
β”‚                  β”‚                                       β”‚                  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜                                       β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The names above are from B3 Propagation, which is built-in to Brave and has implementations in many languages and frameworks., (*19)

Most users will use a framework interceptor which automates propagation. Here's how they might work internally., (*20)

Here's what client-side propagation might look like, (*21)

// configure a function that injects a trace context into a request
$injector = $tracing->getPropagation()->getInjector(new RequestHeaders);

// before a request is sent, add the current span's context to it
$injector($span->getContext(), $request);

Here's what server-side propagation might look like, (*22)

// configure a function that extracts the trace context from a request
$extractor = $tracing->getPropagation()->getExtractor(new RequestHeaders);
$extracted = $extractor($request);

$span = $tracer->newChild($extracted);
$span->setKind(Kind\SERVER);

If you aren't using a framework or don't have access to the Request object, you can extract the context from the $_SERVER variable, (*23)

$extractor = $tracing->getPropagation()->getExtractor(new ServerHeaders);
$extracted = $extractor($_SERVER);

Extracting a propagated context

The Extractor reads trace identifiers and sampling status from an incoming request or message. The carrier is usually a request object or headers., (*24)

SamplingFlags|TraceContext is usually only used with $tracer->newChild(extracted), unless you are sharing span IDs between a client and a server., (*25)

Implementing Propagation

Extractor will output a SamplingFlags|TraceContext with one of the following:, (*26)

  • TraceContext if trace and span IDs were present.
  • SamplingFlags if no identifiers were present

Current Span

Zipkin supports a "current span" concept which represents the in-flight operation. Tracer::currentSpan() can be used to add custom tags to a span and Tracer::nextSpan() can be used to create a child of whatever is in-flight., (*27)

A common use case for the current span is to instrument RPC clients. For example:, (*28)

/**
  * This http clients composes an http client using PSR7
  */
class TraceClient implements ClientInterface
{
    public function request($method, $uri = '', array $options = [])
    {
        /* Gets the child Span of the current one */
        $span = $this->tracer->nextSpan();
        $span->setKind(Zipkin\Kind\CLIENT);
        $span->tag(Tags\HTTP_PATH, $uri);

        try {
            $response = $this->client->request($method, $uri, $options);
            $span->tag(Tags\HTTP_STATUS_CODE, (string) $response->getStatusCode());

            return $response;
        catch (Throwable $e) {
            $span->setError($e);
            throw $e;
        } finally {
            $span->finish();
        }
    }
}

Setting a span in scope manually

When writing new instrumentation, it is important to place a span you created in scope as the current span., (*29)

In edge cases, you may need to clear the current span temporarily. For example, launching a task that should not be associated with the current request. To do this, simply pass null to openScope., (*30)

Instrumentation

Tests

Tests can be run by, (*31)

composer test

Whereas static checks can be run by:, (*32)

composer static-check

Reference

The Versions

25/07 2018

dev-master

9999999-dev

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

25/07 2018

dev-build_badge

dev-build_badge

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

24/07 2018

1.3.0

1.3.0.0

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

11/07 2018

1.2.4

1.2.4.0

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

23/06 2018

1.2.3

1.2.3.0

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

31/05 2018

dev-updates_http_test

dev-updates_http_test

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

25/04 2018

dev-lc_description

dev-lc_description

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

18/04 2018

1.2.2

1.2.2.0

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

18/04 2018

dev-adds_in_memory_reporter

dev-adds_in_memory_reporter

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

24/03 2018

dev-adds_packagist_tags

dev-adds_packagist_tags

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

zipkin tracing openzipkin

06/03 2018

dev-adds_small_tweaks

dev-adds_small_tweaks

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

03/03 2018

dev-adds_http_route_tag

dev-adds_http_route_tag

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

01/03 2018

dev-guards_malformed_propagated_context

dev-guards_malformed_propagated_context

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

01/02 2018

dev-removes_try_catch_when_reporting

dev-removes_try_catch_when_reporting

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

18/01 2018
16/01 2018
10/01 2018
04/12 2017

1.0.0-beta4

1.0.0.0-beta4

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

27/11 2017

dev-29_adds_current_trace_context

dev-29_adds_current_trace_context

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

24/11 2017

1.0.0-beta3

1.0.0.0-beta3

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

22/11 2017

dev-add-code-of-conduct-1

dev-add-code-of-conduct-1

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

21/11 2017

1.0.0-beta2

1.0.0.0-beta2

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

15/11 2017

1.0.0-beta1

1.0.0.0-beta1

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

24/10 2017

dev-adds_current_trace_context

dev-adds_current_trace_context

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires

20/10 2017

dev-13_adds_extra_support

dev-13_adds_extra_support

A Zipkin instrumentation for PHP

  Sources   Download

MIT

The Requires

 

The Development Requires