From 774a517def8cd048d4b86c6e786cc152b03bcfd8 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 20 May 2025 04:37:09 -0400 Subject: [PATCH] final fixes --- crates/api-grub/src/jitter.rs | 2 +- crates/api-grub/src/monitoring.rs | 77 ++++++++++++++++++++----------- crates/api-grub/src/net.rs | 3 +- crates/integr-structs/src/api.rs | 12 +++-- 4 files changed, 60 insertions(+), 34 deletions(-) diff --git a/crates/api-grub/src/jitter.rs b/crates/api-grub/src/jitter.rs index ab0ac93..9b0cbe6 100644 --- a/crates/api-grub/src/jitter.rs +++ b/crates/api-grub/src/jitter.rs @@ -183,7 +183,7 @@ impl Requester { error!("Error casting jitter value from participant {} (id: {}), conference - {} (id: {}). Error: {}", name, id, conf_name, conf_id, er); Value::Null }); - metrics.add(MetricOutputExtended::new_with_slices(&metric_id, "int", "Vinteo native", &desc, None, None,val)); + metrics.add(MetricOutputExtended::new_with_slices(&metric_id, &metric_id,"int", "Vinteo native", &desc, None, None,val)); }); }); metrics diff --git a/crates/api-grub/src/monitoring.rs b/crates/api-grub/src/monitoring.rs index e055b43..94622e7 100644 --- a/crates/api-grub/src/monitoring.rs +++ b/crates/api-grub/src/monitoring.rs @@ -333,26 +333,23 @@ impl MonitoringImporter { None => "undefined-device".to_string(), } }; + let device_id = device_id.trim_end_matches('"'); if let Some(links) = device.get("links") { if let Some(measures) = links.as_array() { for measure in measures.iter() { - let dola_id = measure.get("$id"); + let dola_id = measure.get("id"); let id = measure.get("measure_id"); let source = measure.get("source_id"); let desc = measure.get("name"); - dbg!(&dola_id); - dbg!(&id); - dbg!(&source); - dbg!(&desc); if id.is_some() && source.is_some() && dola_id.is_some() { - let dola_id = dola_id.unwrap().as_str().unwrap_or_else(|| "no-id"); + let dola_id = format!("measure${}", dola_id.unwrap().as_i64().unwrap_or_else(|| 0)); let id = id.unwrap().as_str().unwrap_or_else(|| "no-name"); let source = source.unwrap().as_str().unwrap_or_else(|| "no-source"); let desc = desc.unwrap_or_else(|| &Value::Null).as_str().unwrap_or_else(|| "no description"); if source.is_empty() { return Err(Error::msg("Invalid JSON in response. Wrong types of fields (`measure_id` or `source_id`)")); } - vec.push(MetricInstance::new(dola_id, id, desc, device_id.as_ref(), source)); + vec.push(MetricInstance::new(&dola_id, id, desc, device_id.as_ref(), source)); } } } @@ -510,37 +507,65 @@ impl MonitoringImporter { if id.is_none() || val.is_none() { return Err(Error::msg("Cannot get values of fields `id` and `value` from JSON Response")) } - let id = id.unwrap().as_str().unwrap_or_else(|| "").replace("$", "_"); + + let id = id.unwrap().as_str().unwrap_or_else(|| ""); + let default_meta = MetricMeta::default(); + let meta = hm.get(id).unwrap_or_else(|| &default_meta); + let id = id.replace("$", "_"); let val = val.unwrap(); + let device = meta.device.parse::().unwrap_or_else(|_| 0); if id.is_empty() { return Err(Error::msg("Empty `id` field. Invalid JSON response")) } - let default_meta = MetricMeta::default(); - let meta = hm.get(&id).unwrap_or_else(|| &default_meta); - let device = meta.device.parse::().unwrap_or_else(|_| 0); - - Ok(MetricOutputExtended { - id : id.to_owned(), - json_type : match val { + let temp = MetricOutputExtended::new_with_slices( + id.as_ref(), + &meta.name, + { + match val { Value::Number(val) => { if val.is_i64() { - "i64".to_owned() + "i64" } else if val.is_u64() { - "u64".to_owned() + "u64" } else { - "f64".to_owned() + "f64" } }, - _ => "unknown".to_owned(), + _ => "unknown", + } }, - addr : "enode.monitoring.api".to_owned(), - desc : meta.desc.clone(), - value : val.clone(), - device: Some(device), - source: Some(meta.source.clone()), - status: 0, - }) + "enode.monitoring.api", + &meta.desc, + Some(device), + Some(meta.source.clone()), + val.clone(), + ); + dbg!(temp); + todo!(); + Ok(temp) + // Ok(MetricOutputExtended { + // id : id.to_owned(), + // name : &meta.name, + // json_type : match val { + // Value::Number(val) => { + // if val.is_i64() { + // "i64".to_owned() + // } else if val.is_u64() { + // "u64".to_owned() + // } else { + // "f64".to_owned() + // } + // }, + // _ => "unknown".to_owned(), + // }, + // addr : "enode.monitoring.api".to_owned(), + // desc : meta.desc.clone(), + // value : val.clone(), + // device: Some(device), + // source: Some(meta.source.clone()), + // status: 0, + // }) } } diff --git a/crates/api-grub/src/net.rs b/crates/api-grub/src/net.rs index de1f269..5ca773a 100644 --- a/crates/api-grub/src/net.rs +++ b/crates/api-grub/src/net.rs @@ -97,9 +97,8 @@ impl<'a> ApiPoll<'a> { error!("Bad JSON in response. Error: {}", er); }, Ok(_) => { - let endpoint_name = &metrics.name; let preproc = JsonParser::parse(&metrics.measure, &response); - let preproc = PrometheusMetrics::new(&service_id, endpoint_name, preproc); + let preproc = PrometheusMetrics::new(&service_id, preproc); match Exporter::export_metrics(preproc).await { Ok(bytes) => { info!("Successfully exported {} bytes of metrics data to Prometheus", bytes); diff --git a/crates/integr-structs/src/api.rs b/crates/integr-structs/src/api.rs index 493529c..bebc9e9 100644 --- a/crates/integr-structs/src/api.rs +++ b/crates/integr-structs/src/api.rs @@ -265,6 +265,7 @@ pub mod v3 { #[derive(Serialize, Deserialize, Debug)] pub struct MetricOutputExtended { pub id : String, + pub name : String, #[serde(rename = "type")] pub json_type : String, pub addr : String, @@ -276,9 +277,10 @@ pub mod v3 { pub source: Option, } impl MetricOutputExtended { - pub fn new_with_slices(id : &str, json_type : &str, addr: &str, desc : &str, device: Option, source: Option, value : Value) -> Self { + pub fn new_with_slices(id : &str, name: &str, json_type : &str, addr: &str, desc : &str, device: Option, source: Option, value : Value) -> Self { MetricOutputExtended { id : id.to_string(), + name : name.to_string(), json_type : json_type.to_string(), addr : addr.to_string(), value : value, @@ -293,21 +295,21 @@ pub mod v3 { #[derive(Serialize, Deserialize, Debug)] pub struct PrometheusMetrics { pub service_name: String, - pub endpoint_name: String, + // pub endpoint_name: String, pub metrics: Vec, } impl PrometheusMetrics { - pub fn new(service: &str, endpoint: &str, metrics: Vec) -> Self { + pub fn new(service: &str, metrics: Vec) -> Self { Self { service_name: service.to_string(), - endpoint_name: endpoint.to_string(), + // endpoint_name: endpoint.to_string(), metrics: metrics } } pub async fn new_zvks(metrics: Vec) -> Self { Self { service_name : "zvks".to_owned(), - endpoint_name : "apiforsnmp".to_owned(), + // endpoint_name : "apiforsnmp".to_owned(), metrics : metrics, } }