prometheus_track/
prometheus_track.rs

1use axum::{Router, extract::State, routing::get};
2use epoch_db::DB;
3use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle};
4use std::{
5    path::Path,
6    sync::Arc,
7    thread::{self, sleep},
8    time::Duration,
9};
10use tokio::net::TcpListener;
11
12// Function to open the server
13#[tokio::main]
14async fn server_main() {
15    let recorder_handle = Arc::new(setup_prometheus_metric_recorder());
16
17    let app = Router::new()
18        .route("/metrics", get(metrics_handler))
19        .with_state(recorder_handle);
20
21    let listener = TcpListener::bind("0.0.0.0:3001").await.unwrap();
22
23    axum::serve(listener, app).await.unwrap();
24}
25
26fn setup_prometheus_metric_recorder() -> PrometheusHandle {
27    PrometheusBuilder::new().install_recorder().unwrap()
28}
29
30async fn metrics_handler(State(state): State<Arc<PrometheusHandle>>) -> String {
31    state.render()
32}
33
34fn main() -> Result<(), Box<dyn std::error::Error>> {
35    thread::spawn(server_main);
36
37    // Let the thread build the app first
38    // and Let prometheus scrape once
39    sleep(Duration::new(15, 0));
40
41    let db = DB::new(Path::new("./databasetest")).unwrap();
42
43    db.set("H", "haha", None).unwrap();
44    db.set("HAHAHHAH", "Skib", None).unwrap();
45    db.set("HI", "h", None).unwrap();
46    db.set("Chronos", "Temporal", None).unwrap();
47    db.set("pop", "HAHAHAHH", Some(Duration::new(0, 100000)))
48        .unwrap();
49    for i in 0..1000 {
50        db.get("HI").unwrap();
51        db.set(&format!("{i}"), "h", None).unwrap();
52    }
53    db.backup_to(Path::new("./backup/")).unwrap();
54
55    Ok(())
56}
57
58// TODO: Add more comments to this example