@@ -5,6 +5,7 @@ use futures_util::{SinkExt, StreamExt};
55use rivet_envoy_protocol as protocol;
66use tokio:: sync:: mpsc;
77use tokio_tungstenite:: tungstenite;
8+ use tracing:: Instrument ;
89use vbare:: OwnedVersionedData ;
910
1011use crate :: context:: { SharedContext , WsTxMessage } ;
@@ -14,7 +15,8 @@ use crate::utils::{BackoffOptions, calculate_backoff, parse_ws_close_reason};
1415const STABLE_CONNECTION_MS : u64 = 60_000 ;
1516
1617pub fn start_connection ( shared : Arc < SharedContext > ) {
17- tokio:: spawn ( connection_loop ( shared) ) ;
18+ let span = tracing:: debug_span!( "envoy_connection" , envoy_key = %shared. envoy_key) ;
19+ tokio:: spawn ( connection_loop ( shared) . instrument ( span) ) ;
1820}
1921
2022async fn connection_loop ( shared : Arc < SharedContext > ) {
@@ -118,31 +120,38 @@ async fn single_connection(
118120
119121 // Spawn write task
120122 let shared2 = shared. clone ( ) ;
121- let write_handle = tokio:: spawn ( async move {
122- super :: send_initial_metadata ( & shared2) . await ;
123-
124- while let Some ( msg) = ws_rx. recv ( ) . await {
125- match msg {
126- WsTxMessage :: Send ( data) => {
127- if let Err ( e) = write. send ( tungstenite:: Message :: Binary ( data. into ( ) ) ) . await {
128- tracing:: error!( ?e, "failed to send ws message" ) ;
123+ let write_span = tracing:: debug_span!( "envoy_ws_write" , envoy_key = %shared2. envoy_key) ;
124+ let write_handle = tokio:: spawn (
125+ async move {
126+ super :: send_initial_metadata ( & shared2) . await ;
127+
128+ while let Some ( msg) = ws_rx. recv ( ) . await {
129+ match msg {
130+ WsTxMessage :: Send ( data) => {
131+ let result = write
132+ . send ( tungstenite:: Message :: Binary ( data. into ( ) ) )
133+ . await ;
134+ if let Err ( e) = result {
135+ tracing:: error!( ?e, "failed to send ws message" ) ;
136+ break ;
137+ }
138+ }
139+ WsTxMessage :: Close => {
140+ let _ = write
141+ . send ( tungstenite:: Message :: Close ( Some (
142+ tungstenite:: protocol:: CloseFrame {
143+ code : tungstenite:: protocol:: frame:: coding:: CloseCode :: Normal ,
144+ reason : "envoy.shutdown" . into ( ) ,
145+ } ,
146+ ) ) )
147+ . await ;
129148 break ;
130149 }
131150 }
132- WsTxMessage :: Close => {
133- let _ = write
134- . send ( tungstenite:: Message :: Close ( Some (
135- tungstenite:: protocol:: CloseFrame {
136- code : tungstenite:: protocol:: frame:: coding:: CloseCode :: Normal ,
137- reason : "envoy.shutdown" . into ( ) ,
138- } ,
139- ) ) )
140- . await ;
141- break ;
142- }
143151 }
144152 }
145- } ) ;
153+ . instrument ( write_span) ,
154+ ) ;
146155
147156 let mut result = None ;
148157
0 commit comments