Skip to content

Commit 63fafd1

Browse files
committed
store: Revamp how we log connection checkout wait times
Instead of logging every time we have to wait more than 100ms for a connection (which we log _a lot_) log the moving average of wait times, but at most every 10s
1 parent c76f20c commit 63fafd1

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

store/postgres/src/connection_pool.rs

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ use graph::util::security::SafeDisplay;
77
use std::collections::HashMap;
88
use std::fmt;
99
use std::sync::{Arc, RwLock};
10-
use std::time::Duration;
11-
12-
// Log connection checkouts that take longer than this many millis
13-
const CONTENTION_LOG_THRESHOLD: u64 = 100;
10+
use std::time::{Duration, Instant};
1411

1512
struct ErrorHandler(Logger, Box<Counter>);
1613

@@ -31,6 +28,7 @@ struct EventHandler {
3128
logger: Logger,
3229
gauge: Box<Gauge>,
3330
wait_stats: PoolWaitStats,
31+
last_log: RwLock<Instant>,
3432
}
3533

3634
impl EventHandler {
@@ -46,11 +44,34 @@ impl EventHandler {
4644
logger,
4745
gauge,
4846
wait_stats,
47+
last_log: RwLock::new(Instant::now()),
4948
}
5049
}
5150

5251
fn add_wait_time(&self, duration: Duration) {
53-
self.wait_stats.write().unwrap().add(duration);
52+
let should_log = {
53+
// Log average wait time, but at most every 10s
54+
let mut last_log = self.last_log.write().unwrap();
55+
if last_log.elapsed() > Duration::from_secs(10) {
56+
*last_log = Instant::now();
57+
true
58+
} else {
59+
false
60+
}
61+
};
62+
let wait_avg = {
63+
let mut wait_stats = self.wait_stats.write().unwrap();
64+
wait_stats.add(duration);
65+
if should_log {
66+
wait_stats.average()
67+
} else {
68+
None
69+
}
70+
};
71+
if let Some(wait_avg) = wait_avg {
72+
info!(self.logger, "Average connection wait time";
73+
"wait_ms" => wait_avg.as_millis());
74+
}
5475
}
5576
}
5677

@@ -66,10 +87,6 @@ impl HandleEvent for EventHandler {
6687
fn handle_checkout(&self, event: e::CheckoutEvent) {
6788
self.gauge.inc();
6889
self.add_wait_time(event.duration());
69-
if event.duration() > Duration::from_millis(CONTENTION_LOG_THRESHOLD) {
70-
warn!(self.logger, "Excessive wait time on checkout";
71-
"wait_ms" => event.duration().as_millis())
72-
}
7390
}
7491
fn handle_timeout(&self, event: e::TimeoutEvent) {
7592
self.add_wait_time(event.timeout());

0 commit comments

Comments
 (0)