diff --git a/Cargo.toml b/Cargo.toml index 2e72484..9834493 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,7 +3,7 @@ members = ["crates/*"] resolver = "2" [workspace.package] -version = "0.3.9" +version = "0.3.10" edition = "2021" publish = false authors = ["FastEdge Development Team"] diff --git a/crates/http-service/Cargo.toml b/crates/http-service/Cargo.toml index 024d7b8..0210089 100644 --- a/crates/http-service/Cargo.toml +++ b/crates/http-service/Cargo.toml @@ -7,8 +7,8 @@ authors.workspace = true [features] default = [] -metrics = ["prometheus"] -stats = ["clickhouse"] +metrics = ["runtime/metrics"] +stats = ["runtime/stats"] [dependencies] anyhow = { workspace = true } @@ -30,12 +30,8 @@ reactor = { path = "../reactor" } runtime = { path = "../runtime" } http-backend = { path = "../http-backend" } nanoid = "0.4" -bytesize = "1.2.0" +bytesize = "1.3.0" futures = "0.3.30" -once_cell = "1.19" -prometheus = { version = "0.13.3", features = ["process"], optional = true } -serde = "1.0" -clickhouse = { version = "0.11.6", optional = true } chrono = "0.4" async-trait = "0.1" diff --git a/crates/http-service/src/executor.rs b/crates/http-service/src/executor.rs index d84b32c..25720e2 100644 --- a/crates/http-service/src/executor.rs +++ b/crates/http-service/src/executor.rs @@ -179,12 +179,4 @@ fn to_fastedge_http_method(method: &Method) -> Result { }) } -#[derive(PartialEq, Copy, Clone, Debug)] -pub enum AppResult { - SUCCESS, - #[cfg(feature = "metrics")] - UNKNOWN, - TIMEOUT, - OOM, - OTHER, -} + diff --git a/crates/http-service/src/lib.rs b/crates/http-service/src/lib.rs index 2442375..e351fbe 100644 --- a/crates/http-service/src/lib.rs +++ b/crates/http-service/src/lib.rs @@ -21,18 +21,18 @@ use wasmtime_wasi_nn::WasiNnCtx; use http_backend::Backend; use runtime::app::Status; use runtime::service::Service; -use runtime::{App, ContextT, Router, WasmEngine, WasmEngineBuilder}; +use runtime::{App, AppResult, ContextT, Router, WasmEngine, WasmEngineBuilder}; +#[cfg(feature = "metrics")] +use runtime::util::metrics; -use crate::executor::{AppResult, HttpExecutor}; +use crate::executor::{ HttpExecutor}; #[cfg(feature = "stats")] -use crate::stats::StatRow; -use crate::stats::StatsWriter; +use runtime::util::stats::StatRow; +use runtime::util::stats::StatsWriter; use crate::tls::{load_certs, load_private_key, TlsAcceptor, TlsStream}; pub mod executor; -#[cfg(feature = "metrics")] -pub mod metrics; -pub mod stats; + mod tls; pub use crate::executor::ExecutorFactory; diff --git a/crates/http-service/src/metrics.rs b/crates/http-service/src/metrics.rs deleted file mode 100644 index b26844d..0000000 --- a/crates/http-service/src/metrics.rs +++ /dev/null @@ -1,103 +0,0 @@ -use crate::executor::AppResult; -use http::{header::CONTENT_TYPE, Request, Response}; -use hyper::Body; -use once_cell::sync::Lazy; -use prometheus::{ - self, opts, register_int_counter, register_int_gauge, Encoder, IntCounter, IntGauge, - TextEncoder, -}; -use std::time::SystemTime; - -static TOTAL_COUNT: Lazy = Lazy::new(|| { - register_int_counter!(opts!("fastedge_call_count", "Total number of app calls.")).unwrap() -}); - -static ERROR_COUNT: Lazy = Lazy::new(|| { - register_int_counter!(opts!( - "fastedge_error_total_count", - "Number of failed app calls." - )) - .unwrap() -}); - -static UNKNOWN_COUNT: Lazy = Lazy::new(|| { - register_int_counter!(opts!( - "fastedge_error_unknown_count", - "Number of calls for unknown app." - )) - .unwrap() -}); - -static TIMEOUT_COUNT: Lazy = Lazy::new(|| { - register_int_counter!(opts!("fastedge_error_timeout_count", "Number of timeouts.")).unwrap() -}); - -static OOM_COUNT: Lazy = Lazy::new(|| { - register_int_counter!(opts!("fastedge_error_oom_count", "Number of OOMs.")).unwrap() -}); - -static OTHER_ERROR_COUNT: Lazy = Lazy::new(|| { - register_int_counter!(opts!( - "fastedge_error_other_count", - "Number of other error." - )) - .unwrap() -}); - -static KNOWN_APP_COUNT: Lazy = Lazy::new(|| { - register_int_gauge!(opts!( - "fastedge_known_apps_count", - "Number of configured apps." - )) - .unwrap() -}); - -static LAST_UPDATE_TIMESTAMP: Lazy = Lazy::new(|| { - register_int_gauge!(opts!( - "fastedge_last_update_ts", - "UNIX time of last config update." - )) - .unwrap() -}); - -pub async fn serve_req(_req: Request) -> Result, hyper::Error> { - let mut buffer = Vec::new(); - let encoder = TextEncoder::new(); - - let metric_families = prometheus::gather(); - encoder.encode(&metric_families, &mut buffer).unwrap(); - - let response = Response::builder() - .status(200) - .header(CONTENT_TYPE, encoder.format_type()) - .body(Body::from(buffer)) - .unwrap(); - - Ok(response) -} - -pub fn metrics(result: AppResult) { - TOTAL_COUNT.inc(); - if result != AppResult::SUCCESS { - ERROR_COUNT.inc(); - } - match result { - AppResult::UNKNOWN => UNKNOWN_COUNT.inc(), - AppResult::TIMEOUT => TIMEOUT_COUNT.inc(), - AppResult::OOM => OOM_COUNT.inc(), - AppResult::OTHER => OTHER_ERROR_COUNT.inc(), - _ => {} - }; -} - -pub fn config_update(app_count: usize) { - KNOWN_APP_COUNT.set(app_count.try_into().unwrap()); - LAST_UPDATE_TIMESTAMP.set( - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap() - .as_secs() - .try_into() - .unwrap(), - ); -} diff --git a/crates/runtime/Cargo.toml b/crates/runtime/Cargo.toml index af2ed4d..4185f06 100644 --- a/crates/runtime/Cargo.toml +++ b/crates/runtime/Cargo.toml @@ -8,6 +8,8 @@ authors.workspace = true [features] default = ["kafka_log"] kafka_log = [] +metrics = ["prometheus", "lazy_static"] +stats = ["clickhouse"] [dependencies] anyhow = { workspace = true } @@ -30,6 +32,10 @@ bytes = "1.5" serde = "1.0" serde_json = "1.0.108" chrono = { version = "0.4", features = ["serde"] } +prometheus = { version = "0.13.3", features = ["process"], optional = true } +clickhouse = { version = "0.11.6", optional = true } +lazy_static = { version = "1.4.0", optional = true } + [dev-dependencies] claims = "0.7" diff --git a/crates/runtime/src/lib.rs b/crates/runtime/src/lib.rs index a131bf4..f9f804a 100644 --- a/crates/runtime/src/lib.rs +++ b/crates/runtime/src/lib.rs @@ -21,6 +21,7 @@ pub mod registry; pub mod service; pub mod store; pub mod stub; +pub mod util; use crate::logger::Logger; use anyhow::anyhow; @@ -34,6 +35,16 @@ use wasmtime_environ::wasmparser::{Encoding, Parser, Payload}; const PREVIEW1_ADAPTER: &[u8] = include_bytes!("adapters/wasi_snapshot_preview1.reactor.wasm"); +#[derive(PartialEq, Copy, Clone, Debug)] +pub enum AppResult { + SUCCESS, + #[cfg(feature = "metrics")] + UNKNOWN, + TIMEOUT, + OOM, + OTHER, +} + pub type InstancePre = Arc>>; pub type ModuleInstancePre = Arc>>; diff --git a/crates/runtime/src/util/metrics.rs b/crates/runtime/src/util/metrics.rs new file mode 100644 index 0000000..3049c9b --- /dev/null +++ b/crates/runtime/src/util/metrics.rs @@ -0,0 +1,54 @@ +use lazy_static::lazy_static; +use prometheus::{self, opts, register_int_counter, IntCounter}; + +use crate::AppResult; + +lazy_static! { + static ref TOTAL_COUNT: IntCounter = + register_int_counter!("fastedge_call_count", "Total number of app calls.").unwrap(); +} + +lazy_static! { + static ref ERROR_COUNT: IntCounter = register_int_counter!(opts!( + "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." + )) + .unwrap(); +} + +pub fn metrics(result: AppResult) { + TOTAL_COUNT.inc(); + if result != AppResult::SUCCESS { + ERROR_COUNT.inc(); + } + match result { + AppResult::UNKNOWN => UNKNOWN_COUNT.inc(), + AppResult::TIMEOUT => TIMEOUT_COUNT.inc(), + AppResult::OOM => OOM_COUNT.inc(), + AppResult::OTHER => OTHER_ERROR_COUNT.inc(), + _ => {} + }; +} diff --git a/crates/runtime/src/util/mod.rs b/crates/runtime/src/util/mod.rs new file mode 100644 index 0000000..41c2dc3 --- /dev/null +++ b/crates/runtime/src/util/mod.rs @@ -0,0 +1,3 @@ +#[cfg(feature = "metrics")] +pub mod metrics; +pub mod stats; diff --git a/crates/http-service/src/stats.rs b/crates/runtime/src/util/stats.rs similarity index 100% rename from crates/http-service/src/stats.rs rename to crates/runtime/src/util/stats.rs diff --git a/src/main.rs b/src/main.rs index 316839b..188987d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,6 @@ use clap::{Args, Parser, Subcommand}; use http_backend::{Backend, BackendStrategy}; use http_service::executor::{ExecutorFactory, HttpExecutorImpl}; -use http_service::stats::{StatRow, StatsWriter}; use http_service::{ContextHeaders, HttpConfig, HttpService, HttpState}; use hyper::client::HttpConnector; use hyper_tls::HttpsConnector; @@ -19,6 +18,7 @@ use std::path::PathBuf; use tokio_util::sync::CancellationToken; use wasmtime::component::Component; use wasmtime::{Engine, Module}; +use runtime::util::stats::{StatRow, StatsWriter}; #[derive(Debug, Parser)] #[command(name = "cli")]