diff --git a/.env.example b/.env.example index 4cd9fda..bbf58ee 100644 --- a/.env.example +++ b/.env.example @@ -16,4 +16,7 @@ ENODE_MONITORING_PASSWORD = "admin_password_enode_monitoring" # # admin password # IM configuration for max level of logging info # for example DEBUG, INFO, WARN, ERROR, TRACE -IM_LOG_INFO = "INFO" \ No newline at end of file +IM_LOG_INFO = "INFO" +# IM configuration for setting up API connetion +# timeout (in secs). Default value - 10 +IM_CONNECTION_TIMEOUT = "10" \ No newline at end of file diff --git a/crates/api-grub/Cargo.toml b/crates/api-grub/Cargo.toml index 646580e..87895ad 100644 --- a/crates/api-grub/Cargo.toml +++ b/crates/api-grub/Cargo.toml @@ -2,12 +2,19 @@ name = "api-grub" version = "1.0.2" edition = "2021" +authors = ["Vladislav Drozdov "] +description = "API poller for ZVKS project" +homepage = "http://git.enode/deployer3000/integration-module/src/branch/master/crates/api-grub" +repository = "http://git.enode/deployer3000/integration-module/src/branch/master/crates/api-grub" +license = "MIT OR Apache-2.0" +keywords = ["api", "grub", "zvks"] +publish = ["kellnr"] [dependencies] serde = { version = "1.0.217", features = ["derive"] } serde_json = "1.0.135" tokio = { version = "1.43.0", features = ["full"] } -integr-structs = {path = "../integr-structs"} +integr-structs = { version = ">=0.1.0", path="../integr-structs"} anyhow = "1.0.95" chrono = "0.4.39" reqwest = { version = "0.12.12", features = ["rustls-tls", "json"] } @@ -19,4 +26,4 @@ rand = "0.9.0" sysinfo = "0.33.1" openssl = { version = "0.10", features = ["vendored"] } tracing-subscriber = "0.3.19" -tracing = "0.1.41" +tracing = "0.1.41" \ No newline at end of file diff --git a/crates/api-grub/src/monitoring.rs b/crates/api-grub/src/monitoring.rs index ecc656c..2e71e8d 100644 --- a/crates/api-grub/src/monitoring.rs +++ b/crates/api-grub/src/monitoring.rs @@ -13,6 +13,8 @@ use integr_structs::api::v3::{MetricOutputExtended, PrometheusMetricsExtended}; use tracing::{error, info, warn}; use std::collections::HashMap; +// const IM_CONNECTION_TIMEOUT: String = std::env::var("IM_CONNECTION_TIMEOUT").unwrap_or_else(|_| "10".to_string()); + /// # Fn `get_metrics_from_monitoring` /// /// A function to init pulling and exporting metrics mechanism @@ -38,7 +40,7 @@ use std::collections::HashMap; /// assert_eq!(get_metrics_from_monitoring(0, 5).await, Ok(())); /// ``` /// -#[tracing::instrument(name = "CM mechanism", skip_all)] +#[tracing::instrument(name = "cm_fn_initiator", skip_all)] pub async fn get_metrics_from_monitoring(duration: usize, delay: usize) -> anyhow::Result<()> { let timer = tokio::time::Instant::now(); @@ -94,6 +96,7 @@ pub struct MonitoringImporter { password : String, access_token : String, ts : String, + timeout : usize, } impl MonitoringImporter { @@ -115,6 +118,7 @@ impl MonitoringImporter { password : env::var("ENODE_MONITORING_PASSWORD").unwrap_or_else(|_| String::new()), access_token : String::new(), ts : String::new(), + timeout : std::env::var("IM_CONNECTION_TIMEOUT").unwrap_or_else(|_| "10".to_string()).parse().unwrap_or_else(|_| 10) } } /// Function that checks is current `MonitoringImporter` valid @@ -140,7 +144,7 @@ impl MonitoringImporter { /// /// *Also* it saves ts and access-key in it's runtime environment, /// there's no way to get access-key of session - #[tracing::instrument(name = "CM-session mechanism", skip_all)] + #[tracing::instrument(name = "cm_fn_session_start", skip_all)] pub async fn start_session(&mut self) -> anyhow::Result<()> { if !self.is_valid().await { return Err(Error::msg("Invalid eNODE-Monitoring configuration")); @@ -153,6 +157,7 @@ impl MonitoringImporter { loop { let client = client .post(&url) + .timeout(tokio::time::Duration::from_secs(self.timeout as u64)) .header("Content-Type", "application/json") .json(&fortoken); // let resp = client.send().await?; @@ -191,6 +196,7 @@ impl MonitoringImporter { let url = format!("http://{}/e-cmdb/api/query", self.ip); let client = client .post(url) + .timeout(tokio::time::Duration::from_secs(self.timeout as u64)) .header("Content-Type", "application/json") .header("access-token", &self.access_token) .json(&Query::default()); @@ -256,7 +262,12 @@ impl MonitoringImporter { let _permit = permit.acquire().await.unwrap(); let jh: JoinHandle> = tokio::spawn(async move { - Self::process_endpoint(measure.clone(), client.clone(), arc.clone(), &hm).await + Self::process_endpoint( + measure.clone(), + client.clone(), + arc.clone(), + &hm, + ).await }); jh_vec.push(jh); @@ -289,10 +300,16 @@ impl MonitoringImporter { /// a slice of measures in special format `%5B%22measure$1%22,%20%22measure$2%22%5D`. /// This is a neccesary measure to handle two types of requests and URL restrictions /// - async fn process_endpoint(measure: Arc, client: Arc, arc: Arc, hm: &HashMap) -> anyhow::Result { + async fn process_endpoint( + measure: Arc, + client: Arc, + arc: Arc, + hm: &HashMap, + ) -> anyhow::Result { tracing::trace!("Processing CM endpoint with one or more measure names"); let resp = client .get(format!("http://{}/e-nms/mirror/measure/{}", arc.ip, &measure)) + .timeout(tokio::time::Duration::from_secs(arc.timeout as u64)) .header("Content-Type", "application/json") .header("access-token", &arc.access_token) .send().await? diff --git a/crates/integr-structs/Cargo.toml b/crates/integr-structs/Cargo.toml index d510ba7..ea15533 100644 --- a/crates/integr-structs/Cargo.toml +++ b/crates/integr-structs/Cargo.toml @@ -2,6 +2,12 @@ name = "integr-structs" version = "0.1.0" edition = "2021" +description = "Structs for API poller in ZVKS project" +homepage = "http://git.enode/deployer3000/integration-module/src/branch/master/crates/integr-structs" +repository = "http://git.enode/deployer3000/integration-module/src/branch/master/crates/integr-structs" +license = "MIT OR Apache-2.0" +keywords = ["api", "grub", "zvks", "structs", "contracts"] +publish = ["kellnr"] [dependencies] anyhow = "1.0.95"