-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathcommand-monitoring.txt
133 lines (102 loc) · 2.93 KB
/
command-monitoring.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
.. _node-command-monitoring:
==================
Command Monitoring
==================
.. facet::
:name: genre
:values: reference
.. meta::
:keywords: code example, node.js, watch, command status
:description: Monitor the success or failure of MongoDB commands by subscribing to command monitoring events in your application.
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecols
Overview
--------
This guide shows you how to monitor the success or failure of commands
sent by the driver to your MongoDB deployment.
The following sections demonstrate how to record command status in your
application and explore the information provided in these events.
Event Subscription Example
--------------------------
You can access one or more command monitoring events using the driver by
subscribing to them in your application. The following example demonstrates
connecting to a replica set and subscribing to one of the command monitoring
events created by the MongoDB deployment:
.. literalinclude:: /code-snippets/monitoring/apm-subscribe.js
:language: javascript
.. note::
Command monitoring is disabled by default. To enable command
monitoring, pass the ``monitorCommands`` option as ``true`` to
your ``MongoClient`` constructor.
Event Descriptions
------------------
You can subscribe to any of the following command monitoring events:
.. list-table::
:widths: 33 67
:header-rows: 1
* - Event Name
- Description
* - ``commandStarted``
- Created when a command is started.
* - ``commandSucceeded``
- Created when a command succeeded.
* - ``commandFailed``
- Created when a command failed.
Example Event Documents
-----------------------
The following sections show sample output for each type of command monitoring event.
commandStarted
^^^^^^^^^^^^^^
.. code-block:: javascript
:copyable: false
CommandStartedEvent {
requestId: 1534,
databaseName: "app",
commandName: "find",
address: 'localhost:27017',
connectionId: 812613,
command: {
find: { firstName: "Jane", lastName: "Doe" }
}
}
commandSucceeded
^^^^^^^^^^^^^^^^
.. code-block:: javascript
:copyable: false
CommandSucceededEvent {
requestId: 1534,
commandName: "find",
address: 'localhost:27017',
connectionId: 812613,
duration: 15,
reply: {
cursor: {
firstBatch: [
{
_id: ObjectId("5e8e2ca217b5324fa9847435"),
firstName: "Jane",
lastName: "Doe"
}
],
_id: 0,
ns: "app.users"
},
ok: 1,
operationTime: 1586380205
}
}
commandFailed
^^^^^^^^^^^^^
.. code-block:: javascript
:copyable: false
CommandFailedEvent {
requestId: 1534,
commandName: "find",
address: 'localhost:27017',
connectionId: 812613,
failure: Error("something failed"),
duration: 11
}