From 74a906b520f9b1e0aba65b38e2c3a6ecf2e4c3cf Mon Sep 17 00:00:00 2001 From: Ruslan Pislari Date: Tue, 3 Sep 2024 12:18:36 +0300 Subject: [PATCH] feat: adding request duration and wasm memory used metrics --- Cargo.toml | 2 +- crates/http-service/src/lib.rs | 13 +++--- crates/runtime/src/util/metrics.rs | 70 +++++++++++++----------------- 3 files changed, 38 insertions(+), 47 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 66fc5c6..6cb7e3c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["crates/*"] resolver = "2" [workspace.package] -version = "0.5.4" +version = "0.5.5" edition = "2021" publish = false authors = ["FastEdge Development Team"] diff --git a/crates/http-service/src/lib.rs b/crates/http-service/src/lib.rs index 97dfaeb..62e8f8c 100644 --- a/crates/http-service/src/lib.rs +++ b/crates/http-service/src/lib.rs @@ -38,6 +38,9 @@ pub mod state; pub(crate) static TRACEPARENT: &str = "traceparent"; +#[cfg(feature = "metrics")] +const HTTP_LABEL: &[&str; 1] = &["http"]; + const FASTEDGE_INTERNAL_ERROR: u16 = 530; const FASTEDGE_OUT_OF_MEMORY: u16 = 531; const FASTEDGE_EXECUTION_TIMEOUT: u16 = 532; @@ -205,7 +208,7 @@ where let app_name = match app_name_from_request(&request) { Err(error) => { #[cfg(feature = "metrics")] - metrics::metrics(AppResult::UNKNOWN); + metrics::metrics(AppResult::UNKNOWN, HTTP_LABEL, None, None); tracing::info!(cause=?error, "App name not provided"); return not_found(); } @@ -220,7 +223,7 @@ where let cfg = match self.context.lookup_by_name(&app_name).await { None => { #[cfg(feature = "metrics")] - metrics::metrics(AppResult::UNKNOWN); + metrics::metrics(AppResult::UNKNOWN, HTTP_LABEL, None, None); tracing::info!( "Request for unknown application '{}' on URL: {}", app_name, @@ -263,7 +266,7 @@ where Ok(executor) => executor, Err(error) => { #[cfg(feature = "metrics")] - metrics::metrics(AppResult::UNKNOWN); + metrics::metrics(AppResult::UNKNOWN, HTTP_LABEL, None, None); tracing::warn!(cause=?error, "failure on getting context" ); @@ -301,7 +304,7 @@ where self.context.write_stats(stat_row).await; } #[cfg(feature = "metrics")] - metrics::metrics(AppResult::SUCCESS); + metrics::metrics(AppResult::SUCCESS, &["http"], Some(time_elapsed.as_micros() as u64), Some(memory_used.as_u64())); response.headers_mut().extend(app_res_headers(cfg)); response @@ -388,7 +391,7 @@ where tracing::debug!(?fail_reason, request_id, "stats"); #[cfg(feature = "metrics")] - metrics::metrics(fail_reason); + metrics::metrics(fail_reason, HTTP_LABEL, Some(time_elapsed.as_micros() as u64), None); let builder = Response::builder().status(status_code); let res_headers = app_res_headers(cfg); diff --git a/crates/runtime/src/util/metrics.rs b/crates/runtime/src/util/metrics.rs index 3049c9b..2c79b70 100644 --- a/crates/runtime/src/util/metrics.rs +++ b/crates/runtime/src/util/metrics.rs @@ -1,54 +1,42 @@ use lazy_static::lazy_static; -use prometheus::{self, opts, register_int_counter, IntCounter}; +use prometheus::{self, register_int_counter_vec, register_histogram_vec, IntCounterVec, HistogramVec}; use crate::AppResult; lazy_static! { - static ref TOTAL_COUNT: IntCounter = - register_int_counter!("fastedge_call_count", "Total number of app calls.").unwrap(); -} + static ref TOTAL_COUNT: IntCounterVec = + register_int_counter_vec!("fastedge_call_count", "Total number of app calls.", &["executor"]).unwrap(); -lazy_static! { - static ref ERROR_COUNT: IntCounter = register_int_counter!(opts!( + static ref ERROR_COUNT: IntCounterVec = register_int_counter_vec!( "fastedge_error_total_count", - "Number of failed app calls." - )) - .unwrap(); -} -lazy_static! { - static ref UNKNOWN_COUNT: IntCounter = register_int_counter!(opts!( - "fastedge_error_unknown_count", - "Number of calls for unknown app." - )) - .unwrap(); -} -lazy_static! { - static ref TIMEOUT_COUNT: IntCounter = - register_int_counter!(opts!("fastedge_error_timeout_count", "Number of timeouts.")) - .unwrap(); -} -lazy_static! { - static ref OOM_COUNT: IntCounter = - register_int_counter!(opts!("fastedge_error_oom_count", "Number of OOMs.")).unwrap(); -} -lazy_static! { - static ref OTHER_ERROR_COUNT: IntCounter = register_int_counter!(opts!( - "fastedge_error_other_count", - "Number of other error." - )) + "Number of failed app calls.", &["executor", "reason"] + ) .unwrap(); + + static ref REQUEST_DURATION: HistogramVec = register_histogram_vec!("fastedge_request_duration", "Request duration", &["executor"]).unwrap(); + static ref MEMORY_USAGE: HistogramVec = register_histogram_vec!("fastedge_wasm_memory_used", "WASM Memory usage", &["executor"]).unwrap(); } -pub fn metrics(result: AppResult) { - TOTAL_COUNT.inc(); +pub fn metrics(result: AppResult, label: &[&str], duration: Option, memory_used: Option) { + TOTAL_COUNT.with_label_values(label).inc(); + if result != AppResult::SUCCESS { - ERROR_COUNT.inc(); + let mut values: Vec<&str> = label.iter().map(|v| *v).collect(); + match result { + AppResult::UNKNOWN => values.push("unknown"), + AppResult::TIMEOUT => values.push("timeout"), + AppResult::OOM => values.push("oom"), + AppResult::OTHER => values.push("other"), + _ => {} + }; + + ERROR_COUNT.with_label_values(values.as_slice()).inc(); + } + + if let Some(duration) = duration { + REQUEST_DURATION.with_label_values(label).observe(duration as f64); + } + if let Some(memory_used) = memory_used { + MEMORY_USAGE.with_label_values(label).observe(memory_used as f64); } - match result { - AppResult::UNKNOWN => UNKNOWN_COUNT.inc(), - AppResult::TIMEOUT => TIMEOUT_COUNT.inc(), - AppResult::OOM => OOM_COUNT.inc(), - AppResult::OTHER => OTHER_ERROR_COUNT.inc(), - _ => {} - }; }