-
Notifications
You must be signed in to change notification settings - Fork 6
/
SdkBuilder.php
117 lines (97 loc) · 3.79 KB
/
SdkBuilder.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK;
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\API\Logs\EventLoggerProviderInterface;
use OpenTelemetry\API\Logs\NoopEventLoggerProvider;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\Propagation\NoopTextMapPropagator;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\Context\ScopeInterface;
use OpenTelemetry\SDK\Common\Util\ShutdownHandler;
use OpenTelemetry\SDK\Logs\LoggerProviderInterface;
use OpenTelemetry\SDK\Logs\NoopLoggerProvider;
use OpenTelemetry\SDK\Metrics\MeterProviderInterface;
use OpenTelemetry\SDK\Metrics\NoopMeterProvider;
use OpenTelemetry\SDK\Trace\NoopTracerProvider;
use OpenTelemetry\SDK\Trace\TracerProviderInterface;
class SdkBuilder
{
private ?TracerProviderInterface $tracerProvider = null;
private ?MeterProviderInterface $meterProvider = null;
private ?LoggerProviderInterface $loggerProvider = null;
private ?EventLoggerProviderInterface $eventLoggerProvider = null;
private ?TextMapPropagatorInterface $propagator = null;
private bool $autoShutdown = false;
/**
* Automatically shut down providers on process completion. If not set, the user is responsible for calling `shutdown`.
*/
public function setAutoShutdown(bool $shutdown): self
{
$this->autoShutdown = $shutdown;
return $this;
}
public function setTracerProvider(TracerProviderInterface $provider): self
{
$this->tracerProvider = $provider;
return $this;
}
public function setMeterProvider(MeterProviderInterface $meterProvider): self
{
$this->meterProvider = $meterProvider;
return $this;
}
public function setLoggerProvider(LoggerProviderInterface $loggerProvider): self
{
$this->loggerProvider = $loggerProvider;
return $this;
}
/**
* @deprecated
*/
public function setEventLoggerProvider(EventLoggerProviderInterface $eventLoggerProvider): self
{
$this->eventLoggerProvider = $eventLoggerProvider;
return $this;
}
public function setPropagator(TextMapPropagatorInterface $propagator): self
{
$this->propagator = $propagator;
return $this;
}
public function build(): Sdk
{
$tracerProvider = $this->tracerProvider ?? new NoopTracerProvider();
$meterProvider = $this->meterProvider ?? new NoopMeterProvider();
$loggerProvider = $this->loggerProvider ?? new NoopLoggerProvider();
$eventLoggerProvider = $this->eventLoggerProvider ?? new NoopEventLoggerProvider();
if ($this->autoShutdown) {
// rector rule disabled in config, because ShutdownHandler::register() does not keep a strong reference to $this
ShutdownHandler::register($tracerProvider->shutdown(...));
ShutdownHandler::register($meterProvider->shutdown(...));
ShutdownHandler::register($loggerProvider->shutdown(...));
}
return new Sdk(
$tracerProvider,
$meterProvider,
$loggerProvider,
$eventLoggerProvider,
$this->propagator ?? NoopTextMapPropagator::getInstance(),
);
}
/**
* @phan-suppress PhanDeprecatedFunction
*/
public function buildAndRegisterGlobal(): ScopeInterface
{
$sdk = $this->build();
$context = Configurator::create()
->withPropagator($sdk->getPropagator())
->withTracerProvider($sdk->getTracerProvider())
->withMeterProvider($sdk->getMeterProvider())
->withLoggerProvider($sdk->getLoggerProvider())
->withEventLoggerProvider($sdk->getEventLoggerProvider())
->storeInContext();
return Context::storage()->attach($context);
}
}