From 90551420735a69c835a58d4b24cd8c3d36823e78 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 8 Apr 2025 03:55:25 -0400 Subject: [PATCH] SIGSEGV (Address boundary error) bug fixed! --- src/endpoints.rs | 72 ++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/src/endpoints.rs b/src/endpoints.rs index 0268cf3..7369452 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -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, -} - -impl BoxCollectorProducer { - pub fn new(target: Box) -> Self { - Self { - inner : Box::into_raw(target) - } +#[derive(Clone)] +struct CloneableCollector(Arc); +impl CloneableCollector { + fn from_boxed(collector: Box) -> Self { + CloneableCollector(Arc::from(collector)) } - pub fn new_box(&self) -> Box { - unsafe { Box::from_raw(self.inner) } + fn get_collector(&self) -> Box { + Box::new(self.clone()) + } +} +impl Collector for CloneableCollector { + fn desc(&self) -> Vec<&prometheus::core::Desc> { + self.0.desc() + } + + fn collect(&self) -> Vec { + 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>) -> 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().) - }