From bee0a58b6e2f0aaafc7497ad12e524a65766b6bb Mon Sep 17 00:00:00 2001 From: Plakhotnikov Vladimir Date: Sat, 4 Jan 2025 10:46:43 +0300 Subject: [PATCH] refactor(config): Prototype of config separation and auto loading Signed-off-by: Plakhotnikov Vladimir --- .../OpenTelemetryExtension.php | 59 +--------------- .../OpenTelemetryInstrumentationExtension.php | 70 +++++++++++++++++++ .../config/instrumentation/cache.php | 23 ++++++ .../config/instrumentation/console.php | 16 +++++ .../config/instrumentation/doctrine.php | 17 +++++ .../config/instrumentation/http_client.php | 16 +++++ .../config/instrumentation/http_kernel.php | 25 +++++++ .../config/instrumentation/mailer.php | 31 ++++++++ .../config/instrumentation/messenger.php | 31 ++++++++ src/Resources/config/instrumentation/twig.php | 17 +++++ 10 files changed, 247 insertions(+), 58 deletions(-) create mode 100644 src/DependencyInjection/OpenTelemetryInstrumentationExtension.php create mode 100644 src/Resources/config/instrumentation/cache.php create mode 100644 src/Resources/config/instrumentation/console.php create mode 100644 src/Resources/config/instrumentation/doctrine.php create mode 100644 src/Resources/config/instrumentation/http_client.php create mode 100644 src/Resources/config/instrumentation/http_kernel.php create mode 100644 src/Resources/config/instrumentation/mailer.php create mode 100644 src/Resources/config/instrumentation/messenger.php create mode 100644 src/Resources/config/instrumentation/twig.php diff --git a/src/DependencyInjection/OpenTelemetryExtension.php b/src/DependencyInjection/OpenTelemetryExtension.php index ea71ccd..2797202 100644 --- a/src/DependencyInjection/OpenTelemetryExtension.php +++ b/src/DependencyInjection/OpenTelemetryExtension.php @@ -2,27 +2,11 @@ namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection; -use FriendsOfOpenTelemetry\OpenTelemetryBundle\Instrumentation\InstrumentationTypeEnum; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension; -/** - * @phpstan-type InstrumentationConfig array{ - * type?: string, - * tracing: TracingInstrumentationConfig, - * metering: MeteringInstrumentationConfig, - * } - * @phpstan-type TracingInstrumentationConfig array{ - * enabled: bool, - * tracer: ?string, - * } - * @phpstan-type MeteringInstrumentationConfig array{ - * enabled: bool, - * meter: ?string, - * } - */ final class OpenTelemetryExtension extends ConfigurableExtension { protected function loadInternal(array $mergedConfig, ContainerBuilder $container): void @@ -33,11 +17,10 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container $loader->load('services_logs.php'); $loader->load('services_metrics.php'); $loader->load('services_traces.php'); - $loader->load('services_tracing_instrumentation.php'); $this->loadServiceParams($mergedConfig['service'], $container); - $this->loadInstrumentationParams($mergedConfig['instrumentation'], $container); + (new OpenTelemetryInstrumentationExtension())($mergedConfig['instrumentation'], $container, $loader); (new OpenTelemetryTracesExtension())($mergedConfig['traces'], $container); (new OpenTelemetryMetricsExtension())($mergedConfig['metrics'], $container); (new OpenTelemetryLogsExtension())($mergedConfig['logs'], $container); @@ -66,44 +49,4 @@ private function loadServiceParams(array $config, ContainerBuilder $container): $config['environment'], ]); } - - /** - * @param array{ - * cache: InstrumentationConfig, - * console: InstrumentationConfig, - * doctrine: InstrumentationConfig, - * http_client: InstrumentationConfig, - * http_kernel: InstrumentationConfig, - * mailer: InstrumentationConfig, - * messenger: InstrumentationConfig, - * twig: InstrumentationConfig, - * } $config - */ - private function loadInstrumentationParams(array $config, ContainerBuilder $container): void - { - foreach ($config as $name => $instrumentation) { - $container->setParameter( - sprintf('open_telemetry.instrumentation.%s.tracing.enabled', $name), - $instrumentation['tracing']['enabled'], - ); - if (isset($instrumentation['type'])) { - $container->setParameter( - sprintf('open_telemetry.instrumentation.%s.type', $name), - InstrumentationTypeEnum::from($instrumentation['type']), - ); - } - $container->setParameter( - sprintf('open_telemetry.instrumentation.%s.tracing.tracer', $name), - $instrumentation['tracing']['tracer'] ?? 'default_tracer', - ); - $container->setParameter( - sprintf('open_telemetry.instrumentation.%s.metering.enabled', $name), - $instrumentation['metering']['enabled'], - ); - $container->setParameter( - sprintf('open_telemetry.instrumentation.%s.metering.meter', $name), - $instrumentation['metering']['meter'] ?? 'default_meter', - ); - } - } } diff --git a/src/DependencyInjection/OpenTelemetryInstrumentationExtension.php b/src/DependencyInjection/OpenTelemetryInstrumentationExtension.php new file mode 100644 index 0000000..9acaf2b --- /dev/null +++ b/src/DependencyInjection/OpenTelemetryInstrumentationExtension.php @@ -0,0 +1,70 @@ + $instrumentation) { + $container->setParameter( + sprintf('open_telemetry.instrumentation.%s.tracing.enabled', $name), + $instrumentation['tracing']['enabled'], + ); + + if (isset($instrumentation['type'])) { + $container->setParameter( + sprintf('open_telemetry.instrumentation.%s.type', $name), + InstrumentationTypeEnum::from($instrumentation['type']), + ); + } + $container->setParameter( + sprintf('open_telemetry.instrumentation.%s.tracing.tracer', $name), + $instrumentation['tracing']['tracer'] ?? 'default_tracer', + ); + $container->setParameter( + sprintf('open_telemetry.instrumentation.%s.metering.enabled', $name), + $instrumentation['metering']['enabled'], + ); + $container->setParameter( + sprintf('open_telemetry.instrumentation.%s.metering.meter', $name), + $instrumentation['metering']['meter'] ?? 'default_meter', + ); + + if ($instrumentation['tracing']['enabled']) { + $loader->load('instrumentation/'.$name.'.php'); + } + } + } +} diff --git a/src/Resources/config/instrumentation/cache.php b/src/Resources/config/instrumentation/cache.php new file mode 100644 index 0000000..eea8609 --- /dev/null +++ b/src/Resources/config/instrumentation/cache.php @@ -0,0 +1,23 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.cache.trace.adapter', TraceableCacheAdapter::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->abstract() + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + + ->set('open_telemetry.instrumentation.cache.trace.tag_aware_adapter', TagAwareTraceableCacheAdapter::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->abstract() + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ; +}; diff --git a/src/Resources/config/instrumentation/console.php b/src/Resources/config/instrumentation/console.php new file mode 100644 index 0000000..d6b9921 --- /dev/null +++ b/src/Resources/config/instrumentation/console.php @@ -0,0 +1,16 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.console.trace.event_subscriber', TraceableConsoleEventSubscriber::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->tag('kernel.event_subscriber') + ; +}; diff --git a/src/Resources/config/instrumentation/doctrine.php b/src/Resources/config/instrumentation/doctrine.php new file mode 100644 index 0000000..fcc55b4 --- /dev/null +++ b/src/Resources/config/instrumentation/doctrine.php @@ -0,0 +1,17 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.doctrine.trace.middleware', TraceableDoctrineMiddleware::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->tag('doctrine.middleware') + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ; +}; diff --git a/src/Resources/config/instrumentation/http_client.php b/src/Resources/config/instrumentation/http_client.php new file mode 100644 index 0000000..c5ed854 --- /dev/null +++ b/src/Resources/config/instrumentation/http_client.php @@ -0,0 +1,16 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.http_client.trace.client', TraceableHttpClient::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ; +}; diff --git a/src/Resources/config/instrumentation/http_kernel.php b/src/Resources/config/instrumentation/http_kernel.php new file mode 100644 index 0000000..563dafa --- /dev/null +++ b/src/Resources/config/instrumentation/http_kernel.php @@ -0,0 +1,25 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.http_kernel.trace.event_subscriber', TraceableHttpKernelEventSubscriber::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->arg('$propagator', service('open_telemetry.propagator_text_map.noop')) + ->arg('$propagationGetter', service('open_telemetry.propagation_getter.headers')) + ->tag('kernel.event_subscriber') + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + + ->set('open_telemetry.instrumentation.http_kernel.trace.route_loader', TraceableRouteLoader::class) + ->decorate('routing.loader') + ->arg('$loader', service('.inner')) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ; +}; diff --git a/src/Resources/config/instrumentation/mailer.php b/src/Resources/config/instrumentation/mailer.php new file mode 100644 index 0000000..6f60778 --- /dev/null +++ b/src/Resources/config/instrumentation/mailer.php @@ -0,0 +1,31 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.mailer.trace.transports', TraceableMailerTransport::class) + ->decorate('mailer.transports') + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->arg('$transport', service('.inner')) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + + ->set('open_telemetry.instrumentation.mailer.trace.default_transport', TraceableMailerTransport::class) + ->decorate('mailer.default_transport') + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->arg('$transport', service('.inner')) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + + ->set('open_telemetry.instrumentation.mailer.trace.mailer', TraceableMailer::class) + ->decorate('mailer.mailer') + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->arg('$mailer', service('.inner')) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ; +}; diff --git a/src/Resources/config/instrumentation/messenger.php b/src/Resources/config/instrumentation/messenger.php new file mode 100644 index 0000000..bf636a5 --- /dev/null +++ b/src/Resources/config/instrumentation/messenger.php @@ -0,0 +1,31 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.messenger.trace.transport', TraceableMessengerTransport::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + + ->set('open_telemetry.instrumentation.messenger.trace.transport_factory', TraceableMessengerTransportFactory::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->arg('$transportFactory', service('messenger.transport_factory')) + ->tag('messenger.transport_factory') + ->tag('kernel.reset', ['method' => 'reset']) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ->alias('messenger.transport.open_telemetry_tracer.factory', 'open_telemetry.instrumentation.messenger.trace.transport_factory') + + ->set('open_telemetry.instrumentation.messenger.trace.middleware', TraceableMessengerMiddleware::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ->alias('messenger.middleware.open_telemetry_tracer', 'open_telemetry.instrumentation.messenger.trace.middleware') + ; +}; diff --git a/src/Resources/config/instrumentation/twig.php b/src/Resources/config/instrumentation/twig.php new file mode 100644 index 0000000..15ba17d --- /dev/null +++ b/src/Resources/config/instrumentation/twig.php @@ -0,0 +1,17 @@ +services() + ->defaults() + ->private() + ->set('open_telemetry.instrumentation.twig.trace.extension', TraceableTwigExtension::class) + ->arg('$tracer', service('open_telemetry.traces.default_tracer')) + ->tag('twig.extension') + ->tag('monolog.logger', ['channel' => 'open_telemetry']) + ; +};