From 87b02c91c6af6aef3060ff909231e98b8a6a3e96 Mon Sep 17 00:00:00 2001 From: Alexey Kondratov Date: Wed, 31 May 2023 20:44:47 +0200 Subject: [PATCH] [pg_sni_router] Add session_id to more messages Currently, we only have `session_id` in the `handle_client` span, so it's really hard to track the whole connection path in the logs. Especially if error occurs. This commit adds `session_id` explicitly into several other messages. This could be a bit too verbose, but I've tried several combinations of `span.in_scope()` and `instrument` and it didn't play well, because all related messages are in the different contexts. --- proxy/src/bin/pg_sni_router.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/proxy/src/bin/pg_sni_router.rs b/proxy/src/bin/pg_sni_router.rs index bba2d51caf..53b8e16c84 100644 --- a/proxy/src/bin/pg_sni_router.rs +++ b/proxy/src/bin/pg_sni_router.rs @@ -141,15 +141,22 @@ async fn task_main( tokio::select! { accept_result = listener.accept() => { let (socket, peer_addr) = accept_result?; - info!("accepted postgres client connection from {peer_addr}"); - let session_id = uuid::Uuid::new_v4(); + + info!( + session_id = ?session_id, + "accepted postgres client connection from {peer_addr}", + ); + let tls_config = Arc::clone(&tls_config); let dest_suffix = Arc::clone(&dest_suffix); connections.spawn( async move { - info!("spawned a task for {peer_addr}"); + info!( + session_id = ?session_id, + "spawned a task for {peer_addr}", + ); socket .set_nodelay(true) @@ -157,9 +164,12 @@ async fn task_main( handle_client(dest_suffix, tls_config, session_id, socket).await } - .unwrap_or_else(|e| { + .unwrap_or_else(move |e| { // Acknowledge that the task has finished with an error. - error!("per-client task finished with an error: {e:#}"); + error!( + session_id = ?session_id, + "per-client task finished with an error: {e:#}", + ); }), ); } @@ -205,7 +215,7 @@ async fn ssl_handshake( let (raw, read_buf) = stream.into_inner(); // TODO: Normally, client doesn't send any data before - // server says TLS handshake is ok and read_buf is empy. + // server says TLS handshake is ok and read_buf is empty. // However, you could imagine pipelining of postgres // SSLRequest + TLS ClientHello in one hunk similar to // pipelining in our node js driver. We should probably