2023-04-09 17:50:33 +00:00
|
|
|
<?php
|
|
|
|
use OpenTracing\GlobalTracer;
|
|
|
|
use OpenTracing\Scope;
|
|
|
|
|
|
|
|
class Tracer {
|
2023-04-09 18:20:35 +00:00
|
|
|
/** @var Tracer $instance */
|
2023-04-09 17:50:33 +00:00
|
|
|
private static $instance;
|
|
|
|
|
|
|
|
public function __construct() {
|
2023-04-09 18:20:35 +00:00
|
|
|
$jaeger_host = Config::get(Config::JAEGER_REPORTING_HOST);
|
|
|
|
|
|
|
|
if ($jaeger_host) {
|
|
|
|
$config = new \Jaeger\Config(
|
|
|
|
[
|
|
|
|
'sampler' => [
|
|
|
|
'type' => \Jaeger\SAMPLER_TYPE_CONST,
|
|
|
|
'param' => true,
|
|
|
|
],
|
|
|
|
'logging' => true,
|
|
|
|
"local_agent" => [
|
|
|
|
"reporting_host" => $jaeger_host,
|
|
|
|
"reporting_port" => 6832
|
|
|
|
],
|
|
|
|
'dispatch_mode' => \Jaeger\Config::JAEGER_OVER_BINARY_UDP,
|
2023-04-09 17:50:33 +00:00
|
|
|
],
|
2023-04-09 18:20:35 +00:00
|
|
|
'tt-rss'
|
|
|
|
);
|
2023-04-09 17:50:33 +00:00
|
|
|
|
2023-04-09 18:20:35 +00:00
|
|
|
$config->initializeTracer();
|
2023-04-09 17:50:33 +00:00
|
|
|
|
2023-04-09 18:20:35 +00:00
|
|
|
register_shutdown_function(function() {
|
|
|
|
$tracer = GlobalTracer::get();
|
|
|
|
$tracer->flush();
|
|
|
|
});
|
|
|
|
}
|
2023-04-09 17:50:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 18:20:35 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
2023-04-09 18:29:16 +00:00
|
|
|
* @param array<string>|array<string, array<string, mixed>> $options
|
2023-04-09 18:20:35 +00:00
|
|
|
* @param array<string> $args
|
|
|
|
* @return Scope
|
|
|
|
*/
|
|
|
|
private function _start(string $name, array $options = [], array $args = []): Scope {
|
2023-04-09 17:50:33 +00:00
|
|
|
$tracer = GlobalTracer::get();
|
2023-04-09 18:20:35 +00:00
|
|
|
|
|
|
|
$options['tags']['args'] = json_encode($args);
|
|
|
|
|
2023-04-09 17:50:33 +00:00
|
|
|
return $tracer->startActiveSpan($name, $options);
|
|
|
|
}
|
|
|
|
|
2023-04-09 18:20:35 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
2023-04-09 18:29:16 +00:00
|
|
|
* @param array<string>|array<string, array<string, mixed>> $options
|
2023-04-09 18:20:35 +00:00
|
|
|
* @param array<string> $args
|
|
|
|
* @return Scope
|
|
|
|
*/
|
|
|
|
public static function start(string $name, array $options = [], array $args = []) : Scope {
|
2023-04-09 18:29:16 +00:00
|
|
|
return self::get_instance()->_start($name, $options, $args);
|
2023-04-09 17:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static function get_instance() : Tracer {
|
|
|
|
if (self::$instance == null)
|
|
|
|
self::$instance = new self();
|
|
|
|
|
|
|
|
return self::$instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|