/// 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 metrics; mod structs; use axum::{ routing::{get, post}, Router}; use prometheus::Registry; use std::{str::FromStr, sync::{Arc, Mutex}}; use endpoints::*; use tokio::net::TcpListener; 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, } #[tokio::main] async fn main() -> anyhow::Result<()> { dotenv().ok(); let log_level = std::env::var("PROMETHEUS_EXPORTER_LOG_LEVEL") .unwrap_or_else(|_| "INFO".to_owned()); tracing_subscriber::fmt() .with_max_level(tracing::Level::from_str(&log_level).unwrap_or_else(|_| tracing::Level::INFO)) .with_writer(std::io::stdout) .with_span_events(tracing_subscriber::fmt::format::FmtSpan::NEW) .with_line_number(false) .with_target(false) .with_file(false) .compact() .init(); info!("Logger was created and configurated, dotenv vars were loaded (if exist)"); info!("Initializing local Prometehus metrics registry ..."); let registry = Registry::new(); info!("Initializing shared state for Prometheus Exporter web-server ..."); let state = Arc::new(AppState { registry: Mutex::new(registry), }); 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 port = std::env::var("PROMETHEUS_EXPORTER_PORT") .unwrap_or_else(|_| "9100".to_owned()); let bind_address = format!("0.0.0.0:{}", &port); let listener = TcpListener::bind(bind_address).await?; info!("Serving on ...:{}", &port); axum::serve(listener, app).await?; Ok(()) }