Skip to content

Commit

Permalink
feat(DependencyInjection): do not instrument services if packages are…
Browse files Browse the repository at this point in the history
… missing
  • Loading branch information
gaelreyrol committed Jan 13, 2025
1 parent f247c61 commit 0601912
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
5 changes: 5 additions & 0 deletions src/DependencyInjection/Compiler/CacheInstrumentationPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.');

Check warning on line 25 in src/DependencyInjection/Compiler/CacheInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L25

Added line #L25 was not covered by tests
}

foreach ($container->findTaggedServiceIds('cache.pool') as $serviceId => $tags) {
$cachePoolDefinition = $container->getDefinition($serviceId);
if ($cachePoolDefinition->isAbstract()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.');

Check warning on line 19 in src/DependencyInjection/Compiler/DoctrineInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/DoctrineInstrumentationPass.php#L19

Added line #L19 was not covered by tests
}

$container->getDefinition('open_telemetry.instrumentation.doctrine.trace.middleware')
->addTag('doctrine.middleware');
}
Expand Down
23 changes: 15 additions & 8 deletions src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');

Check warning on line 22 in src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php#L22

Added line #L22 was not covered by tests
}

$decoratedService = $this->getDecoratedService($container);
if (null === $decoratedService) {
$container->removeDefinition('open_telemetry.instrumentation.http_client.trace.client');

Check warning on line 27 in src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php#L27

Added line #L27 was not covered by tests

return;

Check warning on line 29 in src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php#L29

Added line #L29 was not covered by tests
}

$container->getDefinition('open_telemetry.instrumentation.http_client.trace.client')
->setArgument('$client', new Reference('.inner'))
->setDecoratedService($decoratedService[0], null, $decoratedService[1]);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport\TransportFactoryInterface;

class MailerInstrumentationPass implements CompilerPassInterface
{
Expand All @@ -14,6 +16,10 @@ public function process(ContainerBuilder $container): void
return;
}

if (!interface_exists(MailerInterface::class) || !interface_exists(TransportFactoryInterface::class)) {
throw new \LogicException('Mailer instrumentation cannot be enabled because the symfony/mailer package is not installed.');

Check warning on line 20 in src/DependencyInjection/Compiler/MailerInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/MailerInstrumentationPass.php#L20

Added line #L20 was not covered by tests
}

$container->removeDefinition('open_telemetry.instrumentation.mailer.trace.transports');
$container->removeDefinition('open_telemetry.instrumentation.mailer.trace.default_transport');
$container->removeDefinition('open_telemetry.instrumentation.mailer.trace.mailer');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Messenger\Middleware\MiddlewareInterface;
use Symfony\Component\Messenger\Transport\TransportFactoryInterface;

class MessengerInstrumentationPass implements CompilerPassInterface
{
Expand All @@ -14,6 +16,10 @@ public function process(ContainerBuilder $container): void
return;
}

if (!interface_exists(MiddlewareInterface::class) || !interface_exists(TransportFactoryInterface::class)) {
throw new \LogicException('Messenger instrumentation cannot be enabled because the symfony/messenger package is not installed.');

Check warning on line 20 in src/DependencyInjection/Compiler/MessengerInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/MessengerInstrumentationPass.php#L20

Added line #L20 was not covered by tests
}

$container
->setAlias('messenger.transport.open_telemetry_tracer.factory', 'open_telemetry.instrumentation.messenger.trace.transport_factory');
$container
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Compiler/TwigInstrumentationPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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.');

Check warning on line 19 in src/DependencyInjection/Compiler/TwigInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/TwigInstrumentationPass.php#L19

Added line #L19 was not covered by tests
}

$container->getDefinition('open_telemetry.instrumentation.twig.trace.extension')
->addTag('twig.extension');
}
Expand Down

0 comments on commit 0601912

Please sign in to comment.