summaryrefslogtreecommitdiff
path: root/classes/tracer.php
blob: 5a23dfeba0b1420a80c63fcb891d6188699805d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
use OpenTracing\GlobalTracer;
use OpenTracing\Scope;

class Tracer {
	/** @var Tracer $instance */
	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,
				],
				Config::get(Config::JAEGER_SERVICE_NAME)
			);

			$config->initializeTracer();

			register_shutdown_function(function() {
				$tracer = GlobalTracer::get();
				$tracer->flush();
			});
		}
	}

	/**
	 * @param string $name
	 * @param array<string>|array<string, array<string, mixed>> $tags
	 * @param array<string> $args
	 * @return Scope
	 */
	private function _start(string $name, array $tags = [], array $args = []): Scope {
		$tracer = GlobalTracer::get();

		$tags['args'] = json_encode($args);

		return $tracer->startActiveSpan($name, ['tags' => $tags]);
	}

	/**
	 * @param string $name
	 * @param array<string>|array<string, array<string, mixed>> $tags
	 * @param array<string> $args
	 * @return Scope
	 */
	public static function start(string $name, array $tags = [], array $args = []) : Scope {
		return self::get_instance()->_start($name, $tags, $args);
	}

	public static function get_instance() : Tracer {
		if (self::$instance == null)
			self::$instance = new self();

		return self::$instance;
	}

}