|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Spiral\Telemetry\Config; |
|
6
|
|
|
|
|
7
|
|
|
use Spiral\Core\Container\Autowire; |
|
8
|
|
|
use Spiral\Core\InjectableConfig; |
|
9
|
|
|
use Spiral\Telemetry\Exception\InvalidArgumentException; |
|
10
|
|
|
use Spiral\Telemetry\TracerFactoryInterface; |
|
11
|
|
|
|
|
12
|
|
|
final class TelemetryConfig extends InjectableConfig |
|
13
|
|
|
{ |
|
14
|
|
|
public const CONFIG = 'telemetry'; |
|
15
|
|
|
|
|
16
|
|
|
protected array $config = [ |
|
17
|
|
|
'default' => 'null', |
|
18
|
|
|
'drivers' => [], |
|
19
|
|
|
]; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Get default trace driver |
|
23
|
|
|
* |
|
24
|
|
|
* @throws InvalidArgumentException |
|
25
|
|
|
*/ |
|
26
|
263 |
|
public function getDefaultDriver(): string |
|
27
|
|
|
{ |
|
28
|
263 |
|
if (!\is_string($this->config['default'])) { |
|
29
|
|
|
throw new InvalidArgumentException('Default trace driver config value must be a string'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
263 |
|
return $this->config['default']; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param non-empty-string $name |
|
|
|
|
|
|
37
|
|
|
* @return class-string<TracerFactoryInterface>|Autowire|TracerFactoryInterface |
|
|
|
|
|
|
38
|
|
|
* @throws InvalidArgumentException |
|
39
|
|
|
*/ |
|
40
|
266 |
|
public function getDriverConfig(string $name): string|Autowire|TracerFactoryInterface |
|
41
|
|
|
{ |
|
42
|
266 |
|
if (!isset($this->config['drivers'][$name])) { |
|
43
|
1 |
|
throw new InvalidArgumentException( |
|
44
|
1 |
|
\sprintf('Config for telemetry driver `%s` is not defined.', $name) |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
265 |
|
$driver = $this->config['drivers'][$name]; |
|
49
|
|
|
|
|
50
|
265 |
|
if ($driver instanceof TracerFactoryInterface) { |
|
51
|
1 |
|
return $driver; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
264 |
|
if (!\is_string($driver) && !$driver instanceof Autowire) { |
|
55
|
|
|
throw new InvalidArgumentException( |
|
56
|
|
|
\sprintf('Trace type value for `%s` must be a string or %s', $name, Autowire::class) |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
264 |
|
return $driver; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|