Skip to content

Commit

Permalink
refactor(DependencyInjection): move configurate in compiler passes
Browse files Browse the repository at this point in the history
  • Loading branch information
gaelreyrol committed Jan 13, 2025
1 parent 24c7fc3 commit d8c7610
Show file tree
Hide file tree
Showing 21 changed files with 184 additions and 161 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
beStrictAboutOutputDuringTests="true"
Expand Down
73 changes: 73 additions & 0 deletions src/DependencyInjection/Compiler/CacheInstrumentationPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class CacheInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasParameter('open_telemetry.instrumentation.cache.tracing.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.cache.tracing.enabled')) {
$container->removeDefinition('open_telemetry.instrumentation.cache.trace.tag_aware_adapter');
$container->removeDefinition('open_telemetry.instrumentation.cache.trace.adapter');

return;
}

foreach ($container->findTaggedServiceIds('cache.pool') as $serviceId => $tags) {
$cachePoolDefinition = $container->getDefinition($serviceId);
if ($cachePoolDefinition->isAbstract()) {
continue;
}

$definitionClass = $this->resolveDefinitionClass($container, $cachePoolDefinition);
if (null === $definitionClass) {
continue;
}

$traceableCachePoolDefinition = new ChildDefinition('open_telemetry.instrumentation.cache.trace.adapter');
$traceableCachePoolDefinition
->setDecoratedService($serviceId)
->setArgument('$adapter', new Reference('.inner'));

$container->setDefinition($serviceId.'.tracer', $traceableCachePoolDefinition);
}

foreach ($container->findTaggedServiceIds('cache.taggable') as $serviceId => $tags) {
$cachePoolDefinition = $container->getDefinition($serviceId);
if ($cachePoolDefinition->isAbstract()) {
continue;
}

$definitionClass = $this->resolveDefinitionClass($container, $cachePoolDefinition);
if (null === $definitionClass) {
continue;
}

$traceableCachePoolDefinition = new ChildDefinition('open_telemetry.instrumentation.cache.trace.tag_aware_adapter');
$traceableCachePoolDefinition
->setDecoratedService($serviceId)
->setArgument('$adapter', new Reference('.inner'));

$container->setDefinition($serviceId.'.tracer', $traceableCachePoolDefinition);
}
}

private function resolveDefinitionClass(ContainerBuilder $container, Definition $definition): ?string
{
$class = $definition->getClass();

while (null === $class && $definition instanceof ChildDefinition) {
$definition = $container->findDefinition($definition->getParent());
$class = $definition->getClass();
}

return $class;
}
}
71 changes: 0 additions & 71 deletions src/DependencyInjection/Compiler/CachePoolTracingPass.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveConsoleInstrumentationPass implements CompilerPassInterface
class ConsoleInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasParameter('open_telemetry.instrumentation.console.tracing.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.console.tracing.enabled')) {
$container->removeDefinition('open_telemetry.instrumentation.console.trace.event_subscriber');
}

if (false === $container->hasParameter('open_telemetry.instrumentation.console.metering.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.console.metering.enabled')) {
return;
}

$container
->getDefinition('open_telemetry.instrumentation.console.trace.event_subscriber')
->addTag('kernel.event_subscriber');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveDoctrineInstrumentationPass implements CompilerPassInterface
class DoctrineInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasParameter('open_telemetry.instrumentation.doctrine.tracing.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.doctrine.tracing.enabled')) {
$container->removeDefinition('open_telemetry.instrumentation.doctrine.trace.middleware');
}

if (false === $container->hasParameter('open_telemetry.instrumentation.doctrine.metering.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.doctrine.metering.enabled')) {
return;
}

$container
->getDefinition('open_telemetry.instrumentation.doctrine.trace.middleware')
->addTag('doctrine.middleware');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class HttpClientTracingPass implements CompilerPassInterface
class HttpClientInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveHttpKernelInstrumentationPass implements CompilerPassInterface
class HttpKernelInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasParameter('open_telemetry.instrumentation.http_kernel.tracing.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.http_kernel.tracing.enabled')) {
$container->removeDefinition('open_telemetry.instrumentation.http_kernel.trace.event_subscriber');
}
$container->removeDefinition('open_telemetry.instrumentation.http_kernel.trace.route_loader');

if (false === $container->hasParameter('open_telemetry.instrumentation.http_kernel.metering.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.http_kernel.metering.enabled')) {
return;
}

$container
->getDefinition('open_telemetry.instrumentation.http_kernel.trace.event_subscriber')
->addTag('kernel.event_subscriber');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveMailerInstrumentationPass implements CompilerPassInterface
class MailerInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
Expand All @@ -14,10 +14,8 @@ public function process(ContainerBuilder $container): void
$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');
}

