Skip to content
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

Compatibility with the new logging middleware #3

Merged
merged 1 commit into from
Jan 20, 2023
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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"phpunit/phpunit": "<8.0"
},
"require-dev": {
"doctrine/annotations": "^2.0",
"doctrine/doctrine-bundle": "^2.0",
"doctrine/orm": "^2.9",
"phpunit/phpunit": "^9.3",
Expand Down
3 changes: 3 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
->set('monitoring.metrics', MetricRegistry::class)

->set('monitoring.metric.doctrine', DoctrineMetric::class)
->args([
service('doctrine.debug_data_holder')->nullOnInvalid()
])
->tag('monitoring.metric')
->set('monitoring.metric.memory', MemoryMetric::class)
->tag('monitoring.metric')
Expand Down
5 changes: 5 additions & 0 deletions src/DependencyInjection/Compiler/AddDoctrineLoggerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public function process(ContainerBuilder $container): void
return;
}

// With DebugDataHolder, no need to use logger
if ($container->has('doctrine.debug_data_holder')) {
return;
}

$definition = $container->getDefinition('monitoring.metric.doctrine');

$connections = $container->getParameter('doctrine.connections');
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/MetricRegistryPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class MetricRegistryPass implements CompilerPassInterface
final class MetricRegistryPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/MonitoringMetricsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;

class MonitoringMetricsExtension extends Extension
final class MonitoringMetricsExtension extends Extension
{
public function load(array $configs, ContainerBuilder $container): void
{
Expand Down
2 changes: 1 addition & 1 deletion src/EventListener/MonitoringListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(MetricRegistry $metricRegistry)
public function onKernelResponse(ResponseEvent $event): void
{
$route = $event->getRequest()->attributes->get('_route');
if (null === $route || strpos($route, '_') === 0) {
if (null === $route || str_starts_with($route, '_')) {
return;
}

Expand Down
29 changes: 29 additions & 0 deletions src/Metric/DoctrineMetric.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
namespace Odandb\MonitoringMetricsBundle\Metric;

use Doctrine\DBAL\Logging\DebugStack;
use Symfony\Bridge\Doctrine\Middleware\Debug\DebugDataHolder;
use Symfony\Component\HttpFoundation\Response;

class DoctrineMetric extends AbstractMetric
{
public const HEADER_NAME = 'X-Monitor-Doctrine-Queries';

protected ?DebugDataHolder $debugDataHolder;

/**
* @var DebugStack[]
*/
private array $loggers = [];

public function __construct(?DebugDataHolder $debugDataHolder = null)
{
$this->debugDataHolder = $debugDataHolder;
}

/**
* Adds the stack logger for a connection.
*/
Expand All @@ -27,6 +35,21 @@ public function addLogger(string $name, DebugStack $logger): void
public function metric(Response $response): void
{
$queries = [];

if (null !== $this->debugDataHolder) {
foreach ($this->debugDataHolder->getData() as $name => $data) {
if (!isset($queries[$name])) {
$queries[$name] = 0;
}

$queries[$name] += count($data);
}

$response->headers->set(self::HEADER_NAME, (string) array_sum($queries));

return;
}

foreach ($this->loggers as $name => $logger) {
if (!isset($queries[$name])) {
$queries[$name] = 0;
Expand All @@ -40,6 +63,12 @@ public function metric(Response $response): void

public function reset(): void
{
if (null !== $this->debugDataHolder) {
$this->debugDataHolder->reset();

return;
}

foreach ($this->loggers as $logger) {
$logger->queries = [];
$logger->currentQuery = 0;
Expand Down
1 change: 1 addition & 0 deletions tests/Fixtures/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ framework:
annotations:
enabled: true
secret: '123456789'
http_method_override: false
router:
utf8: true
resource: '%kernel.project_dir%/routing.yaml'
Expand Down