peshming/src/metrics.rs
2022-03-17 15:05:18 +01:00

81 lines
3.4 KiB
Rust

/********************************************************************************
* Prometheus exporter for monitoring network connectivity using icmp pings *
* *
* Copyright (C) 2019-2020 Jan Christian Grünhage *
* Copyright (C) 2020 Famedly GmbH *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Affero General Public License as *
* published by the Free Software Foundation, either version 3 of the *
* License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Affero General Public License for more details. *
* *
* You should have received a copy of the GNU Affero General Public License *
* along with this program. If not, see <https://www.gnu.org/licenses/>. *
********************************************************************************/
use crate::config::Config;
use anyhow::Result;
use axum::body::Body;
use axum::http::{Response, StatusCode};
use axum::{http::header::CONTENT_TYPE, response::IntoResponse, routing::get, Router, Server};
use lazy_static::lazy_static;
use log::info;
use prometheus::*;
use prometheus::{Counter, Gauge, HistogramVec, TextEncoder};
lazy_static! {
static ref HTTP_COUNTER: Counter = register_counter!(opts!(
"http_requests_total",
"Total number of HTTP requests made.",
labels! {"handler" => "all",}
))
.unwrap();
static ref HTTP_BODY_GAUGE: Gauge = register_gauge!(opts!(
"http_response_size_bytes",
"The HTTP response sizes in bytes.",
labels! {"handler" => "all",}
))
.unwrap();
static ref HTTP_REQ_HISTOGRAM: HistogramVec = register_histogram_vec!(
"http_request_duration_seconds",
"The HTTP request latencies in seconds.",
&["handler"]
)
.unwrap();
}
async fn serve_metrics() -> impl IntoResponse {
let encoder = TextEncoder::new();
HTTP_COUNTER.inc();
let timer = HTTP_REQ_HISTOGRAM.with_label_values(&["all"]).start_timer();
let metric_families = prometheus::gather();
let mut buffer = vec![];
encoder.encode(&metric_families, &mut buffer).unwrap();
HTTP_BODY_GAUGE.set(buffer.len() as f64);
let response = Response::builder()
.status(StatusCode::OK)
.header(CONTENT_TYPE, encoder.format_type())
.body(Body::from(buffer))
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
timer.observe_duration();
response
}
pub(crate) async fn start_serving_metrics(config: &Config) -> Result<()> {
let app = Router::new()
.route("/metrics", get(serve_metrics))
.route("/health", get(|| async { "" }));
let serve_future = Server::bind(&config.listener).serve(app.into_make_service());
info!("Listening on {}", &config.listener);
Ok(serve_future.await?)
}