PHPackages                             moolex/opentracing - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. moolex/opentracing

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

moolex/opentracing
==================

OpenTracing API for PHP

0.9.171115(7y ago)0712↓50%1MITPHPPHP ^5.6||^7.0

Since Aug 23Pushed 7y ago1 watchersCompare

[ Source](https://github.com/uuland/opentracing-php)[ Packagist](https://packagist.org/packages/moolex/opentracing)[ RSS](/packages/moolex-opentracing/feed)WikiDiscussions master Synced 4w ago

READMEChangelog (1)Dependencies (2)Versions (2)Used By (1)

OpenTracing API for PHP
=======================

[](#opentracing-api-for-php)

[![Build Status](https://camo.githubusercontent.com/8a858e6cfde783491d6c90b51ac1f78162d1855e028d498d3f0729e87199264b/68747470733a2f2f7472617669732d63692e6f72672f6f70656e74726163696e672f6f70656e74726163696e672d7068702e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/opentracing/opentracing-php)[![OpenTracing Badge](https://camo.githubusercontent.com/0c34d42089573bfc38911b30f4fd5c7d3ef20dd05d0849b390643ccbf19695da/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f70656e54726163696e672d656e61626c65642d626c75652e737667)](http://opentracing.io)[![Total Downloads](https://camo.githubusercontent.com/21ea06066461c8ad0742b92d10b1561fbda57a9acc5f6105b4aefe2d12bea9ce/68747470733a2f2f706f7365722e707567782e6f72672f6f70656e74726163696e672f6f70656e74726163696e672f646f776e6c6f616473)](https://packagist.org/packages/opentracing/opentracing)[![Minimum PHP Version](https://camo.githubusercontent.com/4cbdbfeca62402b9ca3d48503f2bf66fc9809569bcd6de47196d39fecff71e72/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d253345253344253230352e362d3838393242462e737667)](https://php.net/)[![License](https://camo.githubusercontent.com/3f02e3766da2fe9e0d828f504c579d1356a6caef794571ac0afce34b9dd7d7ef/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6f70656e74726163696e672f6f70656e74726163696e672e737667)](https://github.com/opentracing/opentracing-php/blob/master/LICENSE)[![Join the chat at https://gitter.im/opentracing/opentracing-php](https://camo.githubusercontent.com/6cf58a3299c9912ddd5fa0216c18c51b0f0a8ec54e333c2f78d6ec9ad7f1e233/68747470733a2f2f6261646765732e6769747465722e696d2f6f70656e74726163696e672f6f70656e74726163696e672d7068702e737667)](https://gitter.im/opentracing/opentracing-php?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

PHP library for the OpenTracing's API.

Required Reading
----------------

[](#required-reading)

In order to understand the library, one must first be familiar with the [OpenTracing project](http://opentracing.io) and [specification](http://opentracing.io/documentation/pages/spec.html) more specifically.

Installation
------------

[](#installation)

OpenTracing-PHP can be installed via Composer:

```
composer require opentracing/opentracing
```

Usage
-----

[](#usage)

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

[](#singleton-initialization)

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

```
    use OpenTracing\GlobalTracer;
    use AnOpenTracingImplementation\MyTracer;

    GlobalTracer::set(new MyTracer());
```

### Creating a Span given an existing Request

[](#creating-a-span-given-an-existing-request)

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

```
    use OpenTracing\Formats;
    use OpenTracing\GlobalTracer;

    ...

    $spanContext = GlobalTracer::get()->extract(
        Formats\HTTP_HEADERS,
        $request->getHeaders()
    );

    function doSomething(SpanContext $spanContext, ...) {
        ...

        $span = GlobalTracer::get()->startSpan('my_span', [
        	'child_of' => $spanContext,
        ]);

        ...

        $span->log([
            'event' => 'soft error',
            'type' => 'cache timeout',
            'waiter.millis' => 1500,
        ])

        $span->finish();
    }
```

### Starting an empty trace by creating a "root span"

[](#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('my_span');
    ...
    $span->finish();
```

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

[](#creating-a-child-span-given-an-existing-parent-span)

```
    function xyz(Span $parentSpan, ...) {
        ...
        $span = GlobalTracer::get()->startSpan(
        	'my_span',
        	[
        		'child_of' => $parentSpan,
        	]
        );

        $span->finish();
        ...
    }
```

### Serializing to the wire

[](#serializing-to-the-wire)

```
    use OpenTracing\GlobalTracer;
    use OpenTracing\Formats;

    ...

    $tracer = GlobalTracer::get();

    $spanContext = $tracer->extract(
        Formats\HTTP_HEADERS,
        $request->getHeaders()
    );

    try {
        $span = $tracer->startSpan('my_span', ['child_of' => $spanContext]);

        $client = new GuzzleHttp\Client;

        $headers = [];

        $tracer->inject(
            $span->getContext(),
            Formats\HTTP_HEADERS,
            $headers
        );

        $request = new \GuzzleHttp\Psr7\Request('GET', 'http://myservice', $headers);
        $client->send($request);
        ...
    } catch (\Exception $e) {
        ...
    }
    ...
```

### Deserializing from the wire

[](#deserializing-from-the-wire)

When using http header for context propagation you can use either the `Request` or the `$_SERVER` variable:

```
    use OpenTracing\GlobalTracer;
    use OpenTracing\Formats;

    $request = Request::createFromGlobals();
    $tracer = GlobalTracer::get();
    $spanContext = $tracer->extract(Formats\HTTP_HEADERS, $request->getHeaders());
    $tracer->startSpan('my_span', [
        'child_of' => $spanContext,
    ]);
```

### Flushing Spans

[](#flushing-spans)

PHP as a request scoped language has no simple means to pass the collected spans data to a background process without blocking the main request thread/process. The OpenTracing API makes no assumptions about this, but for PHP that might cause problems for Tracer implementations. This is why the PHP API contains a `flush` method that allows to trigger a span sending out of process.

```
use OpenTracing\GlobalTracer;

// Do application work, buffer spans in memory
$application->run();

fastcgi_finish_request();

$tracer = GlobalTracer::get();
$tracer->flush(); // release buffer to backend
```

This is optional, tracers can decide to immediately send finished spans to a backend. The flush call can be implemented as a NO-OP for these tracers.

### Using Span Options

[](#using-span-options)

Passing options to the pass can be done using either an array or the SpanOptions wrapper object. The following keys are valid:

- `start_time` is a float, int or `\DateTime` representing a timestamp with arbitrary precision.
- `child_of` is an object of type `OpenTracing\SpanContext` or `OpenTracing\Span`.
- `references` is an array of `OpenTracing\Reference`.
- `tags` is an array with string keys and scalar values that represent OpenTracing tags.

```
$span = $tracer->createSpan('my_span', [
    'child_of' => $spanContext,
    'tags' => ['foo' => 'bar'],
    'start_time' => time(),
]);
```

### Propagation Formats

[](#propagation-formats)

The propagation formats should be implemented consistently across all tracers. If you want to implement your own format, then don't reuse the existing constants. Tracers will throw an exception if the requested format is not handled by them.

- `Tracer::FORMAT_TEXT_MAP` should represents the span context as a key value map. There is no assumption about the semantics where the context is coming from and sent to.
- `Tracer::FORMAT_HTTP_HEADERS` should represent the span context as HTTP header lines in an array list. For two context details "Span-Id" and "Trace-Id", the result would be `['Span-Id: abc123', 'Trace-Id: def456']`. This definition can be passed directly to `curl` and `file_get_contents`.
- `Tracer::FORMAT_BINARY` makes no assumptions about the data format other than it is proprietary and each Tracer can handle it as it wants.

Coding Style
------------

[](#coding-style)

Opentracing PHP follows the [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)coding standard and the [PSR-4](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader.md) autoloading standard.

###  Health Score

24

—

LowBetter than 32% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2816d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/ee5620a146ee71289cb4ad63d06cdfcbfd679eb02b446f78bd772df8342b43a0?d=identicon)[moyo](/maintainers/moyo)

---

Top Contributors

[![moolex](https://avatars.githubusercontent.com/u/1132321?v=4)](https://github.com/moolex "moolex (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/moolex-opentracing/health.svg)

```
[![Health](https://phpackages.com/badges/moolex-opentracing/health.svg)](https://phpackages.com/packages/moolex-opentracing)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
