Compare commits
2 Commits
ab1ed5a57c
...
9055142073
| Author | SHA1 | Date |
|---|---|---|
|
|
9055142073 | |
|
|
c558d8bcc7 |
|
|
@ -4,24 +4,29 @@ use axum::{
|
|||
http
|
||||
};
|
||||
use crate::structs::v3::PrometheusMetrics;
|
||||
use prometheus::{ core::Collector, Encoder, Gauge, Registry, TextEncoder};
|
||||
use std::{cell::RefCell, rc::Rc, sync::{ Arc, MutexGuard }};
|
||||
use prometheus::{ core::Collector, Encoder, Registry, TextEncoder};
|
||||
use std::sync::{ Arc, MutexGuard };
|
||||
use crate::AppState;
|
||||
use tracing::{ debug, error, info, warn, trace };
|
||||
use crate::metrics::{MetricsProcesser, MetricsValueType};
|
||||
|
||||
struct BoxCollectorProducer {
|
||||
inner : *mut dyn Collector,
|
||||
#[derive(Clone)]
|
||||
struct CloneableCollector(Arc<dyn Collector>);
|
||||
impl CloneableCollector {
|
||||
fn from_boxed(collector: Box<dyn Collector>) -> Self {
|
||||
CloneableCollector(Arc::from(collector))
|
||||
}
|
||||
fn get_collector(&self) -> Box<dyn Collector> {
|
||||
Box::new(self.clone())
|
||||
}
|
||||
}
|
||||
impl Collector for CloneableCollector {
|
||||
fn desc(&self) -> Vec<&prometheus::core::Desc> {
|
||||
self.0.desc()
|
||||
}
|
||||
|
||||
impl BoxCollectorProducer {
|
||||
pub fn new(target: Box<dyn Collector>) -> Self {
|
||||
Self {
|
||||
inner : Box::into_raw(target)
|
||||
}
|
||||
}
|
||||
pub fn new_box(&self) -> Box<dyn Collector> {
|
||||
unsafe { Box::from_raw(self.inner) }
|
||||
fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
|
||||
self.0.collect()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -33,7 +38,7 @@ impl BoxCollectorProducer {
|
|||
/// # Usage
|
||||
///
|
||||
/// ``` bash
|
||||
/// curl -X POST -d '...' 'http::/localhost:9100/update'
|
||||
/// curl -X POST -d '...' 'http://127.0.0.1:9100/update' -d ...
|
||||
/// ```
|
||||
///
|
||||
pub async fn update_metrics(
|
||||
|
|
@ -98,7 +103,7 @@ pub async fn update_metrics(
|
|||
/// # Usage
|
||||
///
|
||||
/// ``` bash
|
||||
/// curl -X GET 'http::/localhost:9100/metrics'
|
||||
/// curl -X GET 'http://127.0.0.1:9100/metrics'
|
||||
/// ```
|
||||
///
|
||||
pub async fn metrics_handler(State(state): State<Arc<AppState>>) -> String {
|
||||
|
|
@ -135,11 +140,9 @@ pub fn update_or_insert_metric<'a>(
|
|||
) -> anyhow::Result<()> {
|
||||
trace!("fn update_or_insert_metric is running");
|
||||
use prometheus::Error;
|
||||
// let mut counter = 0;
|
||||
// let ptr = Box::into_raw(metric);
|
||||
let prod = BoxCollectorProducer::new(metric);
|
||||
let prod = CloneableCollector::from_boxed(metric);
|
||||
|
||||
match registry.register(prod.new_box()) {
|
||||
match registry.register(prod.get_collector()) {
|
||||
Ok(_) => {
|
||||
info!("Metric `{}` was registered!", metric_name);
|
||||
},
|
||||
|
|
@ -148,9 +151,9 @@ pub fn update_or_insert_metric<'a>(
|
|||
match er {
|
||||
Error::AlreadyReg => {
|
||||
trace!("processing already regged metric");
|
||||
match registry.unregister(prod.new_box()) {
|
||||
match registry.unregister(prod.get_collector()) {
|
||||
Ok(_) => {
|
||||
if let Err(er) = registry.register(prod.new_box()) {
|
||||
if let Err(er) = registry.register(prod.get_collector()) {
|
||||
warn!("Cannot update metric `{}`", metric_name);
|
||||
return Err(anyhow::Error::msg(
|
||||
format!("Cannot update metric `{}` due to {}", metric_name, er)
|
||||
|
|
@ -166,29 +169,6 @@ pub fn update_or_insert_metric<'a>(
|
|||
))
|
||||
},
|
||||
}
|
||||
// use prometheus::opts;
|
||||
// use prometheus::GaugeVec;
|
||||
|
||||
// let vec = GaugeVec::new(opts!("test", "test_help"), &["label"]).unwrap();
|
||||
// // vec.with_label_values(&["default"]).set(42.0);
|
||||
// if registry.unregister(Box::new(vec)).is_err() {
|
||||
// debug!("unregister failed");
|
||||
// };
|
||||
|
||||
// let vec = GaugeVec::new(opts!("test1", "test_help1"), &["label"]).unwrap();
|
||||
// vec.with_label_values(&["goood!"]).set(412.0);
|
||||
// let _ = registry.register(Box::new(vec));
|
||||
// registry
|
||||
// .gather()
|
||||
// .iter_mut()
|
||||
// .filter(|target| target.get_name() == metric_name.trim())
|
||||
// .for_each(|family| {
|
||||
// // let prev: &mut GaugeVec = family.mut_metric()[0].mut_gauge();
|
||||
|
||||
// // GaugeVec::
|
||||
|
||||
// // info!("Metric `{}` was updated, new value - {}", metric_name, new);
|
||||
// });
|
||||
},
|
||||
_ => {
|
||||
error!("Cannot register new metric `{}` due to {}", metric_name, er);
|
||||
|
|
@ -200,8 +180,4 @@ pub fn update_or_insert_metric<'a>(
|
|||
},
|
||||
}
|
||||
Ok(())
|
||||
// registry.gather()
|
||||
// .iter()
|
||||
// .filter(|fam| fam.get_name().)
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -53,6 +53,7 @@ impl MetricsProcesser {
|
|||
if let Some(status) = metric.status {
|
||||
let vec = GaugeVec::new(opts!(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));
|
||||
}
|
||||
|
||||
|
|
@ -80,7 +81,6 @@ impl MetricsProcesser {
|
|||
},
|
||||
Err(er) => error!("Cannot create Gauge metric {} due to {}", &metric_name, er),
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
pub fn gauge_from_map_metrics(
|
||||
|
|
@ -123,11 +123,10 @@ impl MetricsProcesser {
|
|||
let gauge_vec = GaugeVec::new(opts, &[&label_name]);
|
||||
match gauge_vec {
|
||||
Ok(vec) => {
|
||||
// vec.get_metric_with_label_values(vals)
|
||||
match vec.get_metric_with_label_values(&[&label_value]) {
|
||||
Ok(metric) => {
|
||||
metric.set(metric_value); // Устанавливаем значение метрики
|
||||
return Some(metric.clone()); // Возвращаем `Gauge`
|
||||
metric.set(metric_value);
|
||||
return Some(metric.clone());
|
||||
},
|
||||
Err(er) => {
|
||||
error!("Cannot create Gauge {} due to {}", &name, er);
|
||||
|
|
|
|||
Loading…
Reference in New Issue