SIGSEGV (Address boundary error) bug fixed!

feature/1184
prplV 2025-04-08 03:55:25 -04:00
parent c558d8bcc7
commit 9055142073
1 changed files with 24 additions and 48 deletions

View File

@ -4,24 +4,29 @@ use axum::{
http http
}; };
use crate::structs::v3::PrometheusMetrics; use crate::structs::v3::PrometheusMetrics;
use prometheus::{ core::Collector, Encoder, Gauge, Registry, TextEncoder}; use prometheus::{ core::Collector, Encoder, Registry, TextEncoder};
use std::{cell::RefCell, rc::Rc, sync::{ Arc, MutexGuard }}; use std::sync::{ Arc, MutexGuard };
use crate::AppState; use crate::AppState;
use tracing::{ debug, error, info, warn, trace }; use tracing::{ debug, error, info, warn, trace };
use crate::metrics::{MetricsProcesser, MetricsValueType}; use crate::metrics::{MetricsProcesser, MetricsValueType};
struct BoxCollectorProducer { #[derive(Clone)]
inner : *mut dyn Collector, 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 { fn collect(&self) -> Vec<prometheus::proto::MetricFamily> {
pub fn new(target: Box<dyn Collector>) -> Self { self.0.collect()
Self {
inner : Box::into_raw(target)
}
}
pub fn new_box(&self) -> Box<dyn Collector> {
unsafe { Box::from_raw(self.inner) }
} }
} }
@ -33,7 +38,7 @@ impl BoxCollectorProducer {
/// # Usage /// # Usage
/// ///
/// ``` bash /// ``` 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( pub async fn update_metrics(
@ -98,7 +103,7 @@ pub async fn update_metrics(
/// # Usage /// # Usage
/// ///
/// ``` bash /// ``` 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 { pub async fn metrics_handler(State(state): State<Arc<AppState>>) -> String {
@ -135,11 +140,9 @@ pub fn update_or_insert_metric<'a>(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
trace!("fn update_or_insert_metric is running"); trace!("fn update_or_insert_metric is running");
use prometheus::Error; use prometheus::Error;
// let mut counter = 0; let prod = CloneableCollector::from_boxed(metric);
// let ptr = Box::into_raw(metric);
let prod = BoxCollectorProducer::new(metric);
match registry.register(prod.new_box()) { match registry.register(prod.get_collector()) {
Ok(_) => { Ok(_) => {
info!("Metric `{}` was registered!", metric_name); info!("Metric `{}` was registered!", metric_name);
}, },
@ -148,9 +151,9 @@ pub fn update_or_insert_metric<'a>(
match er { match er {
Error::AlreadyReg => { Error::AlreadyReg => {
trace!("processing already regged metric"); trace!("processing already regged metric");
match registry.unregister(prod.new_box()) { match registry.unregister(prod.get_collector()) {
Ok(_) => { 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); warn!("Cannot update metric `{}`", metric_name);
return Err(anyhow::Error::msg( return Err(anyhow::Error::msg(
format!("Cannot update metric `{}` due to {}", metric_name, er) 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); error!("Cannot register new metric `{}` due to {}", metric_name, er);
@ -200,8 +180,4 @@ pub fn update_or_insert_metric<'a>(
}, },
} }
Ok(()) Ok(())
// registry.gather()
// .iter()
// .filter(|fam| fam.get_name().)
} }