|
| 1 | +================================ |
| 2 | +MongoDB\\Client::addSubscriber() |
| 3 | +================================ |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 1 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Definition |
| 14 | +---------- |
| 15 | + |
| 16 | +.. phpmethod:: MongoDB\\Client::addSubscriber() |
| 17 | + |
| 18 | + Registers a monitoring event subscriber with this Client. The subscriber |
| 19 | + will be notified of all events for this Client. |
| 20 | + |
| 21 | + .. code-block:: php |
| 22 | + |
| 23 | + function addSubscriber(MongoDB\Driver\Monitoring\Subscriber $subscriber): void |
| 24 | + |
| 25 | + This method has the following parameters: |
| 26 | + |
| 27 | + .. include:: /includes/apiargs/MongoDBClient-method-addSubscriber-param.rst |
| 28 | + |
| 29 | + .. note:: |
| 30 | + |
| 31 | + If $subscriber is already registered with this Client, this function |
| 32 | + is a no-op. If $subscriber is also registered globally, it will still |
| 33 | + only be notified once of each event for this Client. |
| 34 | + |
| 35 | +Errors/Exceptions |
| 36 | +----------------- |
| 37 | + |
| 38 | +.. include:: /includes/extracts/error-invalidargumentexception.rst |
| 39 | + |
| 40 | +Example |
| 41 | +------- |
| 42 | + |
| 43 | +Create a :phpclass:`MongoDB\\Driver\\Monitoring\\CommandSubscriber` that |
| 44 | +logs all events: |
| 45 | + |
| 46 | +.. code-block:: php |
| 47 | + |
| 48 | + <?php |
| 49 | + |
| 50 | + use MongoDB\Driver\Monitoring\CommandSubscriber; |
| 51 | + use MongoDB\Driver\Monitoring\CommandStartedEvent; |
| 52 | + use MongoDB\Driver\Monitoring\CommandSucceededEvent; |
| 53 | + use MongoDB\Driver\Monitoring\CommandFailedEvent; |
| 54 | + |
| 55 | + class LogCommandSubscriber implements CommandSubscriber |
| 56 | + { |
| 57 | + public function commandStarted(CommandStartedEvent $event): void |
| 58 | + { |
| 59 | + fwrite(STDERR, sprintf( |
| 60 | + 'Started command #%d "%s": %s%s', |
| 61 | + $event->getRequestId(), |
| 62 | + $event->getCommandName(), |
| 63 | + Document::fromPHP($event->getCommand())->toCanonicalExtendedJSON(), |
| 64 | + PHP_EOL, |
| 65 | + )); |
| 66 | + } |
| 67 | + |
| 68 | + public function commandSucceeded(CommandSucceededEvent $event): void |
| 69 | + { |
| 70 | + fwrite(STDERR, sprintf( |
| 71 | + 'Succeeded command #%d "%s" in %d microseconds: %s%s', |
| 72 | + $event->getRequestId(), |
| 73 | + $event->getCommandName(), |
| 74 | + $event->getDurationMicros(), |
| 75 | + json_encode($event->getReply()), |
| 76 | + PHP_EOL, |
| 77 | + )); |
| 78 | + } |
| 79 | + |
| 80 | + public function commandFailed(CommandFailedEvent $event): void |
| 81 | + { |
| 82 | + fwrite(STDERR, sprintf( |
| 83 | + 'Failed command #%d "%s" in %d microseconds: %s%s', |
| 84 | + $event->getRequestId(), |
| 85 | + $event->getCommandName(), |
| 86 | + $event->getDurationMicros(), |
| 87 | + $event->getError()->getMessage(), |
| 88 | + PHP_EOL, |
| 89 | + )); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | +The subscriber can then be registered with a Client: |
| 94 | + |
| 95 | +.. code-block:: php |
| 96 | + |
| 97 | + <?php |
| 98 | + |
| 99 | + $client = new MongoDB\Client(); |
| 100 | + $subscriber = new LogCommandSubscriber(STDERR); |
| 101 | + |
| 102 | + $client->addSubscriber($subscriber); |
| 103 | + |
| 104 | + $client->test->users->insertOne(['username' => 'alice']); |
| 105 | + |
| 106 | +The above code will log the following to err output: |
| 107 | + |
| 108 | +.. code-block:: text |
| 109 | + |
| 110 | + Started command #1 "insert": { "insert" : "users", "ordered" : true, "$db" : "test", "lsid" : { "id" : { "$binary" : { "base64" : "dKTBhZD7Qvi0vUhvR58mCA==", "subType" : "04" } } }, "documents" : [ { "username" : "alice", "_id" : { "$oid" : "655d1fca12e81018340a4fc2" } } ] } |
| 111 | + Succeeded command #1 "insert" in 3349 microseconds: {"n":1,"ok":1} |
| 112 | + |
| 113 | +See Also |
| 114 | +-------- |
| 115 | + |
| 116 | +- :phpmethod:`MongoDB\\Client::removeSubscriber()` |
| 117 | +- :php:`Application Performance Monitoring (APM) |
| 118 | + <manual/en/mongodb.tutorial.apm>` |
| 119 | +- :php:`MongoDB\\Driver\\Manager::addSubscriber() |
| 120 | + <manual/en/mongodb-driver-manager.addsubscriber>` |
| 121 | +- :php:`MongoDB\Driver\Monitoring\Subscriber |
| 122 | + <manual/en/class.mongodb-driver-monitoring-subscriber>` |
| 123 | +- :php:`MongoDB\Driver\Monitoring\CommandSubscriber |
| 124 | + <manual/en/class.mongodb-driver-monitoring-commandsubscriber>` |
0 commit comments