@@ -38,6 +38,7 @@ use crate::{
3838 aggregated_discovery_service_client:: AggregatedDiscoveryServiceClient ,
3939 } ,
4040 generated:: quilkin:: relay:: v1alpha1:: aggregated_control_plane_discovery_service_client:: AggregatedControlPlaneDiscoveryServiceClient ,
41+ metrics,
4142} ;
4243
4344type AdsGrpcClient = AggregatedDiscoveryServiceClient < TonicChannel > ;
@@ -110,7 +111,7 @@ impl ServiceClient for MdsGrpcClient {
110111 & mut self ,
111112 stream : S ,
112113 ) -> tonic:: Result < tonic:: Response < tonic:: Streaming < Self :: Response > > > {
113- self . stream_aggregated_resources ( stream) . await
114+ self . stream_aggregated_resources ( stream) . inspect_err ( |error| metrics :: errors_total ( & error ) ) . await
114115 }
115116}
116117
@@ -157,6 +158,8 @@ impl<C: ServiceClient> Client<C> {
157158 rand:: rng ( ) . random_range ( 0 ..BACKOFF_MAX_JITTER . as_millis ( ) as _ ) ,
158159 ) ;
159160
161+ metrics:: client_connect_attempt_backoff_millis ( delay) ;
162+
160163 match error {
161164 RpcSessionError :: InvalidEndpoint ( error) => {
162165 tracing:: error!( ?error, "Error creating endpoint" ) ;
@@ -177,37 +180,37 @@ impl<C: ServiceClient> Client<C> {
177180 let mut addresses = management_servers. iter ( ) . cycle ( ) ;
178181 let connect_to_server = tryhard:: retry_fn ( || {
179182 let address = addresses. next ( ) ;
180- async move {
183+ let fut = async move {
181184 match address {
182- None => Err ( RpcSessionError :: Receive ( tonic:: Status :: internal (
183- "Failed initial connection" ,
184- ) ) ) ,
185+ None => {
186+ let error = RpcSessionError :: Receive ( tonic:: Status :: internal (
187+ "Failed initial connection" ,
188+ ) ) ;
189+ Err ( error)
190+ }
185191 Some ( endpoint) => {
186192 tracing:: info!( "attempting to connect to `{}`" , endpoint. uri( ) ) ;
187193 let cendpoint = endpoint. clone ( ) ;
188194 let endpoint = endpoint. clone ( ) . connect_timeout ( CONNECTION_TIMEOUT ) ;
189195
190196 // make sure that we have everything we will need in our URI
191197 if endpoint. uri ( ) . scheme ( ) . is_none ( ) {
192- return Err ( RpcSessionError :: InvalidEndpoint (
193- "No scheme provided" . into ( ) ,
194- ) ) ;
198+ return Err ( RpcSessionError :: InvalidEndpoint ( "No scheme provided" . into ( ) ) ) ;
195199 } else if endpoint. uri ( ) . host ( ) . is_none ( ) {
196- return Err ( RpcSessionError :: InvalidEndpoint (
197- "No host provided" . into ( ) ,
198- ) ) ;
200+ return Err ( RpcSessionError :: InvalidEndpoint ( "No host provided" . into ( ) ) ) ;
199201 }
200202
203+ metrics:: client_connect_attempts_total ( endpoint. uri ( ) ) ;
201204 C :: connect_to_endpoint ( endpoint)
202- . instrument ( tracing:: debug_span!(
203- "AggregatedDiscoveryServiceClient::connect_to_endpoint"
204- ) )
205+ . instrument ( tracing:: debug_span!( "C::connect_to_endpoint" ) )
205206 . await
206207 . map_err ( RpcSessionError :: InitialConnect )
207208 . map ( |client| ( client, cendpoint) )
208209 }
209210 }
210- }
211+ } ;
212+
213+ fut. inspect_err ( |error| metrics:: client_errors_total ( & error) )
211214 } )
212215 . with_config ( retry_config) ;
213216
@@ -259,18 +262,29 @@ impl MdsClient {
259262
260263 let mut stream = control_plane. delta_aggregated_resources ( stream) . await ?;
261264 is_healthy. store ( true , Ordering :: SeqCst ) ;
265+ metrics:: client_active ( true ) ;
262266
263267 while let Some ( result) = stream. next ( ) . await {
264- let response = result?;
268+ let response = match result {
269+ Ok ( response) => response,
270+ Err ( error) => {
271+ metrics:: client_errors_total ( & error) ;
272+ break ;
273+ }
274+ } ;
265275 tracing:: trace!( "received delta discovery response" ) ;
266- ds. send_response ( response) . await ?;
276+ if let Err ( error) = ds. send_response ( response) . await {
277+ metrics:: client_errors_total ( & error) ;
278+ break ;
279+ }
267280 }
268281
269282 change_watcher. abort ( ) ;
270283 let _unused = change_watcher. await ;
271284 }
272285
273286 is_healthy. store ( false , Ordering :: SeqCst ) ;
287+ metrics:: client_active ( false ) ;
274288
275289 //tracing::warn!("lost connection to relay server, retrying");
276290 let new_client = MdsClient :: connect_with_backoff ( & self . management_servers )
0 commit comments