2020
2121namespace MongoDB \Bundle \DataCollector ;
2222
23+ use LogicException ;
2324use MongoDB \Client ;
2425use MongoDB \Driver \Command ;
2526use Symfony \Component \HttpFoundation \Request ;
2627use Symfony \Component \HttpFoundation \Response ;
2728use Symfony \Component \HttpKernel \DataCollector \DataCollector ;
2829use Symfony \Component \HttpKernel \DataCollector \LateDataCollectorInterface ;
30+ use Symfony \Component \Stopwatch \Stopwatch ;
2931use Throwable ;
3032
3133use function array_diff_key ;
34+ use function spl_object_id ;
3235
3336/** @internal */
3437final class MongoDBDataCollector extends DataCollector implements LateDataCollectorInterface
@@ -41,17 +44,23 @@ final class MongoDBDataCollector extends DataCollector implements LateDataCollec
4144 private array $ requests = [];
4245
4346 public function __construct (
47+ private readonly ?Stopwatch $ stopwatch = null ,
4448 /** @var iterable<string, Client> */
4549 private readonly iterable $ clients = [],
4650 ) {
4751 }
4852
49- public function collectCommandEvent ( string $ clientName , string $ requestId , array $ data ): void
53+ public function configureClient ( Client $ client ): void
5054 {
51- if (isset ($ this ->requests [$ clientName ][$ requestId ])) {
52- $ this ->requests [$ clientName ][$ requestId ] += $ data ;
55+ $ client ->getManager ()->addSubscriber (new DriverEventSubscriber (spl_object_id ($ client ), $ this , $ this ->stopwatch ));
56+ }
57+
58+ public function collectCommandEvent (int $ clientId , string $ requestId , array $ data ): void
59+ {
60+ if (isset ($ this ->requests [$ clientId ][$ requestId ])) {
61+ $ this ->requests [$ clientId ][$ requestId ] += $ data ;
5362 } else {
54- $ this ->requests [$ clientName ][$ requestId ] = $ data ;
63+ $ this ->requests [$ clientId ][$ requestId ] = $ data ;
5564 }
5665 }
5766
@@ -61,21 +70,14 @@ public function collect(Request $request, Response $response, ?Throwable $except
6170
6271 public function lateCollect (): void
6372 {
64- $ requests = $ this ->requests ;
6573 $ requestCount = 0 ;
6674 $ errorCount = 0 ;
6775 $ durationMicros = 0 ;
6876
69- foreach ($ requests as $ clientName => $ requestsByClient ) {
70- foreach ($ requestsByClient as $ requestId => $ request ) {
71- $ requestCount ++;
72- $ durationMicros += $ request ['durationMicros ' ] ?? 0 ;
73- $ errorCount += isset ($ request ['error ' ]) ? 1 : 0 ;
74- }
75- }
76-
7777 $ clients = [];
78+ $ clientIdMap = [];
7879 foreach ($ this ->clients as $ name => $ client ) {
80+ $ clientIdMap [spl_object_id ($ client )] = $ name ;
7981 $ clients [$ name ] = [
8082 'serverBuildInfo ' => array_diff_key (
8183 (array ) $ client ->getManager ()->executeCommand ('admin ' , new Command (['buildInfo ' => 1 ]))->toArray ()[0 ],
@@ -85,6 +87,17 @@ public function lateCollect(): void
8587 ];
8688 }
8789
90+ $ requests = [];
91+ foreach ($ this ->requests as $ clientId => $ requestsByClientId ) {
92+ $ clientName = $ clientIdMap [$ clientId ] ?? throw new LogicException ('Client not found ' );
93+ foreach ($ requestsByClientId as $ requestId => $ request ) {
94+ $ requests [$ clientName ][$ requestId ] = $ request ;
95+ $ requestCount ++;
96+ $ durationMicros += $ request ['durationMicros ' ] ?? 0 ;
97+ $ errorCount += isset ($ request ['error ' ]) ? 1 : 0 ;
98+ }
99+ }
100+
88101 $ this ->data = [
89102 'clients ' => $ clients ,
90103 'requests ' => $ requests ,
0 commit comments