1 use actix_web::{rt::System, web, App, HttpResponse, HttpServer, Responder};
2 use lemmy_api_common::context::LemmyContext;
3 use lemmy_utils::settings::structs::PrometheusConfig;
4 use prometheus::{default_registry, Encoder, Gauge, Opts, TextEncoder};
6 net::{IpAddr, Ipv4Addr},
13 db_pool_metrics: DbPoolMetrics,
16 struct DbPoolMetrics {
22 static DEFAULT_BIND: IpAddr = IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1));
23 static DEFAULT_PORT: i32 = 10002;
25 pub fn serve_prometheus(config: Option<&PrometheusConfig>, lemmy_context: LemmyContext) {
26 let context = Arc::new(PromContext {
28 db_pool_metrics: create_db_pool_metrics(),
31 let (bind, port) = match config {
33 config.bind.unwrap_or(DEFAULT_BIND),
34 config.port.unwrap_or(DEFAULT_PORT),
36 None => (DEFAULT_BIND, DEFAULT_PORT),
39 // spawn thread that blocks on handling requests
40 // only mapping /metrics to a handler
41 thread::spawn(move || {
42 let sys = System::new();
44 let server = HttpServer::new(move || {
46 .app_data(web::Data::new(Arc::clone(&context)))
47 .route("/metrics", web::get().to(metrics))
49 .bind((bind, port as u16))
50 .unwrap_or_else(|_| panic!("Cannot bind to {}:{}", bind, port))
53 if let Err(err) = server.await {
54 eprintln!("Prometheus server error: {}", err);
60 // handler for the /metrics path
61 async fn metrics(context: web::Data<Arc<PromContext>>) -> impl Responder {
63 collect_db_pool_metrics(&context).await;
65 let mut buffer = Vec::new();
66 let encoder = TextEncoder::new();
68 // gather metrics from registry and encode in prometheus format
69 let metric_families = prometheus::gather();
70 encoder.encode(&metric_families, &mut buffer).unwrap();
71 let output = String::from_utf8(buffer).unwrap();
73 HttpResponse::Ok().body(output)
76 // create lemmy_db_pool_* metrics and register them with the default registry
77 fn create_db_pool_metrics() -> DbPoolMetrics {
78 let metrics = DbPoolMetrics {
79 max_size: Gauge::with_opts(Opts::new(
80 "lemmy_db_pool_max_connections",
81 "Maximum number of connections in the pool",
84 size: Gauge::with_opts(Opts::new(
85 "lemmy_db_pool_connections",
86 "Current number of connections in the pool",
89 available: Gauge::with_opts(Opts::new(
90 "lemmy_db_pool_available_connections",
91 "Number of available connections in the pool",
97 .register(Box::new(metrics.max_size.clone()))
100 .register(Box::new(metrics.size.clone()))
103 .register(Box::new(metrics.available.clone()))
109 async fn collect_db_pool_metrics(context: &PromContext) {
110 let pool_status = context.lemmy.pool().status();
114 .set(pool_status.max_size as f64);
115 context.db_pool_metrics.size.set(pool_status.size as f64);
119 .set(pool_status.available as f64);