if (false === $container->hasParameter('open_telemetry.instrumentation.mailer.metering.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.mailer.metering.enabled')) {
return;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveMessengerInstrumentationPass implements CompilerPassInterface
class MessengerInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
$container
->getDefinition('open_telemetry.instrumentation.messenger.trace.transport_factory')
->addTag('messenger.transport_factory')
->addTag('kernel.reset', ['method' => 'reset']);

$container->setAlias('messenger.transport.open_telemetry_tracer.factory', 'open_telemetry.instrumentation.messenger.trace.transport_factory');
$container->setAlias('messenger.middleware.open_telemetry_tracer', 'open_telemetry.instrumentation.messenger.trace.middleware');

if (false === $container->hasParameter('open_telemetry.instrumentation.messenger.tracing.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.messenger.tracing.enabled')) {
$container->removeAlias('messenger.transport.open_telemetry_tracer.factory');
Expand All @@ -18,9 +26,5 @@ public function process(ContainerBuilder $container): void
$container->removeDefinition('open_telemetry.instrumentation.messenger.trace.transport_factory');
$container->removeDefinition('open_telemetry.instrumentation.messenger.trace.middleware');
}

if (false === $container->hasParameter('open_telemetry.instrumentation.messenger.metering.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.messenger.metering.enabled')) {
}
}
}
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/TracerLocatorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function process(ContainerBuilder $container): void
{
$tracers = $container->findTaggedServiceIds('open_telemetry.tracer');

if (0 !== count($tracers)) {
if (0 < count($tracers)) {
if ($container->has('open_telemetry.instrumentation.console.trace.event_subscriber')) {
$traceableConsoleEventSubscriber = $container->getDefinition('open_telemetry.instrumentation.console.trace.event_subscriber');
$this->setTracerLocatorArgument($container, $traceableConsoleEventSubscriber, $tracers);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class RemoveTwigInstrumentationPass implements CompilerPassInterface
class TwigInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasParameter('open_telemetry.instrumentation.twig.tracing.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.twig.tracing.enabled')) {
$container->removeDefinition('open_telemetry.instrumentation.twig.trace.extension');
}

if (false === $container->hasParameter('open_telemetry.instrumentation.twig.metering.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.twig.metering.enabled')) {
return;
}

$container
->getDefinition('open_telemetry.instrumentation.twig.trace.extension')
->addTag('twig.extension');
}
}
35 changes: 18 additions & 17 deletions src/OpenTelemetryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
namespace FriendsOfOpenTelemetry\OpenTelemetryBundle;

use Composer\InstalledVersions;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\CachePoolTracingPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\HttpClientTracingPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\RemoveConsoleInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\RemoveDoctrineInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\RemoveHttpKernelInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\RemoveMailerInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\RemoveMessengerInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\RemoveTwigInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\CacheInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\ConsoleInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\DoctrineInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\HttpClientInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\HttpKernelInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\MailerInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\MessengerInstrumentationPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\SetInstrumentationTypePass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\TracerLocatorPass;
use FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler\TwigInstrumentationPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -33,15 +33,16 @@ public function build(ContainerBuilder $container): void
parent::build($container);

$container->addCompilerPass(new SetInstrumentationTypePass());
$container->addCompilerPass(new CachePoolTracingPass());
$container->addCompilerPass(new HttpClientTracingPass());

$container->addCompilerPass(new CacheInstrumentationPass());
$container->addCompilerPass(new ConsoleInstrumentationPass());
$container->addCompilerPass(new DoctrineInstrumentationPass());
$container->addCompilerPass(new HttpClientInstrumentationPass());
$container->addCompilerPass(new HttpKernelInstrumentationPass());
$container->addCompilerPass(new MailerInstrumentationPass());
$container->addCompilerPass(new MessengerInstrumentationPass());
$container->addCompilerPass(new TwigInstrumentationPass());

$container->addCompilerPass(new TracerLocatorPass());
$container->addCompilerPass(new RemoveConsoleInstrumentationPass());
$container->addCompilerPass(new RemoveDoctrineInstrumentationPass());
$container->addCompilerPass(new RemoveHttpKernelInstrumentationPass());
$container->addCompilerPass(new RemoveMailerInstrumentationPass());
$container->addCompilerPass(new RemoveMessengerInstrumentationPass());
$container->addCompilerPass(new RemoveTwigInstrumentationPass());
$container->addCompilerPass(new RemoveTwigInstrumentationPass());
}
}
Loading

0 comments on commit d8c7610

Please sign in to comment.