Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adding request duration and wasm memory used metrics #33

Merged
merged 1 commit into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
13 changes: 8 additions & 5 deletions crates/http-service/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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,
Expand Down Expand Up @@ -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"
);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
70 changes: 29 additions & 41 deletions crates/runtime/src/util/metrics.rs
Original file line number Diff line number Diff line change
@@ -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<u64>, memory_used: Option<u64>) {
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(),
_ => {}
};
}
Loading