Skip to content

Commit

Permalink
[aptos-cli] Add latency to telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario authored and aptos-bot committed May 7, 2022
1 parent 7c8ae83 commit 385f0da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
7 changes: 5 additions & 2 deletions crates/aptos/src/common/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{
fmt::Debug,
path::{Path, PathBuf},
str::FromStr,
time::Instant,
};
use thiserror::Error;

Expand Down Expand Up @@ -595,13 +596,15 @@ pub trait CliCommand<T: Serialize + Send>: 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
}
}

Expand Down
26 changes: 21 additions & 5 deletions crates/aptos/src/common/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::{
io::Write,
path::{Path, PathBuf},
str::FromStr,
time::{Duration, Instant},
};

shadow!(build);
Expand All @@ -50,19 +51,28 @@ pub fn prompt_yes(prompt: &str) -> bool {
}

/// Convert any successful response to Success
pub async fn to_common_success_result<T>(command: &str, result: CliTypedResult<T>) -> CliResult {
to_common_result(command, result.map(|_| "Success")).await
pub async fn to_common_success_result<T>(
command: &str,
start_time: Instant,
result: CliTypedResult<T>,
) -> CliResult {
to_common_result(command, start_time, result.map(|_| "Success")).await
}

/// For pretty printing outputs in JSON
pub async fn to_common_result<T: Serialize>(command: &str, result: CliTypedResult<T>) -> CliResult {
pub async fn to_common_result<T: Serialize>(
command: &str,
start_time: Instant,
result: CliTypedResult<T>,
) -> 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(),
Expand All @@ -79,8 +89,14 @@ pub async fn to_common_result<T: Serialize>(command: &str, result: CliTypedResul
}

/// Collect build and command metrics for better debugging of CLI
fn collect_metrics(command: &str, successful: bool, error: &str) -> HashMap<String, String> {
fn collect_metrics(
command: &str,
successful: bool,
latency: Duration,
error: &str,
) -> HashMap<String, String> {
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());
Expand Down

0 comments on commit 385f0da

Please sign in to comment.