Compare commits

..

No commits in common. "0842f344cf6f25b6dac7b1c5322d4315b04c579d" and "d5f0f470d61f14c41b0935bce5b676e72d4c586c" have entirely different histories.

4 changed files with 42 additions and 47 deletions

View File

@ -1 +0,0 @@
PROMETHEUS_EXPORTER_PORT = 9100

View File

@ -13,5 +13,4 @@ tracing = "0.1.41"
tracing-subscriber = "0.3.19"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
dotenv = "0.15.0"

View File

@ -7,20 +7,9 @@ use crate::structs::v3::PrometheusMetrics;
use prometheus::{ Encoder, Gauge, Registry, TextEncoder};
use std::sync::{ Arc, MutexGuard };
use crate::AppState;
use tracing::{ debug, error, info, warn };
use tracing::{ error, info, warn };
use crate::metrics::{MetricsProcesser, MetricsValueType};
/// An `Update` endpoint
///
/// Used to registrate new metrics and to update already
/// existing in local metrics `Registry`
///
/// # Usage
///
/// ``` bash
/// curl -X POST -d '"id" : ...' 'http::/localhost:9100/update'
/// ```
///
pub async fn update_metrics(
State(state): State<Arc<AppState>>,
Json(request) : Json<PrometheusMetrics<'_>>
@ -30,7 +19,6 @@ pub async fn update_metrics(
let endpoint = &request.endpoint_name;
for i in request.metrics {
debug!("Processing metric: {:?}", &i);
let metric_name = format!("{}_{}_{}", service, endpoint, &i.id);
match MetricsProcesser::get_type_of_value(&i) {
MetricsValueType::Array |
@ -71,17 +59,6 @@ pub async fn update_metrics(
(http::StatusCode::ACCEPTED, "Ok")
}
/// An `Metrics` endpoint
///
/// Needed in sharing current local metrics `Registry` state
/// and all stored metrics
///
/// # Usage
///
/// ``` bash
/// curl -X GET 'http::/localhost:9100/metrics'
/// ```
///
pub async fn metrics_handler(State(state): State<Arc<AppState>>) -> String {
let registry = state.registry.lock();
@ -99,11 +76,6 @@ pub async fn metrics_handler(State(state): State<Arc<AppState>>) -> String {
}
}
/// A function-registrator
///
/// Registrates or updates metrics state in local
/// `Registry`
///
pub fn update_or_insert_metric<'a>(
metric: Gauge,
registry: MutexGuard<'a, Registry>,

View File

@ -1,12 +1,9 @@
/// Prometheus-exporter - a Rust binary crate to provide functional
/// of the metrics Exporter.
///
/// It handles metrics list in it's own local `Registry`
mod endpoints;
// mod logger;
mod metrics;
mod structs;
// use logger::setup_logger;
use axum::{
routing::{get, post},
Router};
@ -14,44 +11,72 @@ use prometheus::Registry;
use std::sync::{Arc, Mutex};
use endpoints::*;
use tokio::net::TcpListener;
// use log::{warn, info, error};
use tracing::info;
use dotenv::dotenv;
/// Shared state of the Web-server
///
/// Used to store and share state of the metrics `Registry`
///
struct AppState {
registry: Mutex<Registry>,
// counter: Mutex<Counter>,
// sum : Mutex<Summary>,
}
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// let _ = setup_logger().await;
tracing_subscriber::fmt()
.with_max_level(tracing::Level::DEBUG)
.init();
info!("Loading env vars from .env if exists ...");
dotenv().ok();
info!("Initializing local Prometehus metrics registry ...");
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();
// // counter.desc()
// registry.register(Box::new(counter.clone())).unwrap();
// registry.register(Box::new(prometheus::proto::MetricFamily::));
// registry.register(Box::new(sunops.clone())).unwrap();
info!("Initializing shared state for Prometheus Exporter web-server ...");
let state = Arc::new(AppState {
registry: Mutex::new(registry),
// counter: Mutex::new(counter),
// sum : Mutex::new(sunops)
});
// info!("Configurating Web-Server...");
info!("Configurating internals of Prometheus Exporter web-server...");
let app = Router::new()
.route("/metrics", get(metrics_handler))
.route("/update", post(update_metrics))
.with_state(state.clone());
let bind_address = format!("0.0.0.0:{}", std::env::var("PROMETHEUS_EXPORTER_PORT").unwrap_or_else(|_| "9100".to_owned()));
let listener = TcpListener::bind(bind_address).await.unwrap();
let listener = TcpListener::bind("0.0.0.0:9100").await.unwrap();
info!("Serving on ...:9100");
axum::serve(listener, app).await?;
Ok(())