|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright 2023-present MongoDB, Inc. |
| 7 | + * |
| 8 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | + * you may not use this file except in compliance with the License. |
| 10 | + * You may obtain a copy of the License at |
| 11 | + * |
| 12 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 13 | + * |
| 14 | + * Unless required by applicable law or agreed to in writing, software |
| 15 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | + * See the License for the specific language governing permissions and |
| 18 | + * limitations under the License. |
| 19 | + */ |
| 20 | + |
| 21 | +namespace MongoDB\Bundle\DataCollector; |
| 22 | + |
| 23 | +use MongoDB\BSON\Document; |
| 24 | +use MongoDB\Bundle\TraceableClient; |
| 25 | +use MongoDB\Client; |
| 26 | +use MongoDB\Driver\Monitoring\CommandFailedEvent; |
| 27 | +use MongoDB\Driver\Monitoring\CommandStartedEvent; |
| 28 | +use MongoDB\Driver\Monitoring\CommandSucceededEvent; |
| 29 | +use Symfony\Component\HttpFoundation\Request; |
| 30 | +use Symfony\Component\HttpFoundation\Response; |
| 31 | +use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
| 32 | + |
| 33 | +final class MongoDBDataCollector extends DataCollector |
| 34 | +{ |
| 35 | + /** |
| 36 | + * @var list<Client> |
| 37 | + */ |
| 38 | + private array $clients = []; |
| 39 | + |
| 40 | + public function addClient(string $name, Client $client): void |
| 41 | + { |
| 42 | + $this->clients[$name] = $client; |
| 43 | + } |
| 44 | + |
| 45 | + public function collect(Request $request, Response $response, ?\Throwable $exception = null): void |
| 46 | + { |
| 47 | + foreach ($this->clients as $name => $client) { |
| 48 | + $totalTime = 0; |
| 49 | + $requestCount = 0; |
| 50 | + $errorCount = 0; |
| 51 | + $requests = []; |
| 52 | + |
| 53 | + if ($client instanceof TraceableClient) { |
| 54 | + foreach ($client->getEvents() as $event) { |
| 55 | + $requestId = $event->getRequestId(); |
| 56 | + |
| 57 | + $eventData = [ |
| 58 | + 'class' => $event::class, |
| 59 | + 'commandName' => $event->getCommandName(), |
| 60 | + 'server' => $event->getServer()->getInfo(), |
| 61 | + 'client' => $name, |
| 62 | + ]; |
| 63 | + |
| 64 | + if ($event instanceof CommandStartedEvent) { |
| 65 | + $command = (array) $event->getCommand(); |
| 66 | + unset($command['lsid'], $command['$clusterTime']); |
| 67 | + |
| 68 | + $requests[$requestId] = [ |
| 69 | + 'client' => $name, |
| 70 | + 'startedAt' => hrtime(true), |
| 71 | + 'commandName' => $event->getCommandName(), |
| 72 | + 'command' => $command, |
| 73 | + // 'server' => $event->getServer()->getInfo(), |
| 74 | + 'operationId' => $event->getOperationId(), |
| 75 | + 'database' => $event->getDatabaseName(), |
| 76 | + 'serviceId' => $event->getServiceId(), |
| 77 | + ]; |
| 78 | + ++$requestCount; |
| 79 | + } elseif ($event instanceof CommandSucceededEvent) { |
| 80 | + $requests[$requestId] += [ |
| 81 | + // 'reply' => Document::fromPHP($event->getReply()), |
| 82 | + 'duration' => $event->getDurationMicros(), |
| 83 | + 'endedAt' => hrtime(true), |
| 84 | + 'success' => true, |
| 85 | + ]; |
| 86 | + $totalTime += $event->getDurationMicros(); |
| 87 | + } elseif ($event instanceof CommandFailedEvent) { |
| 88 | + $requests[$requestId] += [ |
| 89 | + // 'reply' => Document::fromPHP($event->getReply()), |
| 90 | + 'duration' => $event->getDurationMicros(), |
| 91 | + 'error' => $event->getError(), |
| 92 | + 'success' => false, |
| 93 | + ]; |
| 94 | + $totalTime += $event->getDurationMicros(); |
| 95 | + ++$errorCount; |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + $this->data['clients'][$name] = [ |
| 101 | + 'name' => $name, |
| 102 | + 'uri' => (string) $client, |
| 103 | + 'totalTime' => $totalTime, |
| 104 | + 'requestCount' => $requestCount, |
| 105 | + 'errorCount' => $errorCount, |
| 106 | + 'requests' => $requests, |
| 107 | + ]; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public function getRequestCount(): int |
| 112 | + { |
| 113 | + return array_sum(array_column($this->data['clients'], 'requestCount')); |
| 114 | + } |
| 115 | + |
| 116 | + public function getErrorCount(): int |
| 117 | + { |
| 118 | + return array_sum(array_column($this->data['clients'], 'errorCount')); |
| 119 | + } |
| 120 | + |
| 121 | + public function getTime(): float |
| 122 | + { |
| 123 | + return array_sum(array_column($this->data['clients'], 'totalTime')); |
| 124 | + } |
| 125 | + |
| 126 | + public function getClients(): array |
| 127 | + { |
| 128 | + return $this->data['clients']; |
| 129 | + } |
| 130 | + |
| 131 | + public function getName(): string |
| 132 | + { |
| 133 | + return 'mongodb'; |
| 134 | + } |
| 135 | + |
| 136 | + public function reset(): void |
| 137 | + { |
| 138 | + // TODO: Implement reset() method. |
| 139 | + } |
| 140 | +} |
0 commit comments