2023-10-25 10:54:58 +00:00
|
|
|
use actix_web::{rt::System, web, App, HttpServer};
|
2023-07-05 11:25:19 +00:00
|
|
|
use lemmy_api_common::context::LemmyContext;
|
2023-10-25 10:54:58 +00:00
|
|
|
use lemmy_utils::{error::LemmyResult, settings::structs::PrometheusConfig};
|
2023-07-05 11:25:19 +00:00
|
|
|
use prometheus::{default_registry, Encoder, Gauge, Opts, TextEncoder};
|
2023-10-25 10:54:58 +00:00
|
|
|
use std::{sync::Arc, thread};
|
|
|
|
use tracing::error;
|
2023-07-05 11:25:19 +00:00
|
|
|
|
|
|
|
struct PromContext {
|
|
|
|
lemmy: LemmyContext,
|
|
|
|
db_pool_metrics: DbPoolMetrics,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct DbPoolMetrics {
|
|
|
|
max_size: Gauge,
|
|
|
|
size: Gauge,
|
|
|
|
available: Gauge,
|
|
|
|
}
|
|
|
|
|
2023-10-25 10:54:58 +00:00
|
|
|
pub fn serve_prometheus(config: PrometheusConfig, lemmy_context: LemmyContext) -> LemmyResult<()> {
|
2023-07-05 11:25:19 +00:00
|
|
|
let context = Arc::new(PromContext {
|
|
|
|
lemmy: lemmy_context,
|
2023-10-25 10:54:58 +00:00
|
|
|
db_pool_metrics: create_db_pool_metrics()?,
|
2023-07-05 11:25:19 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// spawn thread that blocks on handling requests
|
|
|
|
// only mapping /metrics to a handler
|
|
|
|
thread::spawn(move || {
|
|
|
|
let sys = System::new();
|
|
|
|
sys.block_on(async {
|
|
|
|
let server = HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.app_data(web::Data::new(Arc::clone(&context)))
|
|
|
|
.route("/metrics", web::get().to(metrics))
|
|
|
|
})
|
2023-10-25 10:54:58 +00:00
|
|
|
.bind((config.bind, config.port as u16))
|
|
|
|
.unwrap_or_else(|e| panic!("Cannot bind to {}:{}: {e}", config.bind, config.port))
|
2023-07-05 11:25:19 +00:00
|
|
|
.run();
|
|
|
|
|
|
|
|
if let Err(err) = server.await {
|
2023-10-25 10:54:58 +00:00
|
|
|
error!("Prometheus server error: {err}");
|
2023-07-05 11:25:19 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
2023-10-25 10:54:58 +00:00
|
|
|
Ok(())
|
2023-07-05 11:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handler for the /metrics path
|
2023-10-25 10:54:58 +00:00
|
|
|
async fn metrics(context: web::Data<Arc<PromContext>>) -> LemmyResult<String> {
|
2023-07-05 11:25:19 +00:00
|
|
|
// collect metrics
|
|
|
|
collect_db_pool_metrics(&context).await;
|
|
|
|
|
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let encoder = TextEncoder::new();
|
|
|
|
|
|
|
|
// gather metrics from registry and encode in prometheus format
|
|
|
|
let metric_families = prometheus::gather();
|
2023-10-25 10:54:58 +00:00
|
|
|
encoder.encode(&metric_families, &mut buffer)?;
|
|
|
|
let output = String::from_utf8(buffer)?;
|
2023-07-05 11:25:19 +00:00
|
|
|
|
2023-10-25 10:54:58 +00:00
|
|
|
Ok(output)
|
2023-07-05 11:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// create lemmy_db_pool_* metrics and register them with the default registry
|
2023-10-25 10:54:58 +00:00
|
|
|
fn create_db_pool_metrics() -> LemmyResult<DbPoolMetrics> {
|
2023-07-05 11:25:19 +00:00
|
|
|
let metrics = DbPoolMetrics {
|
|
|
|
max_size: Gauge::with_opts(Opts::new(
|
|
|
|
"lemmy_db_pool_max_connections",
|
|
|
|
"Maximum number of connections in the pool",
|
2023-10-25 10:54:58 +00:00
|
|
|
))?,
|
2023-07-05 11:25:19 +00:00
|
|
|
size: Gauge::with_opts(Opts::new(
|
|
|
|
"lemmy_db_pool_connections",
|
|
|
|
"Current number of connections in the pool",
|
2023-10-25 10:54:58 +00:00
|
|
|
))?,
|
2023-07-05 11:25:19 +00:00
|
|
|
available: Gauge::with_opts(Opts::new(
|
|
|
|
"lemmy_db_pool_available_connections",
|
|
|
|
"Number of available connections in the pool",
|
2023-10-25 10:54:58 +00:00
|
|
|
))?,
|
2023-07-05 11:25:19 +00:00
|
|
|
};
|
|
|
|
|
2023-10-25 10:54:58 +00:00
|
|
|
default_registry().register(Box::new(metrics.max_size.clone()))?;
|
|
|
|
default_registry().register(Box::new(metrics.size.clone()))?;
|
|
|
|
default_registry().register(Box::new(metrics.available.clone()))?;
|
2023-07-05 11:25:19 +00:00
|
|
|
|
2023-10-25 10:54:58 +00:00
|
|
|
Ok(metrics)
|
2023-07-05 11:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn collect_db_pool_metrics(context: &PromContext) {
|
2023-07-11 13:09:59 +00:00
|
|
|
let pool_status = context.lemmy.inner_pool().status();
|
2023-07-05 11:25:19 +00:00
|
|
|
context
|
|
|
|
.db_pool_metrics
|
|
|
|
.max_size
|
|
|
|
.set(pool_status.max_size as f64);
|
|
|
|
context.db_pool_metrics.size.set(pool_status.size as f64);
|
|
|
|
context
|
|
|
|
.db_pool_metrics
|
|
|
|
.available
|
|
|
|
.set(pool_status.available as f64);
|
|
|
|
}
|