taisph/laravel-opentracing

此包已废弃,不再维护。没有建议的替代包。

1.0.0-rc.3 2021-07-29 08:12 UTC

This package is auto-updated.

Last update: 2022-12-29 03:39:00 UTC


README

Total Downloads Latest Stable Version StyleCI Build Status Coverage Status

为Laravel提供OpenTracing API的参考实现,包括用于应用程序日志目的的无服务器本地跟踪器。

有关更多信息,请参阅OpenTracing

支持的客户端

当前支持的客户端

  • 本地:主要用于向日志添加跟踪ID的No-op跟踪器。
  • Jaeger:开源、端到端分布式跟踪。请参阅Jaeger和Jonah George的Jaeger Client PHP

请注意,目前需要Jaeger Client PHP的修补版本来保留PHP 5.6支持。如果您的应用程序需要该功能,请在composer.json文件中的repositories部分添加以下配置。

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

安装

使用以下命令安装最新版本

composer require taisph/laravel-opentracing

如果您想通过运行以下命令更改默认配置文件,请将其复制到您的应用程序中。请注意,如果文件已存在,您必须使用--force

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

基本用法

示例设置

示例bootstrap/app.php文件

<?php

// Create the application.
$app = new \Illuminate\Foundation\Application(realpath(__DIR__ . '/../'));

// Bind important interfaces.
// ...

// Register important providers.
$app->register(\LaravelOpenTracing\TracingServiceProvider::class);

// Enable tracing span context in log messages.
$app->configureMonologUsing(function (\Monolog\Logger $logger) {
    $logger->pushProcessor(new \LaravelOpenTracing\Log\Processor\LocalTracerProcessor());
});

// Return the application.
return $app;

跟踪

要跟踪应用程序中的特定进程,您可以将该进程包裹在一个跟踪闭包中,如下所示。这将负责启动新的跟踪span并在闭包返回或抛出异常时关闭它。

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

嵌套跟踪也是可能的,如下所示。它将自动处理span的父子关系。

function a() {
    // We don't care about tracing this specifically.
    doSomething();

    app(\LaravelOpenTracing\TracingService::class)->trace(
        'app.do_something_else',
        function () {
            doSomethingElse();
        }
    );
}

app(\LaravelOpenTracing\TracingService::class)->trace(
    'app.do_stuff',
    function () {
        a();
    }
);

如果您想向span添加上下文信息或标签,可以像下面这样操作。

$title = 'Make more coffee';

$item = app(\LaravelOpenTracing\TracingService::class)->trace(
    'todo.store_item',
    function () use ($title) {
        return \App\Models\TodoListItem::create(['title' => $title]);
    },
    ['tags' => ['title' => $title]]
);

跟踪作业

配置您的调度器,使作业通过跟踪管道传输。这与中间件类似,但仅针对作业。

<?php

app(\Illuminate\Bus\Dispatcher::class)->pipeThrough([
    \LaravelOpenTracing\TracingJobPipe::class,
]);

跨服务边界跟踪

跟踪上下文可以轻松地在服务之间使用。如果您的应用程序启动了一个跟踪上下文,则该上下文可以携带HTTP到另一个具有OpenTracing兼容实现的服务的另一个服务。

要自动接受来自其他服务的跟踪上下文,请将跟踪中间件添加到您的应用程序中,如下所示将其添加到app/Http/Kernel.php文件中。

<?php

class Kernel extends HttpKernel
{
    protected $middleware = [
        \LaravelOpenTracing\Http\Middleware\Tracing::class,
    ];
}

假设您的应用程序使用GuzzleHttp向外部服务发送请求,您可以在创建客户端时使用提供的跟踪处理程序,如下所示。这将自动将必要的跟踪上下文头信息与HTTP请求发送到外部服务。

<?php

new \GuzzleHttp\Client(
    [
        'handler' => new \LaravelOpenTracing\TracingHandlerStack(),
        'headers' => [
            'cache-control' => 'no-cache',
            'content-type' => 'application/json',
        ],
        'base_uri' => 'https:///api/myservice',
        'http_errors' => false
    ]
);

测试

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'

关于

要求

  • PHP 5.6 或更高版本。

贡献

错误和功能请求在 GitHub 上跟踪。

许可证

Laravel OpenTracing 使用 Apache-2.0 许可证。请参阅 LICENSE 文件以获取详细信息。