From 00f1ddf5467e8a61704179fe07aea8f4bc5622ef Mon Sep 17 00:00:00 2001 From: Maxime David Date: Sat, 25 Jan 2025 15:39:27 +0000 Subject: [PATCH] fix: error in log reporter --- common_lib/src/report_log.rs | 90 +++++++++++++------- function-report-log-processor-rs/src/main.rs | 16 ++-- 2 files changed, 68 insertions(+), 38 deletions(-) diff --git a/common_lib/src/report_log.rs b/common_lib/src/report_log.rs index bd66220c23..2bbeefcf15 100644 --- a/common_lib/src/report_log.rs +++ b/common_lib/src/report_log.rs @@ -55,7 +55,7 @@ impl ReportLog { } impl ReportLogData { - pub fn new(report_log: &str) -> Self { + pub fn new(report_log: &str) -> Result { let regex = regex::Regex::new( r"REPORT RequestId: (?P.*)\tDuration: (?P.*) ms\tBilled Duration: (?P.*) ms\tMemory Size: (?P.*) MB\tMax Memory Used: (?P.*) MB(\tRestore Duration: (?P.*) ms\tBilled Restore Duration: (?P.*) ms)?(\tInit Duration: (?P.*) ms)?\t", ).expect("could not find the regexp"); @@ -71,45 +71,58 @@ impl ReportLogData { let insert_date = ReportLogData::iso8601(&SystemTime::now()); - ReportLogData { - request_id: dict - .get("request_id") - .expect("could not find the request_id") - .to_string(), - duration: dict - .get("duration") - .expect("could not find the duration") - .to_string(), - billed_duration: dict - .get("billed_duration") - .expect("could not find the billed_duration") - .to_string(), - memory_size: dict - .get("memory_size") - .expect("could not find the memory_size") - .to_string(), - max_memory_used: dict - .get("max_memory_used") - .expect("could not find the max_memory_used") - .to_string(), - init_duration: ReportLogData::init_duration(&dict), + let request_id = dict + .get("request_id") + .ok_or("could not find the request_id")? + .to_string(); + + let duration = dict + .get("duration") + .ok_or("could not find the duration")? + .to_string(); + + let billed_duration = dict + .get("billed_duration") + .ok_or("could not find the billed_duration")? + .to_string(); + + let memory_size = dict + .get("memory_size") + .ok_or("could not find the memory_size")? + .to_string(); + + let max_memory_used = dict + .get("max_memory_used") + .ok_or("could not find the max_memory_used")? + .to_string(); + + Ok(ReportLogData { + request_id, + duration, + billed_duration, + memory_size, + max_memory_used, + init_duration: ReportLogData::init_duration(&dict)?, billed_restore_duration: ReportLogData::billed_restore_duration(&dict), insert_date: insert_date.to_string(), - } + }) } + fn iso8601(system_time: &std::time::SystemTime) -> String { let dt: DateTime = (*system_time).into(); format!("{}", dt.format("%+")) } - fn init_duration(dict: &HashMap<&str, &str>) -> String { - match dict.get("init_duration") { - Some(init_duration) => init_duration.to_string(), - None => dict - .get("restore_duration") - .expect("could not find initDuration/restoreDuration") - .to_string(), + + fn init_duration(dict: &HashMap<&str, &str>) -> Result { + if let Some(init_duration) = dict.get("init_duration") { + return Ok(init_duration.to_string()); } + + dict.get("restore_duration") + .map(|restore_duration| restore_duration.to_string()) + .ok_or_else(|| "could not find initDuration/restoreDuration".to_string()) } + fn billed_restore_duration(dict: &HashMap<&str, &str>) -> String { match dict.get("billed_restore_duration") { Some(billed_restore_duration) => billed_restore_duration.to_string(), @@ -140,7 +153,7 @@ mod tests { #[test] fn test_report_log_details_new() { let report_log = "REPORT RequestId: 32f5cbf1-dd22-422a-a566-8965da5a3465 Duration: 8.40 ms Billed Duration: 9 ms Memory Size: 128 MB Max Memory Used: 66 MB Init Duration: 170.13 ms "; - let report_log_details = ReportLogData::new(report_log); + let report_log_details = ReportLogData::new(report_log).unwrap(); assert_eq!( report_log_details.request_id, "32f5cbf1-dd22-422a-a566-8965da5a3465" @@ -155,7 +168,7 @@ mod tests { #[test] fn test_report_log_details_new_snapstart() { let report_log = "REPORT RequestId: 1d4e1c44-b211-4a4c-972b-39c8da774f2e Duration: 36.97 ms Billed Duration: 173 ms Memory Size: 128 MB Max Memory Used: 68 MB Restore Duration: 511.56 ms Billed Restore Duration: 136 ms "; - let report_log_details = ReportLogData::new(report_log); + let report_log_details = ReportLogData::new(report_log).unwrap(); assert_eq!( report_log_details.request_id, "1d4e1c44-b211-4a4c-972b-39c8da774f2e" @@ -167,4 +180,15 @@ mod tests { assert_eq!(report_log_details.init_duration, "511.56"); assert_eq!(report_log_details.billed_restore_duration, "136"); } + + #[test] + fn test_report_log_details_not_init_duration() { + let report_log = "REPORT RequestId: 32f5cbf1-dd22-422a-a566-8965da5a3465 Duration: 8.40 ms Billed Duration: 9 ms Memory Size: 128 MB Max Memory Used: 66 MB "; + let report_log_details = ReportLogData::new(report_log); + assert!(report_log_details.is_err()); + assert_eq!( + report_log_details.err().unwrap(), + "could not find initDuration/restoreDuration" + ); + } } diff --git a/function-report-log-processor-rs/src/main.rs b/function-report-log-processor-rs/src/main.rs index 47e1db7772..f2edc15b29 100644 --- a/function-report-log-processor-rs/src/main.rs +++ b/function-report-log-processor-rs/src/main.rs @@ -48,11 +48,17 @@ async fn process_event( info!("{:?}", report_log); for log_event in data.log_events { info!("log_event = {:?}", log_event.message); - let report_log_data = ReportLogData::new(&log_event.message); - info!("report_log_data = {:?}", report_log_data); - dynamodb_manager - .insert_report_log(&report_log, &report_log_data) - .await?; + match ReportLogData::new(&log_event.message) { + Ok(report_log_data) => { + info!("report_log_data = {:?}", report_log); + dynamodb_manager + .insert_report_log(&report_log, &report_log_data) + .await?; + } + Err(error_message) => { + info!("Could not create report_log_data: {}", error_message); + } + } } Ok(Response::success()) }