-
-
Notifications
You must be signed in to change notification settings - Fork 883
/
prometheus_metrics.rs
98 lines (85 loc) · 2.86 KB
/
prometheus_metrics.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use actix_web::{rt::System, web, App, HttpServer};
use lemmy_api_common::context::LemmyContext;
use lemmy_utils::{error::LemmyResult, settings::structs::PrometheusConfig};
use prometheus::{default_registry, Encoder, Gauge, Opts, TextEncoder};
use std::{sync::Arc, thread};
use tracing::error;
struct PromContext {
lemmy: LemmyContext,
db_pool_metrics: DbPoolMetrics,
}
struct DbPoolMetrics {
max_size: Gauge,
size: Gauge,
available: Gauge,
}
pub fn serve_prometheus(config: PrometheusConfig, lemmy_context: LemmyContext) -> LemmyResult<()> {
let context = Arc::new(PromContext {
lemmy: lemmy_context,
db_pool_metrics: create_db_pool_metrics()?,
});
// 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))
})
.bind((config.bind, config.port as u16))
.unwrap_or_else(|e| panic!("Cannot bind to {}:{}: {e}", config.bind, config.port))
.run();
if let Err(err) = server.await {
error!("Prometheus server error: {err}");
}
})
});
Ok(())
}
// handler for the /metrics path
async fn metrics(context: web::Data<Arc<PromContext>>) -> LemmyResult<String> {
// collect metrics
collect_db_pool_metrics(&context);
let mut buffer = Vec::new();
let encoder = TextEncoder::new();
// gather metrics from registry and encode in prometheus format
let metric_families = prometheus::gather();
encoder.encode(&metric_families, &mut buffer)?;
let output = String::from_utf8(buffer)?;
Ok(output)
}
// create lemmy_db_pool_* metrics and register them with the default registry
fn create_db_pool_metrics() -> LemmyResult<DbPoolMetrics> {
let metrics = DbPoolMetrics {
max_size: Gauge::with_opts(Opts::new(
"lemmy_db_pool_max_connections",
"Maximum number of connections in the pool",
))?,
size: Gauge::with_opts(Opts::new(
"lemmy_db_pool_connections",
"Current number of connections in the pool",
))?,
available: Gauge::with_opts(Opts::new(
"lemmy_db_pool_available_connections",
"Number of available connections in the pool",
))?,
};
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()))?;
Ok(metrics)
}
fn collect_db_pool_metrics(context: &PromContext) {
let pool_status = context.lemmy.inner_pool().status();
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);
}