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 f41fb4a commit 446412a
Show file tree
Hide file tree
Showing 4 changed files with 29 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
22 changes: 14 additions & 8 deletions src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,32 @@
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
{
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 (!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 21 in src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php#L21

Added line #L21 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 26 in src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php#L26

Added line #L26 was not covered by tests

return;

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/HttpClientInstrumentationPass.php#L28

Added line #L28 was not covered by tests
}

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 {
$container->removeDefinition('open_telemetry.instrumentation.http_client.trace.client');
}
$container->getDefinition('open_telemetry.instrumentation.http_client.trace.client')
->setArgument('$client', new Reference('.inner'))
->setDecoratedService($decoratedService[0], null, $decoratedService[1]);
}

/**
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 446412a

Please sign in to comment.