From fe0b012832032cc93117d2cff0f953ea9fc8b892 Mon Sep 17 00:00:00 2001 From: zainkabani <77307340+zainkabani@users.noreply.github.com> Date: Wed, 16 Nov 2022 22:15:47 -0800 Subject: [PATCH] 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 --- .circleci/pgcat.toml | 6 ++++++ examples/docker/pgcat.toml | 6 ++++++ pgcat.toml | 6 ++++++ src/client.rs | 24 ++++++++++++++++++------ src/config.rs | 16 ++++++++++++++++ src/main.rs | 24 ++++++++++++++++++------ 6 files changed, 70 insertions(+), 12 deletions(-) diff --git a/.circleci/pgcat.toml b/.circleci/pgcat.toml index 56aa1dd..2a959d5 100644 --- a/.circleci/pgcat.toml +++ b/.circleci/pgcat.toml @@ -32,6 +32,12 @@ shutdown_timeout = 5000 # For how long to ban a server if it fails a health check (seconds). ban_time = 60 # Seconds +# If we should log client connections +log_client_connections = false + +# If we should log client disconnections +log_client_disconnections = false + # Reload config automatically if it changes. autoreload = true diff --git a/examples/docker/pgcat.toml b/examples/docker/pgcat.toml index 97f1c9c..c41c8cd 100644 --- a/examples/docker/pgcat.toml +++ b/examples/docker/pgcat.toml @@ -32,6 +32,12 @@ shutdown_timeout = 60000 # For how long to ban a server if it fails a health check (seconds). ban_time = 60 # seconds +# If we should log client connections +log_client_connections = false + +# If we should log client disconnections +log_client_disconnections = false + # Reload config automatically if it changes. autoreload = false diff --git a/pgcat.toml b/pgcat.toml index 8d58804..69eec39 100644 --- a/pgcat.toml +++ b/pgcat.toml @@ -32,6 +32,12 @@ shutdown_timeout = 60000 # For how long to ban a server if it fails a health check (seconds). ban_time = 60 # seconds +# If we should log client connections +log_client_connections = false + +# If we should log client disconnections +log_client_disconnections = false + # Reload config automatically if it changes. autoreload = false diff --git a/src/client.rs b/src/client.rs index ec7ae85..56eac27 100644 --- a/src/client.rs +++ b/src/client.rs @@ -98,6 +98,8 @@ pub async fn client_entrypoint( shutdown: Receiver<()>, drain: Sender, admin_only: bool, + tls_certificate: Option, + log_client_connections: bool, ) -> Result<(), Error> { // Figure out if the client wants TLS or not. let addr = stream.peer_addr().unwrap(); @@ -105,10 +107,8 @@ pub async fn client_entrypoint( match get_startup::(&mut stream).await { // Client requested a TLS connection. Ok((ClientConnectionType::Tls, _)) => { - let config = get_config(); - // TLS settings are configured, will setup TLS now. - if config.general.tls_certificate != None { + if tls_certificate != None { debug!("Accepting TLS request"); let mut yes = BytesMut::new(); @@ -118,7 +118,11 @@ pub async fn client_entrypoint( // Negotiate TLS. match startup_tls(stream, client_server_map, shutdown, admin_only).await { Ok(mut client) => { - info!("Client {:?} connected (TLS)", addr); + if log_client_connections { + info!("Client {:?} connected (TLS)", addr); + } else { + debug!("Client {:?} connected (TLS)", addr); + } if !client.is_admin() { let _ = drain.send(1).await; @@ -162,7 +166,11 @@ pub async fn client_entrypoint( .await { Ok(mut client) => { - info!("Client {:?} connected (plain)", addr); + if log_client_connections { + info!("Client {:?} connected (plain)", addr); + } else { + debug!("Client {:?} connected (plain)", addr); + } if !client.is_admin() { let _ = drain.send(1).await; @@ -203,7 +211,11 @@ pub async fn client_entrypoint( .await { Ok(mut client) => { - info!("Client {:?} connected (plain)", addr); + if log_client_connections { + info!("Client {:?} connected (plain)", addr); + } else { + debug!("Client {:?} connected (plain)", addr); + } if !client.is_admin() { let _ = drain.send(1).await; diff --git a/src/config.rs b/src/config.rs index 8e1e14e..39bff1b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -157,6 +157,12 @@ pub struct General { #[serde(default = "General::default_connect_timeout")] pub connect_timeout: u64, + #[serde(default)] // False + pub log_client_connections: bool, + + #[serde(default)] // False + pub log_client_disconnections: bool, + #[serde(default = "General::default_shutdown_timeout")] pub shutdown_timeout: u64, @@ -220,6 +226,8 @@ impl Default for General { healthcheck_timeout: Self::default_healthcheck_timeout(), healthcheck_delay: Self::default_healthcheck_delay(), ban_time: Self::default_ban_time(), + log_client_connections: false, + log_client_disconnections: false, autoreload: false, tls_certificate: None, tls_private_key: None, @@ -517,6 +525,14 @@ impl Config { self.general.healthcheck_timeout ); info!("Connection timeout: {}ms", self.general.connect_timeout); + info!( + "Log client connections: {}", + self.general.log_client_connections + ); + info!( + "Log client disconnections: {}", + self.general.log_client_disconnections + ); info!("Shutdown timeout: {}ms", self.general.shutdown_timeout); info!("Healthcheck delay: {}ms", self.general.healthcheck_delay); match self.general.tls_certificate.clone() { diff --git a/src/main.rs b/src/main.rs index 2572d9f..a05ce90 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,7 +44,7 @@ use jemallocator::Jemalloc; #[global_allocator] static GLOBAL: Jemalloc = Jemalloc; -use log::{error, info, warn}; +use log::{debug, error, info, warn}; use parking_lot::Mutex; use pgcat::format_duration; use tokio::net::TcpListener; @@ -247,6 +247,8 @@ async fn main() { let drain_tx = drain_tx.clone(); let client_server_map = client_server_map.clone(); + let tls_certificate = config.general.tls_certificate.clone(); + tokio::task::spawn(async move { let start = chrono::offset::Utc::now().naive_utc(); @@ -256,6 +258,8 @@ async fn main() { shutdown_rx, drain_tx, admin_only, + tls_certificate.clone(), + config.general.log_client_connections, ) .await { @@ -263,11 +267,19 @@ async fn main() { let duration = chrono::offset::Utc::now().naive_utc() - start; - info!( - "Client {:?} disconnected, session duration: {}", - addr, - format_duration(&duration) - ); + if config.general.log_client_disconnections { + info!( + "Client {:?} disconnected, session duration: {}", + addr, + format_duration(&duration) + ); + } else { + debug!( + "Client {:?} disconnected, session duration: {}", + addr, + format_duration(&duration) + ); + } } Err(err) => {