Skip to content

Commit c18f591

Browse files
committed
Add support for query loggers and command loggers
1 parent 8b8b98a commit c18f591

File tree

14 files changed

+197
-955
lines changed

14 files changed

+197
-955
lines changed

APM/PSRCommandLogger.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
namespace Doctrine\Bundle\MongoDBBundle\APM;
6+
7+
use MongoDB\Driver\Monitoring\CommandFailedEvent;
8+
use MongoDB\Driver\Monitoring\CommandStartedEvent;
9+
use MongoDB\Driver\Monitoring\CommandSubscriber;
10+
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
11+
use Psr\Log\LoggerInterface;
12+
use function json_encode;
13+
use function MongoDB\Driver\Monitoring\addSubscriber;
14+
use function MongoDB\Driver\Monitoring\removeSubscriber;
15+
16+
final class PSRCommandLogger implements CommandSubscriber
17+
{
18+
/** @var bool */
19+
private $registered = false;
20+
21+
/** @var LoggerInterface|null */
22+
private $logger;
23+
24+
/** @var string */
25+
private $prefix;
26+
27+
public function __construct(?LoggerInterface $logger, string $prefix = 'MongoDB command: ')
28+
{
29+
$this->logger = $logger;
30+
$this->prefix = $prefix;
31+
}
32+
33+
public function register(): void
34+
{
35+
if ($this->logger === null || $this->registered) {
36+
return;
37+
}
38+
39+
$this->registered = true;
40+
addSubscriber($this);
41+
}
42+
43+
public function unregister(): void
44+
{
45+
if (! $this->registered) {
46+
return;
47+
}
48+
49+
removeSubscriber($this);
50+
$this->registered = false;
51+
}
52+
53+
public function commandStarted(CommandStartedEvent $event)
54+
{
55+
if (!$this->logger) {
56+
return;
57+
}
58+
59+
$this->logger->debug($this->prefix . json_encode($event->getCommand()));
60+
}
61+
62+
public function commandSucceeded(CommandSucceededEvent $event)
63+
{
64+
}
65+
66+
public function commandFailed(CommandFailedEvent $event)
67+
{
68+
}
69+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Doctrine\Bundle\MongoDBBundle\DataCollector;
4+
5+
use Doctrine\ODM\MongoDB\APM\Command;
6+
use Doctrine\ODM\MongoDB\APM\CommandLogger;
7+
use Symfony\Component\HttpFoundation\Request;
8+
use Symfony\Component\HttpFoundation\Response;
9+
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
10+
11+
class CommandDataCollector extends DataCollector
12+
{
13+
private $commandLogger;
14+
15+
public function __construct(CommandLogger $commandLogger)
16+
{
17+
$this->commandLogger = $commandLogger;
18+
}
19+
20+
public function collect(Request $request, Response $response, \Exception $exception = null)
21+
{
22+
$this->data = [
23+
'num_commands' => count($this->commandLogger),
24+
'commands' => array_map(
25+
function (Command $command): string {
26+
return json_encode($command->getCommand());
27+
},
28+
$this->commandLogger->getAll()
29+
),
30+
];
31+
}
32+
33+
public function reset()
34+
{
35+
$this->commandLogger->clear();
36+
$this->data = [
37+
'num_commands' => 0,
38+
'commands' => [],
39+
];
40+
}
41+
42+
public function getCommandCount(): int
43+
{
44+
return $this->data['num_commands'];
45+
}
46+
47+
/**
48+
* @return string[]
49+
*/
50+
public function getCommands(): array
51+
{
52+
return $this->data['commands'];
53+
}
54+
55+
public function getName()
56+
{
57+
return 'mongodb';
58+
}
59+
}

DataCollector/PrettyDataCollector.php

Lines changed: 0 additions & 254 deletions
This file was deleted.

0 commit comments

Comments
 (0)