| 1 |
use axum::{extract::State, response::IntoResponse}; |
| 2 |
use metrics::{counter, gauge, histogram}; |
| 3 |
use metrics_exporter_prometheus::{PrometheusBuilder, PrometheusHandle}; |
| 4 |
|
| 5 |
pub fn init() -> PrometheusHandle { |
| 6 |
PrometheusBuilder::new() |
| 7 |
.install_recorder() |
| 8 |
.expect("install prometheus recorder") |
| 9 |
} |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
pub fn build_started() { |
| 18 |
counter!("bento_builds_total").increment(1); |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
pub fn target_finished(target: &str, status: &str, secs: f64) { |
| 24 |
counter!("bento_target_runs_total", "target" => target.to_owned(), "status" => status.to_owned()) |
| 25 |
.increment(1); |
| 26 |
histogram!("bento_target_run_seconds", "target" => target.to_owned()).record(secs); |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
pub fn set_in_flight(n: usize) { |
| 31 |
gauge!("bento_targets_in_flight").set(n as f64); |
| 32 |
} |
| 33 |
|
| 34 |
pub async fn render(State(handle): State<PrometheusHandle>) -> impl IntoResponse { |
| 35 |
handle.render() |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
#[cfg(test)] |
| 43 |
pub fn test_handle() -> PrometheusHandle { |
| 44 |
PrometheusBuilder::new().build_recorder().handle() |
| 45 |
} |
| 46 |
|