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
This commit is contained in:
zainkabani
2022-11-16 22:15:47 -08:00
committed by GitHub
parent 0c96156dae
commit fe0b012832
6 changed files with 70 additions and 12 deletions

View File

@@ -32,6 +32,12 @@ shutdown_timeout = 5000
# For how long to ban a server if it fails a health check (seconds). # For how long to ban a server if it fails a health check (seconds).
ban_time = 60 # 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. # Reload config automatically if it changes.
autoreload = true autoreload = true

View File

@@ -32,6 +32,12 @@ shutdown_timeout = 60000
# For how long to ban a server if it fails a health check (seconds). # For how long to ban a server if it fails a health check (seconds).
ban_time = 60 # 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. # Reload config automatically if it changes.
autoreload = false autoreload = false

View File

@@ -32,6 +32,12 @@ shutdown_timeout = 60000
# For how long to ban a server if it fails a health check (seconds). # For how long to ban a server if it fails a health check (seconds).
ban_time = 60 # 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. # Reload config automatically if it changes.
autoreload = false autoreload = false

View File

@@ -98,6 +98,8 @@ pub async fn client_entrypoint(
shutdown: Receiver<()>, shutdown: Receiver<()>,
drain: Sender<i32>, drain: Sender<i32>,
admin_only: bool, admin_only: bool,
tls_certificate: Option<String>,
log_client_connections: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
// Figure out if the client wants TLS or not. // Figure out if the client wants TLS or not.
let addr = stream.peer_addr().unwrap(); let addr = stream.peer_addr().unwrap();
@@ -105,10 +107,8 @@ pub async fn client_entrypoint(
match get_startup::<TcpStream>(&mut stream).await { match get_startup::<TcpStream>(&mut stream).await {
// Client requested a TLS connection. // Client requested a TLS connection.
Ok((ClientConnectionType::Tls, _)) => { Ok((ClientConnectionType::Tls, _)) => {
let config = get_config();
// TLS settings are configured, will setup TLS now. // TLS settings are configured, will setup TLS now.
if config.general.tls_certificate != None { if tls_certificate != None {
debug!("Accepting TLS request"); debug!("Accepting TLS request");
let mut yes = BytesMut::new(); let mut yes = BytesMut::new();
@@ -118,7 +118,11 @@ pub async fn client_entrypoint(
// Negotiate TLS. // Negotiate TLS.
match startup_tls(stream, client_server_map, shutdown, admin_only).await { match startup_tls(stream, client_server_map, shutdown, admin_only).await {
Ok(mut client) => { 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() { if !client.is_admin() {
let _ = drain.send(1).await; let _ = drain.send(1).await;
@@ -162,7 +166,11 @@ pub async fn client_entrypoint(
.await .await
{ {
Ok(mut client) => { 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() { if !client.is_admin() {
let _ = drain.send(1).await; let _ = drain.send(1).await;
@@ -203,7 +211,11 @@ pub async fn client_entrypoint(
.await .await
{ {
Ok(mut client) => { 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() { if !client.is_admin() {
let _ = drain.send(1).await; let _ = drain.send(1).await;

View File

@@ -157,6 +157,12 @@ pub struct General {
#[serde(default = "General::default_connect_timeout")] #[serde(default = "General::default_connect_timeout")]
pub connect_timeout: u64, 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")] #[serde(default = "General::default_shutdown_timeout")]
pub shutdown_timeout: u64, pub shutdown_timeout: u64,
@@ -220,6 +226,8 @@ impl Default for General {
healthcheck_timeout: Self::default_healthcheck_timeout(), healthcheck_timeout: Self::default_healthcheck_timeout(),
healthcheck_delay: Self::default_healthcheck_delay(), healthcheck_delay: Self::default_healthcheck_delay(),
ban_time: Self::default_ban_time(), ban_time: Self::default_ban_time(),
log_client_connections: false,
log_client_disconnections: false,
autoreload: false, autoreload: false,
tls_certificate: None, tls_certificate: None,
tls_private_key: None, tls_private_key: None,
@@ -517,6 +525,14 @@ impl Config {
self.general.healthcheck_timeout self.general.healthcheck_timeout
); );
info!("Connection timeout: {}ms", self.general.connect_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!("Shutdown timeout: {}ms", self.general.shutdown_timeout);
info!("Healthcheck delay: {}ms", self.general.healthcheck_delay); info!("Healthcheck delay: {}ms", self.general.healthcheck_delay);
match self.general.tls_certificate.clone() { match self.general.tls_certificate.clone() {

View File

@@ -44,7 +44,7 @@ use jemallocator::Jemalloc;
#[global_allocator] #[global_allocator]
static GLOBAL: Jemalloc = Jemalloc; static GLOBAL: Jemalloc = Jemalloc;
use log::{error, info, warn}; use log::{debug, error, info, warn};
use parking_lot::Mutex; use parking_lot::Mutex;
use pgcat::format_duration; use pgcat::format_duration;
use tokio::net::TcpListener; use tokio::net::TcpListener;
@@ -247,6 +247,8 @@ async fn main() {
let drain_tx = drain_tx.clone(); let drain_tx = drain_tx.clone();
let client_server_map = client_server_map.clone(); let client_server_map = client_server_map.clone();
let tls_certificate = config.general.tls_certificate.clone();
tokio::task::spawn(async move { tokio::task::spawn(async move {
let start = chrono::offset::Utc::now().naive_utc(); let start = chrono::offset::Utc::now().naive_utc();
@@ -256,6 +258,8 @@ async fn main() {
shutdown_rx, shutdown_rx,
drain_tx, drain_tx,
admin_only, admin_only,
tls_certificate.clone(),
config.general.log_client_connections,
) )
.await .await
{ {
@@ -263,11 +267,19 @@ async fn main() {
let duration = chrono::offset::Utc::now().naive_utc() - start; let duration = chrono::offset::Utc::now().naive_utc() - start;
info!( if config.general.log_client_disconnections {
"Client {:?} disconnected, session duration: {}", info!(
addr, "Client {:?} disconnected, session duration: {}",
format_duration(&duration) addr,
); format_duration(&duration)
);
} else {
debug!(
"Client {:?} disconnected, session duration: {}",
addr,
format_duration(&duration)
);
}
} }
Err(err) => { Err(err) => {