Compare commits

..

3 Commits

Author SHA1 Message Date
deployer3000 fe248da39f Merge pull request 'rc' (#22) from rc into master 2025-05-20 15:01:15 +03:00
Vladislav Drozdov 9ec3ab5e1d Merge pull request 'hotfix: metric' (#21) from feature/161 into rc
test-org/prometheus-exporter/pipeline/pr-master Build succeeded
Reviewed-on: http://git.enode/deployer3000/prometheus-exporter/pulls/21
2025-05-20 14:58:57 +03:00
prplV 8db87b9fb6 hotfix: metric
test-org/prometheus-exporter/pipeline/pr-rc Build queued... Details
2025-05-20 07:58:13 -04:00
3 changed files with 14 additions and 18 deletions

View File

@ -98,7 +98,8 @@ pub async fn update_metrics(
let gauge = MetricsProcesser::gauge_from_number( let gauge = MetricsProcesser::gauge_from_number(
&i, &i,
&metric_name, &metric_name,
&i.desc.clone().unwrap_or_else(|| std::borrow::Cow::Borrowed(&i.id)) &i.desc.clone().unwrap_or_else(|| std::borrow::Cow::Borrowed(&i.id)),
&state.prefix
); );
metrics.push(gauge); metrics.push(gauge);
}, },
@ -155,7 +156,6 @@ pub async fn metrics_handler(State(state): State<Arc<AppState>>) -> String {
let encoder = TextEncoder::new(); let encoder = TextEncoder::new();
let mut buffer = Vec::new(); let mut buffer = Vec::new();
let metric_families = registry.gather(); let metric_families = registry.gather();
// dbg!(&metric_families);
debug!("vec of metric families - {:?}", &metric_families); debug!("vec of metric families - {:?}", &metric_families);
encoder.encode(&metric_families, &mut buffer).unwrap(); encoder.encode(&metric_families, &mut buffer).unwrap();
@ -175,7 +175,6 @@ pub async fn metrics_handler(State(state): State<Arc<AppState>>) -> String {
pub fn update_or_insert_metric<'a>( pub fn update_or_insert_metric<'a>(
metric: Box<dyn Collector>, metric: Box<dyn Collector>,
registry: MutexGuard<'a, Registry>, registry: MutexGuard<'a, Registry>,
// metric_name: &str
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
trace!("fn update_or_insert_metric is running"); trace!("fn update_or_insert_metric is running");
let prod = CloneableCollector::from_boxed(metric); let prod = CloneableCollector::from_boxed(metric);
@ -246,13 +245,11 @@ impl CompareGaugeVec for GaugeVec {
for metric in old_fam.get_metric() { for metric in old_fam.get_metric() {
// labels for current old version of metric in vec // labels for current old version of metric in vec
let lables = get_hashmap_lables_from_metric(metric); let lables = get_hashmap_lables_from_metric(metric);
dbg!(&lables);
let value = metric.get_gauge().get_value(); let value = metric.get_gauge().get_value();
for new in &new { for new in &new {
for new_metric in new.get_metric() { for new_metric in new.get_metric() {
let new_lables = get_hashmap_lables_from_metric(new_metric); let new_lables = get_hashmap_lables_from_metric(new_metric);
dbg!(&new_lables);
if lables.len() != new_lables.len() { if lables.len() != new_lables.len() {
error!("Trying to save invalid metric type. Reseting changes ..."); error!("Trying to save invalid metric type. Reseting changes ...");
self = old.clone(); self = old.clone();
@ -267,11 +264,8 @@ impl CompareGaugeVec for GaugeVec {
((Some(&device), Some(&source_id)), ((Some(&device), Some(&source_id)),
(Some(&new_device), Some(&new_source_id))) => { (Some(&new_device), Some(&new_source_id))) => {
/* */ /* */
dbg!(1);
if device != new_device || source_id != new_source_id { if device != new_device || source_id != new_source_id {
dbg!(2);
self.with_label_values(&[device, source_id, status]).set(value); self.with_label_values(&[device, source_id, status]).set(value);
// continue 'outer;
} }
}, },
((Some(&device), None), ((Some(&device), None),
@ -279,7 +273,6 @@ impl CompareGaugeVec for GaugeVec {
/* */ /* */
if device != new_device { if device != new_device {
self.with_label_values(&[device, status]).set(value); self.with_label_values(&[device, status]).set(value);
// continue 'outer;
} }
}, },
_ => { /* DEAD END */}, _ => { /* DEAD END */},

View File

@ -22,7 +22,8 @@ use dotenv::dotenv;
/// Used to store and share state of the metrics `Registry` /// Used to store and share state of the metrics `Registry`
/// ///
struct AppState { struct AppState {
registry: Mutex<Registry>, registry : Mutex<Registry>,
prefix : String,
} }
#[tokio::main] #[tokio::main]
@ -41,18 +42,19 @@ async fn main() -> anyhow::Result<()> {
.with_file(false) .with_file(false)
.compact() .compact()
.init(); .init();
info!("Logger was created and configurated, dotenv vars were loaded (if exist)");
info!("Logger was created and configurated, dotenv vars were loaded (if exist)");
let prefix = std::env::var("PROMETHEUS_EXPORTER_PREFIX") let prefix = std::env::var("PROMETHEUS_EXPORTER_PREFIX")
.unwrap_or_else(|_| "zvks".to_owned()); .unwrap_or_else(|_| "zvks".to_owned());
info!("Initializing local Prometehus metrics registry ..."); info!("Initializing local Prometehus metrics registry ...");
let registry = Registry::new_custom(Some(prefix), None)?; let registry = Registry::new();
info!("Initializing shared state for Prometheus Exporter web-server ..."); info!("Initializing shared state for Prometheus Exporter web-server ...");
let state = Arc::new(AppState { let state = Arc::new(AppState {
registry: Mutex::new(registry), registry : Mutex::new(registry),
prefix : prefix,
}); });

View File

@ -45,24 +45,25 @@ impl MetricsProcesser {
metric: &MetricOutput, metric: &MetricOutput,
metric_name: &str, metric_name: &str,
metric_desc: &str, metric_desc: &str,
prefix: &str
) -> Option<Box<dyn Collector>> { ) -> Option<Box<dyn Collector>> {
trace!("fn gauge_from_number is running"); trace!("fn gauge_from_number is running");
if let Some(status) = metric.status { if let Some(status) = metric.status {
if let Some(device) = metric.device { if let Some(device) = metric.device {
if let Some(source_id) = &metric.source_id { if let Some(source_id) = &metric.source_id {
let vec = GaugeVec::new(opts!(metric.name.clone(), metric_desc), &["status", "device", "source_id"]).unwrap(); let vec = GaugeVec::new(opts!(format!("{}_{}", prefix, metric.name), metric_desc), &["status", "device", "source_id"]).unwrap();
vec.with_label_values(&[&status.to_string(), &device.to_string(), &source_id.to_string()]).set(metric.value.as_f64().unwrap_or_else(|| 0.0)); vec.with_label_values(&[&status.to_string(), &device.to_string(), &source_id.to_string()]).set(metric.value.as_f64().unwrap_or_else(|| 0.0));
debug!("processed metric: {:?}", &vec); debug!("processed metric: {:?}", &vec);
return Some(Box::new(vec)); return Some(Box::new(vec));
} }
else { else {
let vec = GaugeVec::new(opts!(metric.name.clone(), metric_desc), &["status", "device"]).unwrap(); let vec = GaugeVec::new(opts!(format!("{}_{}", prefix, metric.name), metric_desc), &["status", "device"]).unwrap();
vec.with_label_values(&[&status.to_string(), &device.to_string()]).set(metric.value.as_f64().unwrap_or_else(|| 0.0)); vec.with_label_values(&[&status.to_string(), &device.to_string()]).set(metric.value.as_f64().unwrap_or_else(|| 0.0));
debug!("processed metric: {:?}", &vec); debug!("processed metric: {:?}", &vec);
return Some(Box::new(vec)); return Some(Box::new(vec));
} }
} else { } else {
let vec = GaugeVec::new(opts!(metric.name.clone(), metric_desc), &["status"]).unwrap(); let vec = GaugeVec::new(opts!(format!("{}_{}", prefix, metric.name), metric_desc), &["status"]).unwrap();
vec.with_label_values(&[&status.to_string()]).set(metric.value.as_f64().unwrap_or_else(|| 0.0)); vec.with_label_values(&[&status.to_string()]).set(metric.value.as_f64().unwrap_or_else(|| 0.0));
debug!("processed metric: {:?}", &vec); debug!("processed metric: {:?}", &vec);
return Some(Box::new(vec)); return Some(Box::new(vec));
@ -70,7 +71,7 @@ impl MetricsProcesser {
} }
let gauge = Gauge::new( let gauge = Gauge::new(
metric.name.clone(), format!("{}_{}", prefix, metric.name),
metric_desc metric_desc
); );