Skip to content

Commit b3c53e8

Browse files
committed
track the number of used db connections with an histogram
The histogram should allow us to determine more accurately how many connections are used, as the older approach of taking a sample whenever metrics are scraped does not work well.
1 parent 622826c commit b3c53e8

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

src/app.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@ impl App {
128128
instance_metrics
129129
.database_time_to_obtain_connection
130130
.with_label_values(&["primary"]),
131+
instance_metrics
132+
.database_used_conns_histogram
133+
.with_label_values(&["primary"]),
131134
)
132135
.unwrap()
133136
};
@@ -155,6 +158,9 @@ impl App {
155158
instance_metrics
156159
.database_time_to_obtain_connection
157160
.with_label_values(&["follower"]),
161+
instance_metrics
162+
.database_used_conns_histogram
163+
.with_label_values(&["follower"]),
158164
)
159165
.unwrap(),
160166
)

src/db.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::middleware::app::RequestApp;
1313
pub enum DieselPool {
1414
Pool {
1515
pool: r2d2::Pool<ConnectionManager<PgConnection>>,
16+
used_conns_metric: Histogram,
1617
time_to_obtain_connection_metric: Histogram,
1718
},
1819
Test(Arc<ReentrantMutex<PgConnection>>),
@@ -22,6 +23,7 @@ impl DieselPool {
2223
pub(crate) fn new(
2324
url: &str,
2425
config: r2d2::Builder<ConnectionManager<PgConnection>>,
26+
used_conns_metric: Histogram,
2527
time_to_obtain_connection_metric: Histogram,
2628
) -> Result<DieselPool, PoolError> {
2729
let manager = ConnectionManager::new(connection_url(url));
@@ -39,6 +41,7 @@ impl DieselPool {
3941
// automatically be marked as unhealthy and the rest of the application will adapt.
4042
let pool = DieselPool::Pool {
4143
pool: config.build_unchecked(manager),
44+
used_conns_metric,
4245
time_to_obtain_connection_metric,
4346
};
4447
match pool.wait_until_healthy(Duration::from_secs(5)) {
@@ -62,8 +65,13 @@ impl DieselPool {
6265
match self {
6366
DieselPool::Pool {
6467
pool,
68+
used_conns_metric,
6569
time_to_obtain_connection_metric,
6670
} => time_to_obtain_connection_metric.observe_closure_duration(|| {
71+
// Record the number of used connections before obtaining the current one.
72+
let state = pool.state();
73+
used_conns_metric.observe((state.connections - state.idle_connections) as f64);
74+
6775
if let Some(conn) = pool.try_get() {
6876
Ok(DieselPooledConn::Pool(conn))
6977
} else if !self.is_healthy() {

src/metrics/histogram.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ impl HistogramBuckets for TimingBuckets {
2424
];
2525
}
2626

27+
/// Buckets geared towards measuring how many database connections are used in the database pool.
28+
pub struct DatabasePoolBuckets;
29+
30+
impl HistogramBuckets for DatabasePoolBuckets {
31+
const BUCKETS: &'static [f64] = &[0.0, 1.0, 2.0, 5.0, 10.0, 15.0, 20.0, 30.0, 50.0, 100.0];
32+
}
33+
2734
/// Wrapper type over [`prometheus::Histogram`] to support defining buckets.
2835
pub struct Histogram<B: HistogramBuckets> {
2936
inner: prometheus::Histogram,

src/metrics/instance.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
2020
use crate::util::errors::AppResult;
2121
use crate::{app::App, db::DieselPool};
22-
use crate::metrics::histogram::{Histogram, HistogramVec, TimingBuckets};
22+
use crate::metrics::histogram::{Histogram, HistogramVec, TimingBuckets, DatabasePoolBuckets};
2323
use prometheus::{
2424
proto::MetricFamily, IntCounter, IntCounterVec, IntGauge, IntGaugeVec,
2525
};
@@ -32,6 +32,8 @@ metrics! {
3232
database_used_conns: IntGaugeVec["pool"],
3333
/// Amount of time required to obtain a database connection
3434
pub database_time_to_obtain_connection: HistogramVec<TimingBuckets>["pool"],
35+
/// Number of used database connections in the pool, as histogram
36+
pub database_used_conns_histogram: HistogramVec<DatabasePoolBuckets>["pool"],
3537

3638
/// Number of requests processed by this instance
3739
pub requests_total: IntCounter,

0 commit comments

Comments
 (0)