diff --git a/crates/aptos/src/common/types.rs b/crates/aptos/src/common/types.rs index a78732166325b..b8529c0c4565d 100644 --- a/crates/aptos/src/common/types.rs +++ b/crates/aptos/src/common/types.rs @@ -21,6 +21,7 @@ use std::{ fmt::Debug, path::{Path, PathBuf}, str::FromStr, + time::Instant, }; use thiserror::Error; @@ -595,13 +596,15 @@ pub trait CliCommand: Sized + Send { /// Executes the command, and serializes it to the common JSON output type async fn execute_serialized(self) -> CliResult { let command_name = self.command_name(); - to_common_result(command_name, self.execute().await).await + let start_time = Instant::now(); + to_common_result(command_name, start_time, self.execute().await).await } /// Executes the command, and throws away Ok(result) for the string Success async fn execute_serialized_success(self) -> CliResult { let command_name = self.command_name(); - to_common_success_result(command_name, self.execute().await).await + let start_time = Instant::now(); + to_common_success_result(command_name, start_time, self.execute().await).await } } diff --git a/crates/aptos/src/common/utils.rs b/crates/aptos/src/common/utils.rs index 1d1a883c3f9d6..5c96373fad042 100644 --- a/crates/aptos/src/common/utils.rs +++ b/crates/aptos/src/common/utils.rs @@ -25,6 +25,7 @@ use std::{ io::Write, path::{Path, PathBuf}, str::FromStr, + time::{Duration, Instant}, }; shadow!(build); @@ -50,19 +51,28 @@ pub fn prompt_yes(prompt: &str) -> bool { } /// Convert any successful response to Success -pub async fn to_common_success_result(command: &str, result: CliTypedResult) -> CliResult { - to_common_result(command, result.map(|_| "Success")).await +pub async fn to_common_success_result( + command: &str, + start_time: Instant, + result: CliTypedResult, +) -> CliResult { + to_common_result(command, start_time, result.map(|_| "Success")).await } /// For pretty printing outputs in JSON -pub async fn to_common_result(command: &str, result: CliTypedResult) -> CliResult { +pub async fn to_common_result( + command: &str, + start_time: Instant, + result: CliTypedResult, +) -> CliResult { + let latency = start_time.elapsed(); let is_err = result.is_err(); let error = if let Err(ref e) = result { e.to_str() } else { "None" }; - let metrics = collect_metrics(command, !is_err, error); + let metrics = collect_metrics(command, !is_err, latency, error); aptos_telemetry::send_data( APTOS_CLI_PUSH_METRICS.to_string(), uuid::Uuid::new_v4().to_string(), @@ -79,8 +89,14 @@ pub async fn to_common_result(command: &str, result: CliTypedResul } /// Collect build and command metrics for better debugging of CLI -fn collect_metrics(command: &str, successful: bool, error: &str) -> HashMap { +fn collect_metrics( + command: &str, + successful: bool, + latency: Duration, + error: &str, +) -> HashMap { let mut metrics = HashMap::new(); + metrics.insert("Latency".to_string(), latency.as_millis().to_string()); metrics.insert("Command".to_string(), command.to_string()); metrics.insert("Successful".to_string(), successful.to_string()); metrics.insert("Error".to_string(), error.to_string());