-
Notifications
You must be signed in to change notification settings - Fork 6
/
SdkAutoloader.php
265 lines (234 loc) · 9.79 KB
/
SdkAutoloader.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
<?php
declare(strict_types=1);
namespace OpenTelemetry\SDK;
use Nevay\SPI\ServiceLoader;
use OpenTelemetry\API\Behavior\LogsMessagesTrait;
use OpenTelemetry\API\Configuration\Noop\NoopConfigProperties;
use OpenTelemetry\API\Globals;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Context as InstrumentationContext;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManagerInterface;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation;
use OpenTelemetry\API\Instrumentation\AutoInstrumentation\NoopHookManager;
use OpenTelemetry\API\Instrumentation\Configurator;
use OpenTelemetry\API\Logs\LateBindingLoggerProvider;
use OpenTelemetry\API\Logs\LoggerProviderInterface;
use OpenTelemetry\API\Metrics\LateBindingMeterProvider;
use OpenTelemetry\API\Metrics\MeterProviderInterface;
use OpenTelemetry\API\Trace\LateBindingTracerProvider;
use OpenTelemetry\API\Trace\TracerProviderInterface;
use OpenTelemetry\Config\SDK\Configuration as SdkConfiguration;
use OpenTelemetry\Config\SDK\Instrumentation as SdkInstrumentation;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\Propagation\TextMapPropagatorInterface;
use OpenTelemetry\SDK\Common\Configuration\Configuration;
use OpenTelemetry\SDK\Common\Configuration\Variables;
use OpenTelemetry\SDK\Common\Util\ShutdownHandler;
use OpenTelemetry\SDK\Logs\EventLoggerProviderFactory;
use OpenTelemetry\SDK\Logs\LoggerProviderFactory;
use OpenTelemetry\SDK\Metrics\MeterProviderFactory;
use OpenTelemetry\SDK\Propagation\LateBindingTextMapPropagator;
use OpenTelemetry\SDK\Propagation\PropagatorFactory;
use OpenTelemetry\SDK\Resource\ResourceInfoFactory;
use OpenTelemetry\SDK\Trace\AutoRootSpan;
use OpenTelemetry\SDK\Trace\ExporterFactory;
use OpenTelemetry\SDK\Trace\SamplerFactory;
use OpenTelemetry\SDK\Trace\SpanProcessorFactory;
use OpenTelemetry\SDK\Trace\TracerProviderBuilder;
use RuntimeException;
use Throwable;
/**
* @psalm-suppress RedundantCast
*/
class SdkAutoloader
{
use LogsMessagesTrait;
public static function autoload(): bool
{
if (!self::isEnabled() || self::isExcludedUrl()) {
return false;
}
if (Configuration::has(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE)) {
if (!class_exists(SdkConfiguration::class)) {
throw new RuntimeException('File-based configuration requires open-telemetry/sdk-configuration');
}
Globals::registerInitializer(fn ($configurator) => self::fileBasedInitializer($configurator));
} else {
Globals::registerInitializer(fn ($configurator) => self::environmentBasedInitializer($configurator));
}
self::registerInstrumentations();
if (AutoRootSpan::isEnabled()) {
$request = AutoRootSpan::createRequest();
if ($request) {
AutoRootSpan::create($request);
AutoRootSpan::registerShutdownHandler();
}
}
return true;
}
private static function environmentBasedInitializer(Configurator $configurator): Configurator
{
$propagator = (new PropagatorFactory())->create();
if (Sdk::isDisabled()) {
//@see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/sdk-environment-variables.md#general-sdk-configuration
return $configurator->withPropagator($propagator);
}
$emitMetrics = Configuration::getBoolean(Variables::OTEL_PHP_INTERNAL_METRICS_ENABLED);
$resource = ResourceInfoFactory::defaultResource();
$exporter = (new ExporterFactory())->create();
$meterProvider = (new MeterProviderFactory())->create($resource);
$spanProcessor = (new SpanProcessorFactory())->create($exporter, $emitMetrics ? $meterProvider : null);
$tracerProvider = (new TracerProviderBuilder())
->addSpanProcessor($spanProcessor)
->setResource($resource)
->setSampler((new SamplerFactory())->create())
->build();
$loggerProvider = (new LoggerProviderFactory())->create($emitMetrics ? $meterProvider : null, $resource);
$eventLoggerProvider = (new EventLoggerProviderFactory())->create($loggerProvider);
ShutdownHandler::register($tracerProvider->shutdown(...));
ShutdownHandler::register($meterProvider->shutdown(...));
ShutdownHandler::register($loggerProvider->shutdown(...));
return $configurator
->withTracerProvider($tracerProvider)
->withMeterProvider($meterProvider)
->withLoggerProvider($loggerProvider)
->withEventLoggerProvider($eventLoggerProvider)
->withPropagator($propagator)
;
}
/**
* @phan-suppress PhanPossiblyUndeclaredVariable
*/
private static function fileBasedInitializer(Configurator $configurator): Configurator
{
$file = Configuration::getString(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE);
$config = SdkConfiguration::parseFile($file);
//disable hook manager during SDK to avoid autoinstrumenting SDK exporters.
$scope = HookManager::disable(Context::getCurrent())->activate();
try {
$sdk = $config
->create()
->setAutoShutdown(true)
->build();
} finally {
$scope->detach();
}
return $configurator
->withTracerProvider($sdk->getTracerProvider())
->withMeterProvider($sdk->getMeterProvider())
->withLoggerProvider($sdk->getLoggerProvider())
->withPropagator($sdk->getPropagator())
->withEventLoggerProvider($sdk->getEventLoggerProvider())
;
}
/**
* Register all {@link Instrumentation} configured through SPI
* @psalm-suppress ArgumentTypeCoercion
*/
private static function registerInstrumentations(): void
{
$files = Configuration::has(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE)
? Configuration::getList(Variables::OTEL_EXPERIMENTAL_CONFIG_FILE)
: [];
if (class_exists(SdkInstrumentation::class)) {
$configuration = SdkInstrumentation::parseFile($files)->create();
} else {
$configuration = new NoopConfigProperties();
}
$hookManager = self::getHookManager();
$tracerProvider = self::createLateBindingTracerProvider();
$meterProvider = self::createLateBindingMeterProvider();
$loggerProvider = self::createLateBindingLoggerProvider();
$propagator = self::createLateBindingTextMapPropagator();
$context = new InstrumentationContext($tracerProvider, $meterProvider, $loggerProvider, $propagator);
foreach (ServiceLoader::load(Instrumentation::class) as $instrumentation) {
/** @var Instrumentation $instrumentation */
try {
$instrumentation->register($hookManager, $configuration, $context);
} catch (Throwable $t) {
self::logError(sprintf('Unable to load instrumentation: %s', $instrumentation::class), ['exception' => $t]);
}
}
}
private static function createLateBindingTracerProvider(): TracerProviderInterface
{
return new LateBindingTracerProvider(static function (): TracerProviderInterface {
$scope = Context::getRoot()->activate();
try {
return Globals::tracerProvider();
} finally {
$scope->detach();
}
});
}
private static function createLateBindingMeterProvider(): MeterProviderInterface
{
return new LateBindingMeterProvider(static function (): MeterProviderInterface {
$scope = Context::getRoot()->activate();
try {
return Globals::meterProvider();
} finally {
$scope->detach();
}
});
}
private static function createLateBindingLoggerProvider(): LoggerProviderInterface
{
return new LateBindingLoggerProvider(static function (): LoggerProviderInterface {
$scope = Context::getRoot()->activate();
try {
return Globals::loggerProvider();
} finally {
$scope->detach();
}
});
}
private static function createLateBindingTextMapPropagator(): TextMapPropagatorInterface
{
return new LateBindingTextMapPropagator(static function (): TextMapPropagatorInterface {
$scope = Context::getRoot()->activate();
try {
return Globals::propagator();
} finally {
$scope->detach();
}
});
}
private static function getHookManager(): HookManagerInterface
{
/** @var HookManagerInterface $hookManager */
foreach (ServiceLoader::load(HookManagerInterface::class) as $hookManager) {
return $hookManager;
}
return new NoopHookManager();
}
/**
* @internal
*/
public static function isEnabled(): bool
{
return Configuration::getBoolean(Variables::OTEL_PHP_AUTOLOAD_ENABLED);
}
/**
* Test whether a request URI is set, and if it matches the excluded urls configuration option
*
* @internal
*/
public static function isExcludedUrl(): bool
{
$excludedUrls = Configuration::getList(Variables::OTEL_PHP_EXCLUDED_URLS, []);
if ($excludedUrls === []) {
return false;
}
$url = $_SERVER['REQUEST_URI'] ?? null;
if (!$url) {
return false;
}
foreach ($excludedUrls as $excludedUrl) {
if (preg_match(sprintf('|%s|', $excludedUrl), (string) $url) === 1) {
return true;
}
}
return false;
}
}