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
};
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<dyn Collector>) -> Self {
Self {
inner : Box::into_raw(target)
}
#[derive(Clone)]
struct CloneableCollector(Arc<dyn Collector>);
impl CloneableCollector {
fn from_boxed(collector: Box<dyn Collector>) -> Self {
CloneableCollector(Arc::from(collector))
}
pub fn new_box(&self) -> Box<dyn Collector> {
unsafe { Box::from_raw(self.inner) }
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()
}
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().)
}