|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\ODM\MongoDB\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 function count; |
|
12
|
|
|
use function MongoDB\Driver\Monitoring\addSubscriber; |
|
13
|
|
|
use function MongoDB\Driver\Monitoring\removeSubscriber; |
|
14
|
|
|
|
|
15
|
|
|
final class CommandLogger implements \Countable, CommandSubscriber |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var Command[] */ |
|
18
|
|
|
private $commands = []; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Command[] */ |
|
21
|
|
|
private $commandsByName = []; |
|
22
|
|
|
|
|
23
|
|
|
/** @var CommandStartedEvent[] */ |
|
24
|
|
|
private $startedCommands = []; |
|
25
|
|
|
|
|
26
|
|
|
/** @var bool */ |
|
27
|
|
|
private $registered = false; |
|
28
|
|
|
|
|
29
|
28 |
|
public function register(): void |
|
30
|
|
|
{ |
|
31
|
28 |
|
if ($this->registered) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
28 |
|
$this->registered = true; |
|
36
|
28 |
|
addSubscriber($this); |
|
37
|
28 |
|
} |
|
38
|
|
|
|
|
39
|
28 |
|
public function unregister(): void |
|
40
|
|
|
{ |
|
41
|
28 |
|
if (! $this->registered) { |
|
42
|
|
|
return; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
28 |
|
removeSubscriber($this); |
|
46
|
28 |
|
$this->registered = false; |
|
47
|
28 |
|
} |
|
48
|
|
|
|
|
49
|
28 |
|
public function commandStarted(CommandStartedEvent $event) |
|
50
|
|
|
{ |
|
51
|
28 |
|
$this->startedCommands[$event->getRequestId()] = $event; |
|
52
|
28 |
|
} |
|
53
|
|
|
|
|
54
|
28 |
|
public function commandSucceeded(CommandSucceededEvent $event) |
|
55
|
|
|
{ |
|
56
|
28 |
|
$commandStartedEvent = $this->getCommandStartedEvent($event->getRequestId()); |
|
57
|
28 |
|
if (! $commandStartedEvent) { |
|
58
|
|
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
28 |
|
$this->logCommand(Command::createForSucceededCommand($commandStartedEvent, $event)); |
|
62
|
28 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function commandFailed(CommandFailedEvent $event) |
|
65
|
|
|
{ |
|
66
|
|
|
$commandStartedEvent = $this->getCommandStartedEvent($event->getRequestId()); |
|
67
|
|
|
if (! $commandStartedEvent) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$this->logCommand(Command::createForFailedCommand($commandStartedEvent, $event)); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
13 |
|
public function clear(): void |
|
75
|
|
|
{ |
|
76
|
13 |
|
$this->commands = []; |
|
77
|
13 |
|
} |
|
78
|
|
|
|
|
79
|
15 |
|
public function count(): int |
|
80
|
|
|
{ |
|
81
|
15 |
|
return count($this->commands); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return Command[] |
|
86
|
|
|
*/ |
|
87
|
4 |
|
public function getAll(): array |
|
88
|
|
|
{ |
|
89
|
4 |
|
return $this->commands; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
28 |
|
private function getCommandStartedEvent(string $requestId): ?CommandStartedEvent |
|
93
|
|
|
{ |
|
94
|
28 |
|
$startedEvent = $this->startedCommands[$requestId] ?? null; |
|
95
|
28 |
|
unset($this->startedCommands[$requestId]); |
|
96
|
|
|
|
|
97
|
28 |
|
return $startedEvent; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
28 |
|
private function logCommand(Command $command): void |
|
101
|
|
|
{ |
|
102
|
28 |
|
$this->commands[] = $command; |
|
103
|
|
|
|
|
104
|
28 |
|
$commandName = $command->getCommandName(); |
|
105
|
28 |
|
if (! isset($this->commandsByName[$commandName])) { |
|
106
|
28 |
|
$this->commandsByName[$commandName] = []; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
28 |
|
$this->commandsByName[$commandName][] = $command; |
|
110
|
28 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|