jonahgeorge/jaeger-client-php

PHP OpenTracing API的Jaeger绑定

v1.5.0 2023-08-26 09:30 UTC

README

Build Status PHP version

PHP OpenTracing API的Jaeger绑定

这是一个客户端库,可用于对PHP应用程序进行分布式跟踪收集,并将这些跟踪发送到Jaeger。有关更多详细信息,请参阅OpenTracing PHP API

贡献和开发

请参阅CONTRIBUTING.md

安装

Jaeger客户端可以通过Composer安装

composer require jonahgeorge/jaeger-client-php

入门指南

<?php

require_once 'vendor/autoload.php';

use Jaeger\Config;
use OpenTracing\GlobalTracer;

$config = new Config(
    [
        'sampler' => [
            'type' => Jaeger\SAMPLER_TYPE_CONST,
            'param' => true,
        ],
        'logging' => true,
    ],
    'your-app-name'
);
$config->initializeTracer();

$tracer = GlobalTracer::get();

$scope = $tracer->startActiveSpan('TestSpan', []);
$scope->close();

$tracer->flush();

采样器

支持的采样器列表,有关采样器的更多信息,请参阅Jaeger采样指南。

常量采样器

此采样器要么采样所有内容,要么不采样。

配置
'sampler' => [
    'type' => Jaeger\SAMPLER_TYPE_CONST,
    'param' => true, // boolean wheter to trace or not
],

概率采样器

此采样器以给定的速率采样请求。

配置
'sampler' => [
    'type' => Jaeger\SAMPLER_TYPE_PROBABILISTIC,
    'param' => 0.5, // float [0.0, 1.0]
],

速率限制采样器

每秒采样指定数量的跟踪(请求)。

要求
  • psr/cache PSR-6缓存组件,用于在请求之间存储和检索采样器状态。缓存组件通过其构造函数传递给Jaeger\Config
  • hrtime()函数,可以检索纳秒级的时间。您需要PHP 7.3或PECL/hrtime扩展。
配置
'sampler' => [
    'type' => Jaeger\SAMPLER_TYPE_RATE_LIMITING,
    'param' => 100 // integer maximum number of traces per second,
    'cache' => [
        'currentBalanceKey' => 'rate.currentBalance' // string
        'lastTickKey' => 'rate.lastTick' // string
    ]
],

派发模式

库支持3种将数据发送到Jaeger Agent的方式

  1. Zipkin.thrift通过紧凑协议(套接字-UDP)-默认
  2. Jaeger.thrift通过二进制协议(套接字-UDP)
  3. Jaeger.thrift通过二进制协议(HTTP)

如果您想启用"Jaeger.thrift通过二进制协议",则需要设置dispatch_mode配置选项或JAEGER_DISPATCH_MODE环境变量。

dispatch_mode的允许值

  • jaeger_over_binary_udp
  • jaeger_over_binary_http
  • zipkin_over_compact_udp

有3个常量可用,因此最好使用它们

class Config
{
    const ZIPKIN_OVER_COMPACT_UDP   = "zipkin_over_compact_udp";
    const JAEGER_OVER_BINARY_UDP    = "jaeger_over_binary_udp";
    const JAEGER_OVER_BINARY_HTTP   = "jaeger_over_binary_http";
    ...
}

带有自定义dispatch_mode的可能配置如下

// config.php

use Jaeger\Config;

return [
    'sampler' => [
        'type' => Jaeger\SAMPLER_TYPE_CONST,
        'param' => true,
    ],
    'logging' => true,
    "tags" => [
        // process. prefix works only with JAEGER_OVER_HTTP, JAEGER_OVER_BINARY
        // otherwise it will be shown as simple global tag
        "process.process-tag-key-1" => "process-value-1", // all tags with `process.` prefix goes to process section
        "process.process-tag-key-2" => "process-value-2", // all tags with `process.` prefix goes to process section
        "global-tag-key-1" => "global-tag-value-1", // this tag will be appended to all spans
        "global-tag-key-2" => "global-tag-value-2", // this tag will be appended to all spans
    ],
    "local_agent" => [
        "reporting_host" => "localhost", 
//        You can override port by setting local_agent.reporting_port value   
        "reporting_port" => 6832
    ],
//     Different ways to send data to Jaeger. Config::ZIPKIN_OVER_COMPACT - default):
    'dispatch_mode' => Config::JAEGER_OVER_BINARY_UDP,
];

完整的示例可以在examples目录中查看。

默认情况下,对于每个dispatch_mode,都有一个默认的reporting_port配置值。下面是默认值表

IPv6

如果您需要IPv6支持,则需要设置ip_version配置变量。默认情况下,使用IPv4。有一个别名Config::IP_VERSION,您可以用它作为原始ip_version的替代。

示例

use Jaeger\Config;

$config = new Config(
    [
        "ip_version" => Config::IPV6, // <-- or use Config::IP_VERSION constant
        'logging' => true,
        'dispatch_mode' => Config::JAEGER_OVER_BINARY_UDP,
    ],
    'serviceNameExample',
);
$config->initializeTracer();

use Jaeger\Config;

$config = new Config(
    [
        Config::IP_VERSION => Config::IPV6, // <--
        'logging' => true,
        'dispatch_mode' => Config::JAEGER_OVER_BINARY_UDP,
    ],
    'serviceNameExample',
);
$config->initializeTracer();

测试

测试位于tests目录中。请参阅tests/README.md

路线图

许可证

MIT许可证.