Skip to content

Commit

Permalink
Implement metric "database_queries"
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusklocke committed Jul 30, 2024
1 parent 9953a94 commit 5966383
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
13 changes: 12 additions & 1 deletion src/Infrastructure/API/Metrics/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace HexagonalPlayground\Infrastructure\API\Metrics;

use HexagonalPlayground\Infrastructure\API\Event\ResponseEvent;
use HexagonalPlayground\Infrastructure\Persistence\ORM\Event\QueryEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class EventSubscriber implements EventSubscriberInterface
Expand All @@ -17,7 +18,8 @@ public function __construct(StoreInterface $metricsStore)
public static function getSubscribedEvents(): array
{
return [
ResponseEvent::class => 'handleResponseEvent'
ResponseEvent::class => 'handleResponseEvent',
QueryEvent::class => 'handleQueryEvent'
];
}

Expand All @@ -42,4 +44,13 @@ public function handleResponseEvent(ResponseEvent $event): void
$this->metricsStore->incrementCounter('requests_auth_basic');
}
}

public function handleQueryEvent(QueryEvent $event): void
{
if ($event->getQuery() === 'SELECT 1') {
return;
}

$this->metricsStore->incrementCounter('database_queries');
}
}
16 changes: 16 additions & 0 deletions src/Infrastructure/Persistence/ORM/Event/QueryEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,20 @@ class QueryEvent
{
private string $query;
private array $params;

public function __construct(string $query, array $params = [])
{
$this->query = $query;
$this->params = $params;
}

public function getQuery(): string
{
return $this->query;
}

public function getParams(): array
{
return $this->params;
}
}
26 changes: 16 additions & 10 deletions src/Infrastructure/Persistence/ORM/Logging/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,22 @@
use Doctrine\DBAL\Driver\Middleware\AbstractConnectionMiddleware;
use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use HexagonalPlayground\Infrastructure\Persistence\ORM\Event\QueryEvent;
use HexagonalPlayground\Infrastructure\Persistence\ORM\Event\TransactionCommitEvent;
use HexagonalPlayground\Infrastructure\Persistence\ORM\Event\TransactionRollbackEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;

class Connection extends AbstractConnectionMiddleware
{
private LoggerInterface $logger;
private EventDispatcherInterface $eventDispatcher;

public function __construct(ConnectionInterface $connection, LoggerInterface $logger)
public function __construct(ConnectionInterface $connection, LoggerInterface $logger, EventDispatcherInterface $eventDispatcher)
{
parent::__construct($connection);
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
}

public function __destruct()
Expand All @@ -30,45 +36,45 @@ public function prepare(string $sql): DriverStatement
parent::prepare($sql),
$this->logger,
$sql,
$this->eventDispatcher
);
}

public function query(string $sql): Result
{
// dispatch event for "database_queries"
$this->logger->debug('Executing database query', ['sql' => $sql]);
$result = parent::query($sql);
$this->eventDispatcher->dispatch(new QueryEvent($sql));

return parent::query($sql);
return $result;
}

public function exec(string $sql): int|string
{
// dispatch event for "database_queries"
$this->logger->debug('Executing database statement', ['sql' => $sql]);
$result = parent::exec($sql);
$this->eventDispatcher->dispatch(new QueryEvent($sql));

return parent::exec($sql);
return $result;
}

public function beginTransaction(): void
{
$this->logger->debug('Beginning database transaction');

parent::beginTransaction();
}

public function commit(): void
{
// dispatch event for "database_transaction_commits"
$this->logger->debug('Committing database transaction');

parent::commit();
$this->eventDispatcher->dispatch(new TransactionCommitEvent());
}

public function rollBack(): void
{
// dispatch event for "database_transaction_rollbacks"
$this->logger->debug('Rolling back database transaction');

parent::rollBack();
$this->eventDispatcher->dispatch(new TransactionRollbackEvent());
}
}
6 changes: 5 additions & 1 deletion src/Infrastructure/Persistence/ORM/Logging/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Middleware\AbstractDriverMiddleware;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;

class Driver extends AbstractDriverMiddleware
{
private LoggerInterface $logger;
private EventDispatcherInterface $eventDispatcher;

public function __construct(DriverInterface $driver, LoggerInterface $logger)
public function __construct(DriverInterface $driver, LoggerInterface $logger, EventDispatcherInterface $eventDispatcher)
{
parent::__construct($driver);
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -27,6 +30,7 @@ public function connect(array $params): Connection
return new Connection(
parent::connect($params),
$this->logger,
$this->eventDispatcher
);
}

Expand Down
7 changes: 5 additions & 2 deletions src/Infrastructure/Persistence/ORM/Logging/Middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@

use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;

class Middleware implements MiddlewareInterface
{
private LoggerInterface $logger;
private EventDispatcherInterface $eventDispatcher;

public function __construct(LoggerInterface $logger)
public function __construct(LoggerInterface $logger, EventDispatcherInterface $eventDispatcher)
{
$this->logger = $logger;
$this->eventDispatcher = $eventDispatcher;
}

public function wrap(DriverInterface $driver): DriverInterface
{
return new Driver($driver, $this->logger);
return new Driver($driver, $this->logger, $this->eventDispatcher);
}
}
11 changes: 8 additions & 3 deletions src/Infrastructure/Persistence/ORM/Logging/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@
use Doctrine\DBAL\Driver\Result as ResultInterface;
use Doctrine\DBAL\Driver\Statement as StatementInterface;
use Doctrine\DBAL\ParameterType;
use HexagonalPlayground\Infrastructure\Persistence\ORM\Event\QueryEvent;
use Psr\EventDispatcher\EventDispatcherInterface;
use Psr\Log\LoggerInterface;

class Statement extends AbstractStatementMiddleware
{
private LoggerInterface $logger;
private string $sql;
private array $params = [];
private EventDispatcherInterface $eventDispatcher;

public function __construct(
StatementInterface $statement,
LoggerInterface $logger,
string $sql,
EventDispatcherInterface $eventDispatcher
) {
parent::__construct($statement);
$this->logger = $logger;
$this->sql = $sql;
$this->eventDispatcher = $eventDispatcher;
}

public function bindValue(int|string $param, mixed $value, ParameterType $type): void
Expand All @@ -34,13 +39,13 @@ public function bindValue(int|string $param, mixed $value, ParameterType $type):

public function execute(): ResultInterface
{
// dispatch event for "database_queries"

$this->logger->debug('Executing database statement', [
'sql' => $this->sql,
'params' => $this->params
]);
$result = parent::execute();
$this->eventDispatcher->dispatch(new QueryEvent($this->sql, $this->params));

return parent::execute();
return $result;
}
}
3 changes: 3 additions & 0 deletions tests/Metrics/MetricsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ public function testMetricsCanBeQueried(): void
// Memory metrics
self::assertMatchesRegularExpression('/^memory_usage \d+$/m', $metrics);
self::assertMatchesRegularExpression('/^memory_peak_usage \d+$/m', $metrics);

// Database metrics
self::assertMatchesRegularExpression('/^database_queries \d+$/m', $metrics);
}
}

0 comments on commit 5966383

Please sign in to comment.