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-10 04:18:36 +00:00
|
|
|
Config::get(Config::JAEGER_SERVICE_NAME)
|
2023-04-09 18:20:35 +00:00
|
|
|
);
|
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:37:17 +00:00
|
|
|
* @param array<string>|array<string, array<string, mixed>> $tags
|
2023-04-09 18:20:35 +00:00
|
|
|
* @param array<string> $args
|
|
|
|
* @return Scope
|
|
|
|
*/
|
2023-04-09 18:37:17 +00:00
|
|
|
private function _start(string $name, array $tags = [], array $args = []): Scope {
|
2023-04-09 17:50:33 +00:00
|
|
|
$tracer = GlobalTracer::get();
|
2023-04-09 18:20:35 +00:00
|
|
|
|
2023-04-09 18:37:17 +00:00
|
|
|
$tags['args'] = json_encode($args);
|
2023-04-09 18:20:35 +00:00
|
|
|
|
2023-04-09 18:37:17 +00:00
|
|
|
return $tracer->startActiveSpan($name, ['tags' => $tags]);
|
2023-04-09 17:50:33 +00:00
|
|
|
}
|
|
|
|
|
2023-04-09 18:20:35 +00:00
|
|
|
/**
|
|
|
|
* @param string $name
|
2023-04-09 18:37:17 +00:00
|
|
|
* @param array<string>|array<string, array<string, mixed>> $tags
|
2023-04-09 18:20:35 +00:00
|
|
|
* @param array<string> $args
|
|
|
|
* @return Scope
|
|
|
|
*/
|
2023-04-09 18:37:17 +00:00
|
|
|
public static function start(string $name, array $tags = [], array $args = []) : Scope {
|
|
|
|
return self::get_instance()->_start($name, $tags, $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;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|