Skip to content

Commit fe0b012

Browse files
authored
Adds configuration for logging connections and removes get_config from entrypoint (#236)
* Adds configuration for logging connections and removes get_config from entrypoint * typo * rename connection config var and add to toml files * update config log * fmt
1 parent 0c96156 commit fe0b012

File tree

6 files changed

+70
-12
lines changed

6 files changed

+70
-12
lines changed

.circleci/pgcat.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ shutdown_timeout = 5000
3232
# For how long to ban a server if it fails a health check (seconds).
3333
ban_time = 60 # Seconds
3434

35+
# If we should log client connections
36+
log_client_connections = false
37+
38+
# If we should log client disconnections
39+
log_client_disconnections = false
40+
3541
# Reload config automatically if it changes.
3642
autoreload = true
3743

examples/docker/pgcat.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ shutdown_timeout = 60000
3232
# For how long to ban a server if it fails a health check (seconds).
3333
ban_time = 60 # seconds
3434

35+
# If we should log client connections
36+
log_client_connections = false
37+
38+
# If we should log client disconnections
39+
log_client_disconnections = false
40+
3541
# Reload config automatically if it changes.
3642
autoreload = false
3743

pgcat.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ shutdown_timeout = 60000
3232
# For how long to ban a server if it fails a health check (seconds).
3333
ban_time = 60 # seconds
3434

35+
# If we should log client connections
36+
log_client_connections = false
37+
38+
# If we should log client disconnections
39+
log_client_disconnections = false
40+
3541
# Reload config automatically if it changes.
3642
autoreload = false
3743

src/client.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -98,17 +98,17 @@ pub async fn client_entrypoint(
9898
shutdown: Receiver<()>,
9999
drain: Sender<i32>,
100100
admin_only: bool,
101+
tls_certificate: Option<String>,
102+
log_client_connections: bool,
101103
) -> Result<(), Error> {
102104
// Figure out if the client wants TLS or not.
103105
let addr = stream.peer_addr().unwrap();
104106

105107
match get_startup::<TcpStream>(&mut stream).await {
106108
// Client requested a TLS connection.
107109
Ok((ClientConnectionType::Tls, _)) => {
108-
let config = get_config();
109-
110110
// TLS settings are configured, will setup TLS now.
111-
if config.general.tls_certificate != None {
111+
if tls_certificate != None {
112112
debug!("Accepting TLS request");
113113

114114
let mut yes = BytesMut::new();
@@ -118,7 +118,11 @@ pub async fn client_entrypoint(
118118
// Negotiate TLS.
119119
match startup_tls(stream, client_server_map, shutdown, admin_only).await {
120120
Ok(mut client) => {
121-
info!("Client {:?} connected (TLS)", addr);
121+
if log_client_connections {
122+
info!("Client {:?} connected (TLS)", addr);
123+
} else {
124+
debug!("Client {:?} connected (TLS)", addr);
125+
}
122126

123127
if !client.is_admin() {
124128
let _ = drain.send(1).await;
@@ -162,7 +166,11 @@ pub async fn client_entrypoint(
162166
.await
163167
{
164168
Ok(mut client) => {
165-
info!("Client {:?} connected (plain)", addr);
169+
if log_client_connections {
170+
info!("Client {:?} connected (plain)", addr);
171+
} else {
172+
debug!("Client {:?} connected (plain)", addr);
173+
}
166174

167175
if !client.is_admin() {
168176
let _ = drain.send(1).await;
@@ -203,7 +211,11 @@ pub async fn client_entrypoint(
203211
.await
204212
{
205213
Ok(mut client) => {
206-
info!("Client {:?} connected (plain)", addr);
214+
if log_client_connections {
215+
info!("Client {:?} connected (plain)", addr);
216+
} else {
217+
debug!("Client {:?} connected (plain)", addr);
218+
}
207219

208220
if !client.is_admin() {
209221
let _ = drain.send(1).await;

src/config.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ pub struct General {
157157
#[serde(default = "General::default_connect_timeout")]
158158
pub connect_timeout: u64,
159159

160+
#[serde(default)] // False
161+
pub log_client_connections: bool,
162+
163+
#[serde(default)] // False
164+
pub log_client_disconnections: bool,
165+
160166
#[serde(default = "General::default_shutdown_timeout")]
161167
pub shutdown_timeout: u64,
162168

@@ -220,6 +226,8 @@ impl Default for General {
220226
healthcheck_timeout: Self::default_healthcheck_timeout(),
221227
healthcheck_delay: Self::default_healthcheck_delay(),
222228
ban_time: Self::default_ban_time(),
229+
log_client_connections: false,
230+
log_client_disconnections: false,
223231
autoreload: false,
224232
tls_certificate: None,
225233
tls_private_key: None,
@@ -517,6 +525,14 @@ impl Config {
517525
self.general.healthcheck_timeout
518526
);
519527
info!("Connection timeout: {}ms", self.general.connect_timeout);
528+
info!(
529+
"Log client connections: {}",
530+
self.general.log_client_connections
531+
);
532+
info!(
533+
"Log client disconnections: {}",
534+
self.general.log_client_disconnections
535+
);
520536
info!("Shutdown timeout: {}ms", self.general.shutdown_timeout);
521537
info!("Healthcheck delay: {}ms", self.general.healthcheck_delay);
522538
match self.general.tls_certificate.clone() {

src/main.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use jemallocator::Jemalloc;
4444
#[global_allocator]
4545
static GLOBAL: Jemalloc = Jemalloc;
4646

47-
use log::{error, info, warn};
47+
use log::{debug, error, info, warn};
4848
use parking_lot::Mutex;
4949
use pgcat::format_duration;
5050
use tokio::net::TcpListener;
@@ -247,6 +247,8 @@ async fn main() {
247247
let drain_tx = drain_tx.clone();
248248
let client_server_map = client_server_map.clone();
249249

250+
let tls_certificate = config.general.tls_certificate.clone();
251+
250252
tokio::task::spawn(async move {
251253
let start = chrono::offset::Utc::now().naive_utc();
252254

@@ -256,18 +258,28 @@ async fn main() {
256258
shutdown_rx,
257259
drain_tx,
258260
admin_only,
261+
tls_certificate.clone(),
262+
config.general.log_client_connections,
259263
)
260264
.await
261265
{
262266
Ok(()) => {
263267

264268
let duration = chrono::offset::Utc::now().naive_utc() - start;
265269

266-
info!(
267-
"Client {:?} disconnected, session duration: {}",
268-
addr,
269-
format_duration(&duration)
270-
);
270+
if config.general.log_client_disconnections {
271+
info!(
272+
"Client {:?} disconnected, session duration: {}",
273+
addr,
274+
format_duration(&duration)
275+
);
276+
} else {
277+
debug!(
278+
"Client {:?} disconnected, session duration: {}",
279+
addr,
280+
format_duration(&duration)
281+
);
282+
}
271283
}
272284

273285
Err(err) => {

0 commit comments

Comments
 (0)