From 6e3014b604f043a287a86f7af60edf576a1bc6ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABl=20Reyrol?= Date: Mon, 13 Jan 2025 15:50:41 +0100 Subject: [PATCH] feat(DependencyInjection): do not instrument services if packages are missing --- .../Compiler/CacheInstrumentationPass.php | 5 ++++ .../Compiler/DoctrineInstrumentationPass.php | 5 ++++ .../HttpClientInstrumentationPass.php | 23 ++++++++++++------- .../Compiler/TwigInstrumentationPass.php | 5 ++++ 4 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/DependencyInjection/Compiler/CacheInstrumentationPass.php b/src/DependencyInjection/Compiler/CacheInstrumentationPass.php index 55c322e..9665b2d 100644 --- a/src/DependencyInjection/Compiler/CacheInstrumentationPass.php +++ b/src/DependencyInjection/Compiler/CacheInstrumentationPass.php @@ -2,6 +2,7 @@ namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler; +use Symfony\Component\Cache\CacheItem; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -20,6 +21,10 @@ public function process(ContainerBuilder $container): void return; } + if (!class_exists(CacheItem::class)) { + throw new \LogicException('Cache instrumentation cannot be enabled because the symfony/cache package is not installed.'); + } + foreach ($container->findTaggedServiceIds('cache.pool') as $serviceId => $tags) { $cachePoolDefinition = $container->getDefinition($serviceId); if ($cachePoolDefinition->isAbstract()) { diff --git a/src/DependencyInjection/Compiler/DoctrineInstrumentationPass.php b/src/DependencyInjection/Compiler/DoctrineInstrumentationPass.php index 086cf26..bd36d69 100644 --- a/src/DependencyInjection/Compiler/DoctrineInstrumentationPass.php +++ b/src/DependencyInjection/Compiler/DoctrineInstrumentationPass.php @@ -2,6 +2,7 @@ namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler; +use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -14,6 +15,10 @@ public function process(ContainerBuilder $container): void return; } + if (!class_exists(DoctrineBundle::class)) { + throw new \LogicException('Doctrine instrumentation cannot be enabled because the doctrine/doctrine-bundle package is not installed.'); + } + $container->getDefinition('open_telemetry.instrumentation.doctrine.trace.middleware') ->addTag('doctrine.middleware'); } diff --git a/src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php b/src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php index e3ef118..d5bc9a4 100644 --- a/src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php +++ b/src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php @@ -5,26 +5,33 @@ use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Reference; +use Symfony\Component\HttpClient\HttpClient; class HttpClientInstrumentationPass implements CompilerPassInterface { public function process(ContainerBuilder $container): void { - $decoratedService = $this->getDecoratedService($container); - if (null === $decoratedService) { + if (false === $container->hasParameter('open_telemetry.instrumentation.http_client.tracing.enabled') + || false === $container->getParameter('open_telemetry.instrumentation.http_client.tracing.enabled')) { $container->removeDefinition('open_telemetry.instrumentation.http_client.trace.client'); return; } - if (true === $container->hasParameter('open_telemetry.instrumentation.http_client.tracing.enabled') - && true === $container->getParameter('open_telemetry.instrumentation.http_client.tracing.enabled')) { - $container->getDefinition('open_telemetry.instrumentation.http_client.trace.client') - ->setArgument('$client', new Reference('.inner')) - ->setDecoratedService($decoratedService[0], null, $decoratedService[1]); - } else { + if (!class_exists(HttpClient::class)) { + throw new \LogicException('Http client instrumentation cannot be enabled because the symfony/http-client package is not installed.'); + } + + $decoratedService = $this->getDecoratedService($container); + if (null === $decoratedService) { $container->removeDefinition('open_telemetry.instrumentation.http_client.trace.client'); + + return; } + + $container->getDefinition('open_telemetry.instrumentation.http_client.trace.client') + ->setArgument('$client', new Reference('.inner')) + ->setDecoratedService($decoratedService[0], null, $decoratedService[1]); } /** diff --git a/src/DependencyInjection/Compiler/TwigInstrumentationPass.php b/src/DependencyInjection/Compiler/TwigInstrumentationPass.php index e8e8045..0586b83 100644 --- a/src/DependencyInjection/Compiler/TwigInstrumentationPass.php +++ b/src/DependencyInjection/Compiler/TwigInstrumentationPass.php @@ -2,6 +2,7 @@ namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler; +use Symfony\Bundle\TwigBundle\TwigBundle; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; @@ -14,6 +15,10 @@ public function process(ContainerBuilder $container): void return; } + if (!class_exists(TwigBundle::class)) { + throw new \LogicException('Twig instrumentation cannot be enabled because the symfony/twig-bundle package is not installed.'); + } + $container->getDefinition('open_telemetry.instrumentation.twig.trace.extension') ->addTag('twig.extension'); }