|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Telemetry\Bootloader; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
|
8
|
|
|
use Spiral\Boot\Bootloader\Bootloader; |
|
9
|
|
|
use Spiral\Boot\EnvironmentInterface; |
|
10
|
|
|
use Spiral\Config\ConfiguratorInterface; |
|
11
|
|
|
use Spiral\Config\Patch\Append; |
|
12
|
|
|
use Spiral\Core\BinderInterface; |
|
13
|
|
|
use Spiral\Core\Config\Proxy; |
|
14
|
|
|
use Spiral\Core\Container\Autowire; |
|
15
|
|
|
use Spiral\Telemetry\Clock\SystemClock; |
|
16
|
|
|
use Spiral\Telemetry\ClockInterface; |
|
17
|
|
|
use Spiral\Telemetry\Config\TelemetryConfig; |
|
18
|
|
|
use Spiral\Telemetry\ConfigTracerFactoryProvider; |
|
19
|
|
|
use Spiral\Telemetry\Exception\TracerException; |
|
20
|
|
|
use Spiral\Telemetry\LogTracerFactory; |
|
21
|
|
|
use Spiral\Telemetry\NullTracerFactory; |
|
22
|
|
|
use Spiral\Telemetry\TracerFactoryInterface; |
|
23
|
|
|
use Spiral\Telemetry\TracerFactoryProviderInterface; |
|
24
|
|
|
use Spiral\Telemetry\TracerInterface; |
|
25
|
|
|
|
|
26
|
|
|
final class TelemetryBootloader extends Bootloader |
|
27
|
|
|
{ |
|
28
|
|
|
protected const SINGLETONS = [ |
|
29
|
|
|
TracerFactoryInterface::class => [self::class, 'initFactory'], |
|
30
|
|
|
TracerFactoryProviderInterface::class => ConfigTracerFactoryProvider::class, |
|
31
|
|
|
ClockInterface::class => SystemClock::class, |
|
32
|
|
|
]; |
|
33
|
|
|
|
|
34
|
387 |
|
public function __construct( |
|
35
|
|
|
private readonly ConfiguratorInterface $config, |
|
36
|
387 |
|
) {} |
|
37
|
|
|
|
|
38
|
387 |
|
public function init(BinderInterface $binder, EnvironmentInterface $env): void |
|
39
|
|
|
{ |
|
40
|
387 |
|
$binder->bind(TracerInterface::class, new Proxy( |
|
41
|
387 |
|
TracerInterface::class, |
|
42
|
387 |
|
false, |
|
43
|
387 |
|
static fn(ContainerInterface $container): TracerInterface => $container |
|
44
|
387 |
|
->get(TracerFactoryInterface::class) |
|
45
|
387 |
|
->make(), |
|
46
|
387 |
|
)); |
|
47
|
387 |
|
$this->initConfig($env); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param class-string<TracerFactoryInterface>|TracerFactoryInterface|Autowire $driver |
|
|
|
|
|
|
52
|
|
|
*/ |
|
53
|
1 |
|
public function registerTracer(string $name, string|TracerFactoryInterface|Autowire $driver): void |
|
54
|
|
|
{ |
|
55
|
1 |
|
$this->config->modify( |
|
56
|
1 |
|
TelemetryConfig::CONFIG, |
|
57
|
1 |
|
new Append('drivers', $name, $driver), |
|
58
|
1 |
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @throws TracerException |
|
63
|
|
|
*/ |
|
64
|
66 |
|
public function initFactory( |
|
65
|
|
|
TracerFactoryProviderInterface $tracerProvider, |
|
66
|
|
|
): TracerFactoryInterface { |
|
67
|
66 |
|
return $tracerProvider->getTracerFactory(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
387 |
|
private function initConfig(EnvironmentInterface $env): void |
|
71
|
|
|
{ |
|
72
|
387 |
|
$this->config->setDefaults( |
|
73
|
387 |
|
TelemetryConfig::CONFIG, |
|
74
|
387 |
|
[ |
|
75
|
387 |
|
'default' => $env->get('TELEMETRY_DRIVER', 'null'), |
|
76
|
387 |
|
'drivers' => [ |
|
77
|
387 |
|
'null' => NullTracerFactory::class, |
|
78
|
387 |
|
'log' => LogTracerFactory::class, |
|
79
|
387 |
|
], |
|
80
|
387 |
|
], |
|
81
|
387 |
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|