ttrss/classes/tracer.php

70 lines
1.6 KiB
PHP
Raw Normal View History

2023-04-09 17:50:33 +00:00
<?php
use OpenTracing\GlobalTracer;
use OpenTracing\Scope;
class Tracer {
/** @var Tracer $instance */
2023-04-09 17:50:33 +00:00
private static $instance;
public function __construct() {
$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
],
'tt-rss'
);
2023-04-09 17:50:33 +00:00
$config->initializeTracer();
2023-04-09 17:50:33 +00:00
register_shutdown_function(function() {
$tracer = GlobalTracer::get();
$tracer->flush();
});
}
2023-04-09 17:50:33 +00:00
}
/**
* @param string $name
2023-04-09 18:29:16 +00:00
* @param array<string>|array<string, array<string, mixed>> $options
* @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();
$options['tags']['args'] = json_encode($args);
2023-04-09 17:50:33 +00:00
return $tracer->startActiveSpan($name, $options);
}
/**
* @param string $name
2023-04-09 18:29:16 +00:00
* @param array<string>|array<string, array<string, mixed>> $options
* @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;
}
}