Skip to content

Commit aacd9be

Browse files
authored
RUST-270 Make command monitoring lazy (#114)
1 parent 99fd8ba commit aacd9be

File tree

2 files changed

+50
-57
lines changed

2 files changed

+50
-57
lines changed

src/client/executor.rs

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -114,22 +114,24 @@ impl Client {
114114
let connection_info = connection.info();
115115
let request_id = crate::cmap::conn::next_request_id();
116116

117-
let should_redact = REDACTED_COMMANDS.contains(cmd.name.to_lowercase().as_str());
118-
119-
let command_body = if should_redact {
120-
Document::new()
121-
} else {
122-
cmd.body.clone()
123-
};
124-
let command_started_event = CommandStartedEvent {
125-
command: command_body,
126-
db: cmd.target_db.clone(),
127-
command_name: cmd.name.clone(),
128-
request_id,
129-
connection: connection_info.clone(),
130-
};
131-
132-
self.send_command_started_event(command_started_event);
117+
self.emit_command_event(|handler| {
118+
let should_redact = REDACTED_COMMANDS.contains(cmd.name.to_lowercase().as_str());
119+
120+
let command_body = if should_redact {
121+
Document::new()
122+
} else {
123+
cmd.body.clone()
124+
};
125+
let command_started_event = CommandStartedEvent {
126+
command: command_body,
127+
db: cmd.target_db.clone(),
128+
command_name: cmd.name.clone(),
129+
request_id,
130+
connection: connection_info.clone(),
131+
};
132+
133+
handler.handle_command_started_event(command_started_event);
134+
});
133135

134136
let start_time = PreciseTime::now();
135137

@@ -148,31 +150,39 @@ impl Client {
148150

149151
match response_result {
150152
Err(error) => {
151-
let command_failed_event = CommandFailedEvent {
152-
duration,
153-
command_name: cmd.name,
154-
failure: error.clone(),
155-
request_id,
156-
connection: connection_info,
157-
};
158-
self.send_command_failed_event(command_failed_event);
153+
self.emit_command_event(|handler| {
154+
let command_failed_event = CommandFailedEvent {
155+
duration,
156+
command_name: cmd.name,
157+
failure: error.clone(),
158+
request_id,
159+
connection: connection_info,
160+
};
161+
162+
handler.handle_command_failed_event(command_failed_event);
163+
});
159164
Err(error)
160165
}
161166
Ok(response) => {
162-
let reply = if should_redact {
163-
Document::new()
164-
} else {
165-
response.raw_response.clone()
166-
};
167-
168-
let command_succeeded_event = CommandSucceededEvent {
169-
duration,
170-
reply,
171-
command_name: cmd.name.clone(),
172-
request_id,
173-
connection: connection_info,
174-
};
175-
self.send_command_succeeded_event(command_succeeded_event);
167+
self.emit_command_event(|handler| {
168+
let should_redact =
169+
REDACTED_COMMANDS.contains(cmd.name.to_lowercase().as_str());
170+
171+
let reply = if should_redact {
172+
Document::new()
173+
} else {
174+
response.raw_response.clone()
175+
};
176+
177+
let command_succeeded_event = CommandSucceededEvent {
178+
duration,
179+
reply,
180+
command_name: cmd.name.clone(),
181+
request_id,
182+
connection: connection_info,
183+
};
184+
handler.handle_command_succeeded_event(command_succeeded_event);
185+
});
176186
op.handle_response(response)
177187
}
178188
}

src/client/mod.rs

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ use crate::{
1616
concern::{ReadConcern, WriteConcern},
1717
db::Database,
1818
error::{ErrorKind, Result},
19-
event::command::{
20-
CommandEventHandler,
21-
CommandFailedEvent,
22-
CommandStartedEvent,
23-
CommandSucceededEvent,
24-
},
19+
event::command::CommandEventHandler,
2520
operation::ListDatabases,
2621
options::{ClientOptions, DatabaseOptions},
2722
sdam::{Server, Topology, TopologyUpdateCondvar},
@@ -98,7 +93,7 @@ impl Client {
9893
Ok(Self { inner })
9994
}
10095

101-
fn emit_command_event(&self, emit: impl FnOnce(&Arc<dyn CommandEventHandler>)) {
96+
pub(crate) fn emit_command_event(&self, emit: impl FnOnce(&Arc<dyn CommandEventHandler>)) {
10297
if let Some(ref handler) = self.inner.options.command_event_handler {
10398
emit(handler);
10499
}
@@ -166,18 +161,6 @@ impl Client {
166161
}
167162
}
168163

169-
pub(crate) fn send_command_started_event(&self, event: CommandStartedEvent) {
170-
self.emit_command_event(|handler| handler.handle_command_started_event(event.clone()));
171-
}
172-
173-
pub(crate) fn send_command_succeeded_event(&self, event: CommandSucceededEvent) {
174-
self.emit_command_event(|handler| handler.handle_command_succeeded_event(event.clone()));
175-
}
176-
177-
pub(crate) fn send_command_failed_event(&self, event: CommandFailedEvent) {
178-
self.emit_command_event(|handler| handler.handle_command_failed_event(event.clone()));
179-
}
180-
181164
fn topology(&self) -> Arc<RwLock<Topology>> {
182165
self.inner.topology.clone()
183166
}

0 commit comments

Comments
 (0)