Skip to content

Commit e2afd97

Browse files
committed
Add stopwatch
1 parent d8e0d06 commit e2afd97

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

src/DataCollector/DriverEventSubscriber.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@
2020

2121
namespace MongoDB\Bundle\DataCollector;
2222

23-
use MongoDB\Client;
2423
use MongoDB\Driver\Monitoring\CommandFailedEvent;
2524
use MongoDB\Driver\Monitoring\CommandStartedEvent;
2625
use MongoDB\Driver\Monitoring\CommandSubscriber;
2726
use MongoDB\Driver\Monitoring\CommandSucceededEvent;
27+
use Symfony\Component\Stopwatch\Stopwatch;
2828
use Symfony\Contracts\Service\ResetInterface;
2929

3030
/** @internal */
@@ -34,10 +34,12 @@ final class DriverEventSubscriber implements CommandSubscriber, ResetInterface
3434
* @var list<CommandFailedEvent|CommandStartedEvent|CommandSucceededEvent>
3535
*/
3636
private array $events = [];
37+
private array $stopwatchEvents = [];
3738

38-
public function subscribe(Client $client): void
39-
{
40-
$client->getManager()->addSubscriber($this);
39+
public function __construct(
40+
private string $clientName,
41+
private ?Stopwatch $stopwatch = null,
42+
) {
4143
}
4244

4345
/**
@@ -51,16 +53,33 @@ public function getEvents(): array
5153
public function commandFailed(CommandFailedEvent $event): void
5254
{
5355
$this->events[] = $event;
56+
57+
if (isset($this->stopwatchEvents[$event->getRequestId()])) {
58+
$this->stopwatchEvents[$event->getRequestId()]->stop();
59+
unset($this->stopwatchEvents[$event->getRequestId()]);
60+
}
5461
}
5562

5663
public function commandStarted(CommandStartedEvent $event): void
5764
{
5865
$this->events[] = $event;
66+
67+
if ($this->stopwatch) {
68+
$this->stopwatchEvents[$event->getRequestId()] = $this->stopwatch->start(
69+
'mongodb.'.$this->clientName.'.'.$event->getCommandName(),
70+
'mongodb',
71+
);
72+
}
5973
}
6074

6175
public function commandSucceeded(CommandSucceededEvent $event): void
6276
{
6377
$this->events[] = $event;
78+
79+
if (isset($this->stopwatchEvents[$event->getRequestId()])) {
80+
$this->stopwatchEvents[$event->getRequestId()]->stop();
81+
unset($this->stopwatchEvents[$event->getRequestId()]);
82+
}
6483
}
6584

6685
public function reset(): void

src/DependencyInjection/Compiler/DataCollectorPass.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use MongoDB\Bundle\DataCollector\DriverEventSubscriber;
2424
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
2525
use Symfony\Component\DependencyInjection\ContainerBuilder;
26+
use Symfony\Component\DependencyInjection\ContainerInterface;
2627
use Symfony\Component\DependencyInjection\Definition;
2728
use Symfony\Component\DependencyInjection\Reference;
2829

@@ -40,7 +41,13 @@ public function process(ContainerBuilder $container): void
4041
// Add a subscriber to each client to collect driver events, and register the client to the data collector.
4142
foreach ($container->findTaggedServiceIds('mongodb.client', true) as $clientId => $attributes) {
4243
$subscriberId = sprintf('%s.subscriber', $clientId);
43-
$container->setDefinition($subscriberId, new Definition(DriverEventSubscriber::class));
44+
$subscriber = new Definition(DriverEventSubscriber::class);
45+
$subscriber->setArguments([
46+
$attributes[0]['name'] ?? $clientId,
47+
new Reference('debug.stopwatch', ContainerInterface::NULL_ON_INVALID_REFERENCE),
48+
]);
49+
$container->setDefinition($subscriberId, $subscriber);
50+
4451
$container->getDefinition($clientId)->addMethodCall('addSubscriber', [new Reference($subscriberId)]);
4552
$dataCollector->addMethodCall('addClient', [
4653
$attributes[0]['name'] ?? $clientId,

0 commit comments

Comments
 (0)