Compare commits

..

No commits in common. "master" and "0.2.9" have entirely different histories.

3 changed files with 18 additions and 14 deletions

View File

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

View File

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

View File

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