Skip to content

Commit e7600e9

Browse files
authored
DOCSP-39853: Updated monitoring api (#120)
1 parent 1afb1b2 commit e7600e9

File tree

6 files changed

+63
-100
lines changed

6 files changed

+63
-100
lines changed

source/fundamentals/monitoring/cluster-monitoring.txt

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ how they affect your application.
2626
This guide includes the following sections:
2727

2828
- :ref:`Event Descriptions <rust-cluster-monitoring-descriptions>` describes
29-
the SDAM events that the driver can generate
29+
the SDAM events that the driver can generate.
3030

3131
- :ref:`Event Subscription Example <rust-cluster-monitoring-example>`
32-
provides sample code that shows how to subscribe to an SDAM event
32+
provides sample code that shows how to subscribe to SDAM events.
3333

3434
- :ref:`Example Event Documents <rust-cluster-monitoring-documents>`
35-
provides samples of each SDAM event
35+
provides samples of each SDAM event.
3636

3737
- :ref:`Additional Information <rust-cluster-addtl-info>`
3838
provides links to resources and API documentation for types
39-
and methods mentioned in this guide
39+
and methods mentioned in this guide.
4040

4141
.. _rust-cluster-monitoring-descriptions:
4242

4343
Event Descriptions
4444
------------------
4545

46-
You can subscribe to one or more of the following SDAM events:
46+
You can monitor the following SDAM events:
4747

4848
.. list-table::
4949
:widths: 33 67
@@ -85,12 +85,15 @@ You can subscribe to one or more of the following SDAM events:
8585

8686
.. _rust-cluster-monitoring-example:
8787

88-
Event Subscription Example
89-
--------------------------
88+
Event Monitoring Example
89+
------------------------
9090

91-
You can access one or more SDAM events by
92-
subscribing to them in your application. The following example connects
93-
to a MongoDB deployment and subscribes to the ``ServerOpeningEvent`` event type:
91+
You can monitor SDAM events by assigning an ``EventHandler`` instance as the value of the
92+
``sdam_event_handler`` client option. To construct an ``EventHandler``, which processes all
93+
SDAM events, use the ``callback()`` or ``async_callback()`` method.
94+
95+
The following example connects to a MongoDB deployment, instructs the client to monitor
96+
SDAM events, and prints each event:
9497

9598
.. literalinclude:: /includes/fundamentals/code-snippets/monitoring/sdam-subscribe.rs
9699
:language: rust
@@ -233,6 +236,6 @@ API Documentation
233236
To learn more about the methods and types mentioned in this
234237
guide, see the following API documentation:
235238

236-
- `SdamEventHandler <{+api+}/event/sdam/trait.SdamEventHandler.html>`__
239+
- `EventHandler <{+api+}/event/enum.EventHandler.html>`__
237240
- `ClientOptions <{+api+}/options/struct.ClientOptions.html>`__
238241
- `Client::with_options() <{+api+}/struct.Client.html#method.with_options>`__

source/fundamentals/monitoring/command-monitoring.txt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,24 @@ how the driver executes them.
2323
This guide includes the following sections:
2424

2525
- :ref:`Event Descriptions <rust-command-monitoring-descriptions>` describes
26-
the command events that the driver can generate
26+
the command events that the driver can generate.
2727

2828
- :ref:`Event Subscription Example <rust-command-monitoring-example>`
29-
provides sample code that shows how to subscribe to a command event
29+
provides sample code that shows how to subscribe to command events.
3030

3131
- :ref:`Example Event Documents <rust-command-monitoring-documents>`
32-
provides samples of each command event
32+
provides samples of each command event.
3333

3434
- :ref:`Additional Information <rust-command-monitoring-addtl-info>`
3535
provides links to resources and API documentation for types
36-
and methods mentioned in this guide
36+
and methods mentioned in this guide.
3737

3838
.. _rust-command-monitoring-descriptions:
3939

4040
Event Descriptions
4141
------------------
4242

43-
You can subscribe to one or more of the following command monitoring
44-
events:
43+
You can monitor the following command events:
4544

4645
.. list-table::
4746
:widths: 33 67
@@ -61,12 +60,15 @@ events:
6160

6261
.. _rust-command-monitoring-example:
6362

64-
Event Subscription Example
65-
--------------------------
63+
Event Monitoring Example
64+
------------------------
6665

67-
You can access one or more command events by
68-
subscribing to them in your application. The following example connects
69-
to a MongoDB deployment and subscribes to the ``CommandStartedEvent`` event type:
66+
You can monitor command events by assigning an ``EventHandler`` instance as the value of the
67+
``command_event_handler`` client option. To construct an ``EventHandler``, which processes all
68+
command events, use the ``callback()`` or ``async_callback()`` method.
69+
70+
The following example connects to a MongoDB deployment, instructs the client to monitor
71+
command events, and prints each event:
7072

7173
.. literalinclude:: /includes/fundamentals/code-snippets/monitoring/command-subscribe.rs
7274
:language: rust
@@ -144,6 +146,6 @@ API Documentation
144146
To learn more about the methods and types mentioned in this
145147
guide, see the following API documentation:
146148

147-
- `CommandEventHandler <{+api+}/event/command/trait.CommandEventHandler.html>`__
149+
- `EventHandler <{+api+}/event/enum.EventHandler.html>`__
148150
- `ClientOptions <{+api+}/options/struct.ClientOptions.html>`__
149151
- `Client::with_options() <{+api+}/struct.Client.html#method.with_options>`__

source/fundamentals/monitoring/connection-monitoring.txt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,24 @@ how the driver uses connections.
2727
This guide includes the following sections:
2828

2929
- :ref:`Event Descriptions <rust-cpm-descriptions>` describes
30-
the connection pool events that the driver can generate
30+
the connection pool events that the driver can generate.
3131

3232
- :ref:`Event Subscription Example <rust-cpm-example>`
33-
provides sample code that shows how to subscribe to a connection pool event
33+
provides sample code that shows how to subscribe to connection pool events.
3434

3535
- :ref:`Example Event Documents <rust-cpm-documents>`
36-
provides samples of each connection pool event
36+
provides samples of each connection pool event.
3737

3838
- :ref:`Additional Information <rust-cpm-addtl-info>`
3939
provides links to resources and API documentation for types
40-
and methods mentioned in this guide
40+
and methods mentioned in this guide.
4141

4242
.. _rust-cpm-descriptions:
4343

4444
Event Descriptions
4545
------------------
4646

47-
You can subscribe to one or more of the following connection pool
48-
monitoring events:
47+
You can monitor the following connection pool events:
4948

5049
.. list-table::
5150
:widths: 33 67
@@ -96,12 +95,15 @@ monitoring events:
9695

9796
.. _rust-cpm-example:
9897

99-
Event Subscription Example
100-
--------------------------
98+
Event Monitoring Example
99+
------------------------
101100

102-
You can access one or more connection pool events by
103-
subscribing to them in your application. The following example connects
104-
to a MongoDB deployment and subscribes to the ``ConnectionCreatedEvent`` event type:
101+
You can monitor connection pool events by assigning an ``EventHandler`` instance as the value
102+
of the ``cmap_event_handler`` client option. To construct an ``EventHandler``, which processes
103+
all connection pool events, use the ``callback()`` or ``async_callback()`` method.
104+
105+
The following example connects to a MongoDB deployment, instructs the client to monitor
106+
connection pool events, and prints each event:
105107

106108
.. literalinclude:: /includes/fundamentals/code-snippets/monitoring/cpm-subscribe.rs
107109
:language: rust
@@ -259,6 +261,6 @@ API Documentation
259261
To learn more about the methods and types mentioned in this
260262
guide, see the following API documentation:
261263

262-
- `CmapEventHandler <{+api+}/event/cmap/trait.CmapEventHandler.html>`__
264+
- `EventHandler <{+api+}/event/enum.EventHandler.html>`__
263265
- `ClientOptions <{+api+}/options/struct.ClientOptions.html>`__
264266
- `Client::with_options() <{+api+}/struct.Client.html#method.with_options>`__
Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
1-
use std::{ env, sync::Arc };
2-
3-
use bson::Document;
41
use mongodb::{
5-
Client,
6-
Collection,
7-
event::command::{ CommandEventHandler, CommandStartedEvent },
2+
bson::{doc, Document},
3+
event::EventHandler,
84
options::ClientOptions,
5+
Client, Collection,
96
};
107

11-
fn main() -> mongodb::error::Result<()> {
12-
let uri = "<connection string>";
13-
14-
let mut client_options = ClientOptions::parse_async(uri).await?;
8+
#[tokio::main]
9+
async fn main() -> mongodb::error::Result<()> {
1510

1611
// begin-command
17-
struct CommandStartHandler;
18-
19-
impl CommandEventHandler for CommandStartHandler {
20-
fn handle_command_started_event(&self, event: CommandStartedEvent) {
21-
eprintln!("Command started: {:?}", event);
22-
}
23-
}
24-
25-
let handler: Arc<dyn CommandEventHandler> = Arc::new(CommandStartHandler);
26-
client_options.command_event_handler = Some(handler);
12+
let mut client_options = ClientOptions::parse("<connection string>").await?;
13+
client_options.command_event_handler = Some(EventHandler::callback(|ev| println!("{:?}", ev)));
2714

2815
let client = Client::with_options(client_options)?;
2916

3017
// ... perform actions with the client to generate events
3118

3219
// end-command
33-
3420
Ok(())
3521
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
use std::{ env, sync::Arc };
2-
3-
use bson::Document;
41
use mongodb::{
5-
Client,
6-
Collection,
7-
event::cmap::{ CmapEventHandler, ConnectionCreatedEvent },
2+
bson::{doc, Document},
3+
event::EventHandler,
84
options::ClientOptions,
5+
Client, Collection,
96
};
107

11-
fn main() -> mongodb::error::Result<()> {
12-
let uri = "<connection string>";
13-
14-
let mut client_options = ClientOptions::parse_async(uri).await?;
15-
8+
#[tokio::main]
9+
async fn main() -> mongodb::error::Result<()> {
1610
// begin-cmap
17-
struct ConnectionCreatedHandler;
18-
19-
impl CmapEventHandler for ConnectionCreatedHandler {
20-
fn handle_connection_created_event(&self, event: ConnectionCreatedEvent) {
21-
eprintln!("Connection created: {:?}", event);
22-
}
23-
}
24-
25-
let handler: Arc<dyn CmapEventHandler> = Arc::new(ConnectionCreatedHandler);
26-
client_options.cmap_event_handler = Some(handler);
11+
let mut client_options = ClientOptions::parse("<connection string>").await?;
12+
client_options.cmap_event_handler = Some(EventHandler::callback(|ev| println!("{:?}", ev)));
2713

2814
let client = Client::with_options(client_options)?;
2915

3016
// ... perform actions with the client to generate events
3117

3218
// end-cmap
33-
3419
Ok(())
3520
}
Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
use std::{ env, sync::Arc };
2-
3-
use bson::Document;
41
use mongodb::{
5-
Client,
6-
Collection,
7-
event::sdam::{ SdamEventHandler, ServerOpeningEvent },
2+
bson::{doc, Document},
3+
event::EventHandler,
84
options::ClientOptions,
5+
Client, Collection,
96
};
107

11-
fn main() -> mongodb::error::Result<()> {
12-
let uri = "<connection string>";
13-
14-
let mut client_options = ClientOptions::parse_async(uri).await?;
15-
8+
#[tokio::main]
9+
async fn main() -> mongodb::error::Result<()> {
1610
// begin-sdam
17-
struct ServerOpenHandler;
18-
19-
impl SdamEventHandler for ServerOpenHandler {
20-
fn handle_server_opening_event(&self, event: ServerOpeningEvent) {
21-
eprintln!("Server opening: {:?}", event);
22-
}
23-
}
24-
25-
let handler: Arc<dyn SdamEventHandler> = Arc::new(ServerOpenHandler);
26-
client_options.sdam_event_handler = Some(handler);
11+
let mut client_options = ClientOptions::parse("<connection string>").await?;
12+
client_options.sdam_event_handler = Some(EventHandler::callback(|ev| println!("{:?}", ev)));
2713

2814
let client = Client::with_options(client_options)?;
2915

3016
// ... perform actions with the client to generate events
3117

3218
// end-sdam
33-
3419
Ok(())
3520
}

0 commit comments

Comments
 (0)