|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MongoDB\Tests\UnifiedSpecTests; |
| 4 | + |
| 5 | +use MongoDB\Driver\Monitoring\CommandFailedEvent; |
| 6 | +use MongoDB\Driver\Monitoring\CommandStartedEvent; |
| 7 | +use MongoDB\Driver\Monitoring\CommandSubscriber; |
| 8 | +use MongoDB\Driver\Monitoring\CommandSucceededEvent; |
| 9 | +use MongoDB\Model\BSONArray; |
| 10 | +use function array_filter; |
| 11 | +use function array_flip; |
| 12 | +use function get_class; |
| 13 | +use function microtime; |
| 14 | +use function MongoDB\Driver\Monitoring\addSubscriber; |
| 15 | +use function MongoDB\Driver\Monitoring\removeSubscriber; |
| 16 | +use function PHPUnit\Framework\assertArrayHasKey; |
| 17 | +use function PHPUnit\Framework\assertIsObject; |
| 18 | +use function PHPUnit\Framework\assertIsString; |
| 19 | +use function PHPUnit\Framework\assertNotEmpty; |
| 20 | +use function sprintf; |
| 21 | + |
| 22 | +/** |
| 23 | + * EventCollector handles "storeEventsAsEntities" for client entities. |
| 24 | + * |
| 25 | + * Unlike EventObserver, this does not support ignoring command monitoring |
| 26 | + * events for specific commands. That said, internal/security commands that |
| 27 | + * bypass command monitoring will still be ignored. |
| 28 | + */ |
| 29 | +final class EventCollector implements CommandSubscriber |
| 30 | +{ |
| 31 | + /** @var array */ |
| 32 | + private static $supportedEvents = [ |
| 33 | + 'PoolCreatedEvent' => null, |
| 34 | + 'PoolReadyEvent' => null, |
| 35 | + 'PoolClearedEvent' => null, |
| 36 | + 'PoolClosedEvent' => null, |
| 37 | + 'ConnectionCreatedEvent' => null, |
| 38 | + 'ConnectionReadyEvent' => null, |
| 39 | + 'ConnectionClosedEvent' => null, |
| 40 | + 'ConnectionCheckOutStartedEvent' => null, |
| 41 | + 'ConnectionCheckOutFailedEvent' => null, |
| 42 | + 'ConnectionCheckedOutEvent' => null, |
| 43 | + 'ConnectionCheckedInEvent' => null, |
| 44 | + 'CommandStartedEvent' => CommandStartedEvent::class, |
| 45 | + 'CommandSucceededEvent' => CommandSucceededEvent::class, |
| 46 | + 'CommandFailedEvent' => CommandFailedEvent::class, |
| 47 | + ]; |
| 48 | + |
| 49 | + /** @var string */ |
| 50 | + private $clientId; |
| 51 | + |
| 52 | + /** @var Context */ |
| 53 | + private $context; |
| 54 | + |
| 55 | + /** @var array */ |
| 56 | + private $collectEvents = []; |
| 57 | + |
| 58 | + /** @var BSONArray */ |
| 59 | + private $eventList; |
| 60 | + |
| 61 | + public function __construct(BSONArray $eventList, array $collectEvents, string $clientId, Context $context) |
| 62 | + { |
| 63 | + assertNotEmpty($collectEvents); |
| 64 | + |
| 65 | + foreach ($collectEvents as $event) { |
| 66 | + assertIsString($event); |
| 67 | + assertArrayHasKey($event, self::$supportedEvents); |
| 68 | + |
| 69 | + /* CMAP events are "supported" only in the sense that we recognize |
| 70 | + * them in the test format; however, PHPC does not implement |
| 71 | + * connection pooling so these events cannot be collected. */ |
| 72 | + if (self::$supportedEvents[$event] !== null) { |
| 73 | + $this->collectEvents[self::$supportedEvents[$event]] = 1; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + $this->clientId = $clientId; |
| 78 | + $this->context = $context; |
| 79 | + $this->eventList = $eventList; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandfailed.php |
| 84 | + */ |
| 85 | + public function commandFailed(CommandFailedEvent $event) |
| 86 | + { |
| 87 | + $this->handleCommandMonitoringEvent($event); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandstarted.php |
| 92 | + */ |
| 93 | + public function commandStarted(CommandStartedEvent $event) |
| 94 | + { |
| 95 | + $this->handleCommandMonitoringEvent($event); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @see https://www.php.net/manual/en/mongodb-driver-monitoring-commandsubscriber.commandsucceeded.php |
| 100 | + */ |
| 101 | + public function commandSucceeded(CommandSucceededEvent $event) |
| 102 | + { |
| 103 | + $this->handleCommandMonitoringEvent($event); |
| 104 | + } |
| 105 | + |
| 106 | + public function start() |
| 107 | + { |
| 108 | + addSubscriber($this); |
| 109 | + } |
| 110 | + |
| 111 | + public function stop() |
| 112 | + { |
| 113 | + removeSubscriber($this); |
| 114 | + } |
| 115 | + |
| 116 | + /** @param CommandStartedEvent|CommandSucceededEvent|CommandFailedEvent $event */ |
| 117 | + private function handleCommandMonitoringEvent($event) |
| 118 | + { |
| 119 | + assertIsObject($event); |
| 120 | + |
| 121 | + if (! $this->context->isActiveClient($this->clientId)) { |
| 122 | + return; |
| 123 | + } |
| 124 | + |
| 125 | + if (! isset($this->collectEvents[get_class($event)])) { |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + $log = [ |
| 130 | + 'name' => self::getEventName($event), |
| 131 | + 'observedAt' => microtime(true), |
| 132 | + 'commandName' => $event->getCommandName(), |
| 133 | + 'connectionId' => self::getConnectionId($event), |
| 134 | + 'requestId' => $event->getRequestId(), |
| 135 | + 'operationId' => $event->getOperationId(), |
| 136 | + ]; |
| 137 | + |
| 138 | + /* Note: CommandStartedEvent.command and CommandSucceededEvent.reply can |
| 139 | + * be omitted from logged events. */ |
| 140 | + |
| 141 | + if ($event instanceof CommandStartedEvent) { |
| 142 | + $log['databaseName'] = $event->getDatabaseName(); |
| 143 | + } |
| 144 | + |
| 145 | + if ($event instanceof CommandSucceededEvent) { |
| 146 | + $log['duration'] = $event->getDurationMicros(); |
| 147 | + } |
| 148 | + |
| 149 | + if ($event instanceof CommandFailedEvent) { |
| 150 | + $log['failure'] = $event->getError()->getMessage(); |
| 151 | + $log['duration'] = $event->getDurationMicros(); |
| 152 | + } |
| 153 | + |
| 154 | + $this->eventList[] = $log; |
| 155 | + } |
| 156 | + |
| 157 | + /** @param CommandStartedEvent|CommandSucceededEvent|CommandFailedEvent $event */ |
| 158 | + private static function getConnectionId($event) : string |
| 159 | + { |
| 160 | + $server = $event->getServer(); |
| 161 | + |
| 162 | + return sprintf('%s:%d', $server->getHost(), $server->getPort()); |
| 163 | + } |
| 164 | + |
| 165 | + /** @param object $event */ |
| 166 | + private static function getEventName($event) : string |
| 167 | + { |
| 168 | + static $eventNamesByClass = null; |
| 169 | + |
| 170 | + if ($eventNamesByClass === null) { |
| 171 | + $eventNamesByClass = array_flip(array_filter(self::$supportedEvents)); |
| 172 | + } |
| 173 | + |
| 174 | + return $eventNamesByClass[get_class($event)]; |
| 175 | + } |
| 176 | +} |
0 commit comments