prometheus_track/
prometheus_track.rs

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