exporter summary test

pull/6/head
prplV 2025-02-11 20:43:56 +03:00
parent 0dde8e7604
commit 784a6f7517
1 changed files with 60 additions and 5 deletions

View File

@ -1,17 +1,31 @@
use axum::{extract::State, routing::get, Router};
use prometheus::{Encoder, TextEncoder, Counter, Opts, Registry};
use prometheus::{proto::{Histogram, Quantile, Summary}, Counter, Encoder, Opts, Registry, TextEncoder};
use std::sync::{Arc, Mutex};
use tokio::net::TcpListener;
struct AppState {
registry: Registry,
counter: Mutex<Counter>,
sum : Mutex<Summary>,
}
async fn metrics_handler(State(state): State<Arc<AppState>>) -> String {
let encoder = TextEncoder::new();
let mut buffer = Vec::new();
let metric_families = state.registry.gather();
//
let mut fm = prometheus::proto::MetricFamily::new();
// prometheus::Registry::
let mut met = prometheus::proto::Metric::new();
let mutex_guard = state.sum.lock().unwrap();
met.set_summary(mutex_guard.clone());
// let metric = met.take_label();
fm.set_metric(vec![met].into());
fm.set_help("example summary".to_string());
fm.set_name("example_summary".to_string());
fm.set_field_type(prometheus::proto::MetricType::SUMMARY);
let mut metric_families = state.registry.gather();
metric_families.push(fm);
encoder.encode(&metric_families, &mut buffer).unwrap();
String::from_utf8(buffer).unwrap()
}
@ -22,25 +36,66 @@ async fn increment_handler(State(state): State<Arc<AppState>>) -> &'static str {
"Counter incremented"
}
async fn summary_handler(State(state): State<Arc<AppState>>) -> &'static str {
let mut sum = state.sum.lock().unwrap();
let qs = sum.get_quantile();
let new_qs: Vec<Quantile> = qs
.into_iter()
.enumerate()
.map(|(idx, q)| {
let mut q = q.to_owned();
q.set_value(q.get_value() + idx as f64);
// q.set_quantile(q.get_quantile() * 2.0);
q
})
.collect();
sum.set_quantile(new_qs.into());
"Summary changed"
}
#[tokio::main]
async fn main() {
let registry = Registry::new();
let counter_opts = Opts::new("example_counter", "Пример счётчика");
// let histogram_opts = Opts::new("example_histogram", "Пример histogram");
use prometheus::proto::{Summary, Quantile};
// use prometheus::proto::
// let guage = prometheus::ProtobufEncoder::new();
let mut sunops = Summary::new();
let mut q1 = Quantile::new();
let mut q2 = Quantile::new();
q1.set_quantile(25.0);
q2.set_quantile(75.0);
// prometheus::proto::Metric::
let vq = vec![q1, q2];
sunops.set_quantile(vq.into());
let counter = Counter::with_opts(counter_opts).unwrap();
registry.register(Box::new(counter.clone())).unwrap();
// registry.register(Box::new(prometheus::proto::MetricFamily::));
// registry.register(Box::new(sunops.clone())).unwrap();
let state = Arc::new(AppState {
registry,
counter: Mutex::new(counter),
sum : Mutex::new(sunops)
});
let app = Router::new()
.route("/metrics", get(metrics_handler))
.route("/increment", get(increment_handler))
.route("/sum", get(summary_handler))
.with_state(state.clone());
let listener = TcpListener::bind("0.0.0.0:8080").await.unwrap();
// axum::Server::from_tcp(listener).unwrap().serve(app.into_make_service()).await.unwrap();
// let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
let listener = TcpListener::bind("0.0.0.0:9100").await.unwrap();
axum::serve(listener, app).await.unwrap();
}