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