|
| 1 | +<?php declare(strict_types=1); |
| 2 | +namespace _; |
| 3 | + |
| 4 | +use Nevay\SPI\ServiceLoader; |
| 5 | +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ConfigurationRegistry; |
| 6 | +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\ExtensionHookManager; |
| 7 | +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\HookManager; |
| 8 | +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\Instrumentation; |
| 9 | +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\InstrumentationConfiguration; |
| 10 | +use OpenTelemetry\API\Instrumentation\AutoInstrumentation\NoopHookManager; |
| 11 | +use OpenTelemetry\Config\SDK\Configuration; |
| 12 | +use OpenTelemetry\Config\SDK\Configuration\ComponentPlugin; |
| 13 | +use OpenTelemetry\Config\SDK\Configuration\ComponentProvider; |
| 14 | +use OpenTelemetry\Config\SDK\Configuration\ComponentProviderRegistry; |
| 15 | +use OpenTelemetry\Config\SDK\Configuration\ConfigurationFactory; |
| 16 | +use OpenTelemetry\Config\SDK\Configuration\Context; |
| 17 | +use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceReader; |
| 18 | +use OpenTelemetry\Config\SDK\Configuration\Environment\PhpIniEnvSource; |
| 19 | +use OpenTelemetry\Config\SDK\Configuration\Environment\ServerEnvSource; |
| 20 | +use OpenTelemetry\Example\Example; |
| 21 | +use OpenTelemetry\Example\ExampleConfigProvider; |
| 22 | +use OpenTelemetry\Example\ExampleInstrumentation; |
| 23 | +use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; |
| 24 | +use const PHP_EOL; |
| 25 | + |
| 26 | +// EXAMPLE_INSTRUMENTATION_SPAN_NAME=test1234 php examples/instrumentation/configure_instrumentation.php |
| 27 | + |
| 28 | +require __DIR__ . '/../../vendor/autoload.php'; |
| 29 | + |
| 30 | +ServiceLoader::register(HookManager::class, ExtensionHookManager::class); |
| 31 | +ServiceLoader::register(ComponentProvider::class, ExampleConfigProvider::class); |
| 32 | +ServiceLoader::register(Instrumentation::class, ExampleInstrumentation::class); |
| 33 | + |
| 34 | +$sdk = Configuration::parseFile(__DIR__ . '/otel-sdk.yaml')->create(new Context())->setAutoShutdown(true)->build(); |
| 35 | +$configuration = parseInstrumentationConfig(__DIR__ . '/otel-instrumentation.yaml')->create(new Context()); |
| 36 | + |
| 37 | +$hookManager = hookManager(); |
| 38 | +$context = new Context($sdk->getTracerProvider()); |
| 39 | +$storage = \OpenTelemetry\Context\Context::storage(); |
| 40 | + |
| 41 | +foreach (ServiceLoader::load(Instrumentation::class) as $instrumentation) { |
| 42 | + $instrumentation->register($hookManager, $context, $configuration, $storage); |
| 43 | +} |
| 44 | + |
| 45 | +$scope = $storage->attach($hookManager->enable($storage->current())); |
| 46 | + |
| 47 | +try { |
| 48 | + echo (new Example())->test(), PHP_EOL; |
| 49 | +} finally { |
| 50 | + $scope->detach(); |
| 51 | +} |
| 52 | + |
| 53 | + |
| 54 | +function hookManager(): HookManager { |
| 55 | + foreach (ServiceLoader::load(HookManager::class) as $hookManager) { |
| 56 | + return $hookManager; |
| 57 | + } |
| 58 | + |
| 59 | + return new NoopHookManager(); |
| 60 | +} |
| 61 | + |
| 62 | +function parseInstrumentationConfig(string $file): ComponentPlugin { |
| 63 | + // TODO Include in SDK config? |
| 64 | + return (new ConfigurationFactory( |
| 65 | + ServiceLoader::load(ComponentProvider::class), |
| 66 | + new class implements ComponentProvider { |
| 67 | + |
| 68 | + /** |
| 69 | + * @param array{ |
| 70 | + * config: list<ComponentPlugin<InstrumentationConfiguration>>, |
| 71 | + * } $properties |
| 72 | + */ |
| 73 | + public function createPlugin(array $properties, Context $context): ConfigurationRegistry { |
| 74 | + $configurationRegistry = new ConfigurationRegistry(); |
| 75 | + foreach ($properties['config'] as $configuration) { |
| 76 | + $configurationRegistry->add($configuration->create($context)); |
| 77 | + } |
| 78 | + |
| 79 | + return $configurationRegistry; |
| 80 | + } |
| 81 | + |
| 82 | + public function getConfig(ComponentProviderRegistry $registry): ArrayNodeDefinition { |
| 83 | + $root = new ArrayNodeDefinition('instrumentation'); |
| 84 | + $root |
| 85 | + ->children() |
| 86 | + // TODO add disabled_instrumentations arrayNode to allow disabling specific instrumentation classes? |
| 87 | + ->append($registry->componentList('config', InstrumentationConfiguration::class)) |
| 88 | + ->end() |
| 89 | + ; |
| 90 | + |
| 91 | + return $root; |
| 92 | + } |
| 93 | + }, |
| 94 | + new EnvSourceReader([ |
| 95 | + new ServerEnvSource(), |
| 96 | + new PhpIniEnvSource(), |
| 97 | + ]), |
| 98 | + ))->parseFile($file); |
| 99 | +} |
0 commit comments