-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathcommand-monitoring.txt
149 lines (114 loc) · 3.65 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
.. _golang-command-monitoring:
==================
Command Monitoring
==================
.. facet::
:name: genre
:values: reference
.. meta::
:keywords: code example, operation
:description: Learn to monitor MongoDB command events using the Go driver to track query performance and resolve bottlenecks.
.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecols
Overview
--------
This guide shows you how to use the {+driver-short+} to monitor the
outcome of commands that the driver sends to your MongoDB deployment.
You might use information about command events in your
application to understand changes in query performance or resolve bottlenecks.
Subscribe to Events
-------------------
You can access details about command events by subscribing to them
in your application. The following example demonstrates how to subscribe
to the ``CommandStartedEvent`` event by instantiating a
``CommandMonitor`` and connecting to a deployment:
.. code-block:: go
var eventArray []*event.CommandStartedEvent
cmdMonitor := &event.CommandMonitor{
Started: func(ctx context.Context, e *event.CommandStartedEvent) {
eventArray = append(eventArray, e)
},
}
clientOpts := options.Client().ApplyURI(uri).SetMonitor(cmdMonitor)
client, err := mongo.Connect(clientOpts)
Event Descriptions
------------------
You can subscribe to one or more of the following command monitoring
events:
.. list-table::
:widths: 33 67
:header-rows: 1
* - Event Name
- Description
* - ``CommandStartedEvent``
- Created when a command starts.
* - ``CommandSucceededEvent``
- Created when a command succeeds.
* - ``CommandFailedEvent``
- Created when a command does not succeed.
Example Event Documents
-----------------------
The following sections show sample output for each type of command monitoring event.
CommandStartedEvent
~~~~~~~~~~~~~~~~~~~
.. code-block:: none
:copyable: false
*event.CommandStartedEvent
{
"Command": "...",
"DatabaseName": "...",
"CommandName": "...",
"RequestID": ...,
"ConnectionID": "...",
"ServerConnectionID": ...,
"ServiceID": "..."
}
CommandSucceededEvent
~~~~~~~~~~~~~~~~~~~~~
.. code-block:: none
:copyable: false
*event.CommandSucceededEvent
{
"DurationNanos": 38717583,
"Duration": 38717583,
"CommandName": "insert",
"RequestID": 13,
"ConnectionID": "...",
"ServerConnectionID": ...,
"ServiceID": null,
"Reply": "..."
}
CommandFailedEvent
~~~~~~~~~~~~~~~~~~
.. code-block:: none
:copyable: false
*event.CommandFailedEvent
{
"DurationNanos": 38717583,
"Duration": 38717583,
"CommandName": "insert",
"RequestID": 13,
"ConnectionID": "...",
"ServerConnectionID": ...,
"ServiceID": null,
"Failure": "..."
}
Additional Information
----------------------
To learn more about monitoring a MongoDB deployment, see the :website:`How
to Monitor MongoDB
</basics/how-to-monitor-mongodb-and-what-metrics-to-monitor>` article.
To learn more about performing MongoDB operations, see the
:ref:`golang-crud` guides.
API Documentation
~~~~~~~~~~~~~~~~~
To learn more about the methods and types mentioned in this
guide, see the following API documentation:
- `CommandMonitor <{+api+}/event#CommandMonitor>`__ type
- `SetMonitor() <{+api+}/mongo/options#ClientOptions.SetMonitor>`__ method
- `CommandStartedEvent <{+api+}/event#CommandStartedEvent>`__ type
- `CommandSucceededEvent <{+api+}/event#CommandSucceededEvent>`__ type
- `CommandFailedEvent <{+api+}/event#CommandFailedEvent>`__ type