Skip to content

refactor(DependencyInjection): move configurate in compiler passes #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
78 changes: 78 additions & 0 deletions src/DependencyInjection/Compiler/CacheInstrumentationPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

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;
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;
}

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()) {
continue;

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L31

Added line #L31 was not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L36

Added line #L36 was not covered by tests
}

$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;

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L48-L50

Added lines #L48 - L50 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L53-L55

Added lines #L53 - L55 were not covered by tests
}

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

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L58-L61

Added lines #L58 - L61 were not covered by tests

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

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L63

Added line #L63 was not covered by tests
}
}

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();

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

View check run for this annotation

Codecov / codecov/patch

src/DependencyInjection/Compiler/CacheInstrumentationPass.php#L72-L73

Added lines #L72 - L73 were not covered by tests
}

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

This file was deleted.

19 changes: 19 additions & 0 deletions src/DependencyInjection/Compiler/ConsoleInstrumentationPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class ConsoleInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (true === $container->hasParameter('open_telemetry.instrumentation.console.tracing.enabled')
&& true === $container->getParameter('open_telemetry.instrumentation.console.tracing.enabled')) {
return;
}

$container->removeDefinition('open_telemetry.instrumentation.console.trace.event_subscriber');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler;

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
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');
return;
}

if (false === $container->hasParameter('open_telemetry.instrumentation.doctrine.metering.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.doctrine.metering.enabled')) {
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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,33 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpClient\HttpClient;

class HttpClientTracingPass implements CompilerPassInterface
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 {
$container->removeDefinition('open_telemetry.instrumentation.http_client.trace.client');
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
}

if (true === $container->hasParameter('open_telemetry.instrumentation.http_client.metering.enabled')
&& true === $container->getParameter('open_telemetry.instrumentation.http_client.metering.enabled')) {
$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
@@ -0,0 +1,20 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class HttpKernelInstrumentationPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (true === $container->hasParameter('open_telemetry.instrumentation.http_kernel.tracing.enabled')
&& true === $container->getParameter('open_telemetry.instrumentation.http_kernel.tracing.enabled')) {
return;
}

$container->removeDefinition('open_telemetry.instrumentation.http_kernel.trace.event_subscriber');
$container->removeDefinition('open_telemetry.instrumentation.http_kernel.trace.route_loader');
}
}
27 changes: 27 additions & 0 deletions src/DependencyInjection/Compiler/MailerInstrumentationPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler;

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
{
public function process(ContainerBuilder $container): void
{
if (true === $container->hasParameter('open_telemetry.instrumentation.mailer.tracing.enabled')
&& true === $container->getParameter('open_telemetry.instrumentation.mailer.tracing.enabled')) {
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');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace FriendsOfOpenTelemetry\OpenTelemetryBundle\DependencyInjection\Compiler;

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
{
public function process(ContainerBuilder $container): void
{
if (false === $container->hasParameter('open_telemetry.instrumentation.messenger.tracing.enabled')
|| false === $container->getParameter('open_telemetry.instrumentation.messenger.tracing.enabled')) {
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
->setAlias('messenger.middleware.open_telemetry_tracer', 'open_telemetry.instrumentation.messenger.trace.middleware');

$container->getDefinition('open_telemetry.instrumentation.messenger.trace.transport_factory')
->addTag('messenger.transport_factory')
->addTag('kernel.reset', ['method' => 'reset']);
$container->getDefinition('open_telemetry.instrumentation.messenger.trace.middleware')
->addTag('messenger.middleware');
}
}

This file was deleted.

This file was deleted.

Loading
Loading