项目作者: taisph

项目描述 :
OpenTracing for Laravel
高级语言: PHP
项目地址: git://github.com/taisph/laravel-opentracing.git
创建时间: 2018-07-03T13:58:57Z
项目社区:https://github.com/taisph/laravel-opentracing

开源协议:Apache License 2.0

下载


OpenTracing for Laravel

Total Downloads
Latest Stable Version
StyleCI
Build Status
Coverage Status

Reference implementation of the OpenTracing API for Laravel including a server-less local tracer for application
logging purposes.

See OpenTracing for more information.

Supported Clients

Currently supported clients:

  • Local: No-op tracer used mainly for adding trace ids to logs.
  • Jaeger: open source, end-to-end distributed tracing. See Jaeger and
    Jonah George’s Jaeger Client PHP.

Note that a patched version of Jaeger Client PHP is currently required to retain PHP 5.6 support. If you need that in
your application, add the config below to your composer.json file in the repositories section.

  1. {
  2. "type": "vcs",
  3. "url": "https://github.com/taisph/jaeger-client-php"
  4. }

Installation

Install the latest version using:

  1. composer require taisph/laravel-opentracing

Copy the default configuration file to your application if you want to change it by running the command below. Note
that you have to use --force if the file already exists.

  1. php artisan vendor:publish --provider="LaravelOpenTracing\TracingServiceProvider"

Basic Usage

Example setup

Example bootstrap/app.php file:

  1. <?php
  2. // Create the application.
  3. $app = new \Illuminate\Foundation\Application(realpath(__DIR__ . '/../'));
  4. // Bind important interfaces.
  5. // ...
  6. // Register important providers.
  7. $app->register(\LaravelOpenTracing\TracingServiceProvider::class);
  8. // Enable tracing span context in log messages.
  9. $app->configureMonologUsing(function (\Monolog\Logger $logger) {
  10. $logger->pushProcessor(new \LaravelOpenTracing\Log\Processor\LocalTracerProcessor());
  11. });
  12. // Return the application.
  13. return $app;

Tracing

To trace a specific process in your application you can wrap the process in a trace closure like below. This will take
care of starting a new trace span and closing it again when the closure either returns or throws.

  1. $items = app(\LaravelOpenTracing\TracingService::class)->trace(
  2. 'todo.get_list_items',
  3. function () {
  4. return \App\Models\TodoListItem::get();
  5. }
  6. );

Nested traces are also possible like below. It will automatically take care of the span child/parent relations.

  1. function a() {
  2. // We don't care about tracing this specifically.
  3. doSomething();
  4. app(\LaravelOpenTracing\TracingService::class)->trace(
  5. 'app.do_something_else',
  6. function () {
  7. doSomethingElse();
  8. }
  9. );
  10. }
  11. app(\LaravelOpenTracing\TracingService::class)->trace(
  12. 'app.do_stuff',
  13. function () {
  14. a();
  15. }
  16. );

If you want to add context information or tags to your spans, it is possible like below.

  1. $title = 'Make more coffee';
  2. $item = app(\LaravelOpenTracing\TracingService::class)->trace(
  3. 'todo.store_item',
  4. function () use ($title) {
  5. return \App\Models\TodoListItem::create(['title' => $title]);
  6. },
  7. ['tags' => ['title' => $title]]
  8. );

Tracing Jobs

Configure your dispatcher to pipe jobs through the tracing pipe. This is similar to middleware, only for jobs.

  1. <?php
  2. app(\Illuminate\Bus\Dispatcher::class)->pipeThrough([
  3. \LaravelOpenTracing\TracingJobPipe::class,
  4. ]);

Tracing Across Service Boundaries

Trace contexts can easily be used across services. If your application starts a tracing context, that context can be
carried over HTTP to another service with an OpenTracing compatible implementation.

To automatically accept trace contexts from other services, add the tracing middleware to your application by adding it
to your app/Http/Kernel.php file like below:

  1. <?php
  2. class Kernel extends HttpKernel
  3. {
  4. protected $middleware = [
  5. \LaravelOpenTracing\Http\Middleware\Tracing::class,
  6. ];
  7. }

Assuming your application uses GuzzleHttp for sending requests to external services, you can use the provided tracing
handler when creating the client like below. This will automatically send the necessary trace context headers with the
HTTP request to the external service.

  1. <?php
  2. new \GuzzleHttp\Client(
  3. [
  4. 'handler' => new \LaravelOpenTracing\TracingHandlerStack(),
  5. 'headers' => [
  6. 'cache-control' => 'no-cache',
  7. 'content-type' => 'application/json',
  8. ],
  9. 'base_uri' => 'http://localhost/api/myservice',
  10. 'http_errors' => false
  11. ]
  12. );

Testing

docker run —rm -it -v $(pwd):/app php:5.6-cli-alpine /bin/sh -c ‘apk add —no-cache $PHPIZE_DEPS && pecl install xdebug-2.5.5 && cd app && php -dzend_extension=xdebug.so vendor/bin/phpunit’

About

Requirements

  • PHP 5.6 or newer.

Contributions

Bugs and feature requests are tracked on GitHub.

License

OpenTracing for Laravel is licensed under the Apache-2.0 license. See the LICENSE file for details.