Skip to content

Commit 25b3894

Browse files
committed
Implement basic prometheus metrics
Signed-off-by: Denys Fedoryshchenko <denys.f@collabora.com>
1 parent a317aa8 commit 25b3894

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ reqwest = { version = "0.12.9", features = ["blocking"] }
2525
rustls = "0.23.20"
2626
serde = { version = "1.0.216", features = ["derive"] }
2727
sha2 = "0.10.8"
28+
sysinfo = { version = "0.35.2", features = ["serde"] }
2829
tempfile = "3.14.0"
2930
tokio = { version = "1.42.0", features = ["rt", "rt-multi-thread", "macros"] }
3031
tokio-util = "0.7.13"

src/main.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use toml::Table;
3333
use tower::ServiceBuilder;
3434
use std::{collections::HashMap, sync::Arc};
3535
use tokio::sync::{RwLock, Semaphore};
36+
use sysinfo::Disks;
3637

3738
#[derive(Parser, Debug)]
3839
#[command(version, about, long_about = None)]
@@ -193,6 +194,34 @@ async fn initial_setup() -> Option<RustlsConfig> {
193194
}
194195
}
195196

197+
async fn ax_metrics() -> (StatusCode, String) {
198+
/*
199+
Prometheus metrics:
200+
storage_files_cached NNN
201+
storage_free_space NNN
202+
*/
203+
let mut metrics = String::new();
204+
// prometehus header
205+
metrics.push_str("# HELP storage_free_space Free space on the disk\n");
206+
metrics.push_str("# TYPE storage_free_space gauge\n");
207+
metrics.push_str("# HELP storage_total_space Total space on the disk\n");
208+
metrics.push_str("# TYPE storage_total_space gauge\n");
209+
let hostname = sysinfo::System::host_name().unwrap_or_else(|| "<unknown>".to_owned());
210+
211+
let disks = Disks::new_with_refreshed_list();
212+
for disk in disks.list() {
213+
// name, mount_point, total_space, available_space
214+
let tag_diskname = disk.name().to_string_lossy();
215+
let tag_mount_point = disk.mount_point().to_string_lossy();
216+
let tag_total_space = disk.total_space();
217+
let tag_available_space = disk.available_space();
218+
219+
metrics.push_str(&format!("storage_free_space {{hostname=\"{}\", diskname=\"{}\", mount_point=\"{}\"}} {}\n", hostname, tag_diskname, tag_mount_point, tag_available_space));
220+
metrics.push_str(&format!("storage_total_space {{hostname=\"{}\", diskname=\"{}\", mount_point=\"{}\"}} {}\n", hostname, tag_diskname, tag_mount_point, tag_total_space));
221+
}
222+
(StatusCode::OK, metrics)
223+
}
224+
196225
#[tokio::main]
197226
async fn main() {
198227
tracing_subscriber::fmt::init();
@@ -216,6 +245,7 @@ async fn main() {
216245
.route("/upload", post(ax_post_file))
217246
.route("/*filepath", get(ax_get_file))
218247
.route("/v1/list", get(ax_list_files))
248+
.route("/metrics", get(ax_metrics))
219249
.layer(ServiceBuilder::new().layer(DefaultBodyLimit::max(1024 * 1024 * 1024 * 4)))
220250
.with_state(state);
221251

0 commit comments

Comments
 (0)