Compare commits
2 Commits
ebba2fa008
...
9ec3ab5e1d
| Author | SHA1 | Date |
|---|---|---|
|
|
9ec3ab5e1d | |
|
|
8db87b9fb6 |
|
|
@ -98,7 +98,8 @@ 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))
|
||||
&i.desc.clone().unwrap_or_else(|| std::borrow::Cow::Borrowed(&i.id)),
|
||||
&state.prefix
|
||||
);
|
||||
metrics.push(gauge);
|
||||
},
|
||||
|
|
@ -155,7 +156,6 @@ 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,7 +175,6 @@ 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);
|
||||
|
|
@ -246,13 +245,11 @@ 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();
|
||||
|
|
@ -267,11 +264,8 @@ 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),
|
||||
|
|
@ -279,7 +273,6 @@ impl CompareGaugeVec for GaugeVec {
|
|||
/* */
|
||||
if device != new_device {
|
||||
self.with_label_values(&[device, status]).set(value);
|
||||
// continue 'outer;
|
||||
}
|
||||
},
|
||||
_ => { /* DEAD END */},
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -22,7 +22,8 @@ use dotenv::dotenv;
|
|||
/// Used to store and share state of the metrics `Registry`
|
||||
///
|
||||
struct AppState {
|
||||
registry: Mutex<Registry>,
|
||||
registry : Mutex<Registry>,
|
||||
prefix : String,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
|
|
@ -41,18 +42,19 @@ async fn main() -> anyhow::Result<()> {
|
|||
.with_file(false)
|
||||
.compact()
|
||||
.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")
|
||||
.unwrap_or_else(|_| "zvks".to_owned());
|
||||
|
||||
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 ...");
|
||||
let state = Arc::new(AppState {
|
||||
registry: Mutex::new(registry),
|
||||
registry : Mutex::new(registry),
|
||||
prefix : prefix,
|
||||
});
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -45,24 +45,25 @@ 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!(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));
|
||||
debug!("processed metric: {:?}", &vec);
|
||||
return Some(Box::new(vec));
|
||||
}
|
||||
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));
|
||||
debug!("processed metric: {:?}", &vec);
|
||||
return Some(Box::new(vec));
|
||||
}
|
||||
} 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));
|
||||
debug!("processed metric: {:?}", &vec);
|
||||
return Some(Box::new(vec));
|
||||
|
|
@ -70,7 +71,7 @@ impl MetricsProcesser {
|
|||
}
|
||||
|
||||
let gauge = Gauge::new(
|
||||
metric.name.clone(),
|
||||
format!("{}_{}", prefix, metric.name),
|
||||
metric_desc
|
||||
);
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue