Skip to content

Change the way the Doctrine tracing middleware is registered in DBAL >=3.x #808

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
Feb 11, 2024
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 composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
},
"require-dev": {
"doctrine/dbal": "^2.13||^3.0",
"doctrine/doctrine-bundle": "^1.12||^2.5",
"doctrine/doctrine-bundle": "^2.6",
"friendsofphp/php-cs-fixer": "^2.19||^3.40",
"masterminds/html5": "^2.8",
"phpstan/extension-installer": "^1.0",
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Parameter \\#1 \\.\\.\\.\\$arrays of function array_merge expects array, mixed given\\.$#"
count: 1
path: src/DependencyInjection/Compiler/DbalTracingPass.php

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Config\\\\Definition\\\\Builder\\\\TreeBuilder\\:\\:root\\(\\)\\.$#"
count: 1
Expand Down
30 changes: 2 additions & 28 deletions src/DependencyInjection/Compiler/DbalTracingPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

final class DbalTracingPass implements CompilerPassInterface
Expand All @@ -20,12 +19,6 @@ final class DbalTracingPass implements CompilerPassInterface
*/
private const CONNECTION_SERVICE_NAME_FORMAT = 'doctrine.dbal.%s_connection';

/**
* This is the format used by the DoctrineBundle bundle to register the
* services for each connection's configuration.
*/
private const CONNECTION_CONFIG_SERVICE_NAME_FORMAT = 'doctrine.dbal.%s_connection.configuration';

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -66,13 +59,8 @@ public function process(ContainerBuilder $container): void

private function configureConnectionForDoctrineDBALVersion3(ContainerBuilder $container, string $connectionName): void
{
$configurationDefinition = $container->getDefinition(sprintf(self::CONNECTION_CONFIG_SERVICE_NAME_FORMAT, $connectionName));
$setMiddlewaresMethodCallArguments = $this->getSetMiddlewaresMethodCallArguments($configurationDefinition);
$setMiddlewaresMethodCallArguments[0] = array_merge($setMiddlewaresMethodCallArguments[0] ?? [], [new Reference(TracingDriverMiddleware::class)]);

$configurationDefinition
->removeMethodCall('setMiddlewares')
->addMethodCall('setMiddlewares', $setMiddlewaresMethodCallArguments);
$tracingMiddlewareDefinition = $container->getDefinition(TracingDriverMiddleware::class);
$tracingMiddlewareDefinition->addTag('doctrine.middleware', ['connection' => $connectionName]);
}

private function configureConnectionForDoctrineDBALVersion2(ContainerBuilder $container, string $connectionName): void
Expand All @@ -81,20 +69,6 @@ private function configureConnectionForDoctrineDBALVersion2(ContainerBuilder $co
$connectionDefinition->setConfigurator([new Reference(ConnectionConfigurator::class), 'configure']);
}

/**
* @return mixed[]
*/
private function getSetMiddlewaresMethodCallArguments(Definition $definition): array
{
foreach ($definition->getMethodCalls() as $methodCall) {
if ('setMiddlewares' === $methodCall[0]) {
return $methodCall[1];
}
}

return [];
}

private function assertRequiredDbalVersion(): void
{
if (interface_exists(Result::class)) {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<argument type="service" id="Sentry\State\HubInterface" />
</service>

<!-- TODO: make this service abstract (see DoctrineBundle MiddlewarePass) when DBAL 2.x support is dropped -->
<service id="Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware" class="Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware">
<argument type="service" id="Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionFactoryInterface" />
</service>
Expand Down
3 changes: 2 additions & 1 deletion src/SentryBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Sentry\SentryBundle\DependencyInjection\Compiler\CacheTracingPass;
use Sentry\SentryBundle\DependencyInjection\Compiler\DbalTracingPass;
use Sentry\SentryBundle\DependencyInjection\Compiler\HttpClientTracingPass;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Bundle\Bundle;

Expand All @@ -21,7 +22,7 @@ public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(new DbalTracingPass());
$container->addCompilerPass(new DbalTracingPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, 10);
$container->addCompilerPass(new CacheTracingPass());
$container->addCompilerPass(new HttpClientTracingPass());
$container->addCompilerPass(new AddLoginListenerTagPass());
Expand Down
61 changes: 7 additions & 54 deletions tests/DependencyInjection/Compiler/DbalTracingPassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,62 +23,15 @@ public function testProcessWithDoctrineDBALVersionAtLeast30(): void
}

$container = $this->createContainerBuilder();
$container->setParameter('sentry.tracing.dbal.connections', ['foo', 'bar', 'baz']);

$container
->register('foo.service', \stdClass::class)
->setPublic(true);

$container
->register('doctrine.dbal.foo_connection.configuration', Configuration::class)
->setPublic(true);

$container
->register('doctrine.dbal.bar_connection.configuration', Configuration::class)
->addMethodCall('setMiddlewares', [[]])
->setPublic(true);

$container
->register('doctrine.dbal.baz_connection.configuration', Configuration::class)
->addMethodCall('setMiddlewares', [[new Reference('foo.service')]])
->setPublic(true);

$container->setParameter('sentry.tracing.dbal.connections', ['foo', 'bar']);
$container->compile();

$this->assertEquals(
[
[
'setMiddlewares',
[[new Reference(TracingDriverMiddleware::class)]],
],
],
$container->getDefinition('doctrine.dbal.foo_connection.configuration')->getMethodCalls()
);

$this->assertEquals(
[
[
'setMiddlewares',
[[new Reference(TracingDriverMiddleware::class)]],
],
],
$container->getDefinition('doctrine.dbal.bar_connection.configuration')->getMethodCalls()
);

$this->assertEquals(
[
[
'setMiddlewares',
[
[
new Reference('foo.service'),
new Reference(TracingDriverMiddleware::class),
],
],
],
],
$container->getDefinition('doctrine.dbal.baz_connection.configuration')->getMethodCalls()
);
$tracingMiddlewareDefinition = $container->getDefinition(TracingDriverMiddleware::class);
$doctrineMiddlewareTags = $tracingMiddlewareDefinition->getTag('doctrine.middleware');

$this->assertCount(2, $doctrineMiddlewareTags);
$this->assertSame(['connection' => 'foo'], $doctrineMiddlewareTags[0]);
$this->assertSame(['connection' => 'bar'], $doctrineMiddlewareTags[1]);
}

public function testProcessWithDoctrineDBALVersionLowerThan30(): void
Expand Down