@@ -7,7 +7,7 @@ use axum::{
77use clap:: Parser ;
88use concordium_rust_sdk:: {
99 endpoints:: QueryError ,
10- smart_contracts:: common:: AccountAddress ,
10+ smart_contracts:: common:: { AccountAddress , Amount } ,
1111 v2:: { self , BlockIdentifier } ,
1212} ;
1313use futures:: { stream:: FuturesOrdered , TryStreamExt } ;
@@ -187,12 +187,12 @@ type ServiceState = (
187187 Arc < Vec < ( AccountAddress , GenericGauge < AtomicU64 > ) > > ,
188188) ;
189189
190- # [ tracing :: instrument ( level = "debug" , skip ( client , registry ) ) ]
191- async fn text_metrics (
192- axum :: extract :: State ( ( client , registry , gauges) ) : axum :: extract :: State < ServiceState > ,
193- ) -> Result < String , axum :: response :: ErrorResponse > {
190+ async fn get_data (
191+ client : v2 :: Client ,
192+ gauges : impl Iterator < Item = AccountAddress > ,
193+ ) -> Result < Vec < Amount > , QueryError > {
194194 let mut futures = FuturesOrdered :: new ( ) ;
195- for acc in gauges. iter ( ) . map ( |x| x . 0 ) {
195+ for acc in gauges {
196196 let mut client = client. clone ( ) ;
197197 futures. push_back ( async move {
198198 let acc = client
@@ -202,15 +202,27 @@ async fn text_metrics(
202202 Ok :: < _ , QueryError > ( acc. account_amount )
203203 } )
204204 }
205- match futures. try_collect :: < Vec < _ > > ( ) . await {
206- Ok ( balances) => {
207- for ( balance, gauge) in balances. into_iter ( ) . zip ( gauges. iter ( ) ) {
208- gauge. 1 . set ( balance. micro_ccd ( ) )
209- }
205+ futures. try_collect :: < Vec < _ > > ( ) . await
206+ }
207+
208+ #[ tracing:: instrument( level = "debug" , skip( client, registry) ) ]
209+ async fn text_metrics (
210+ axum:: extract:: State ( ( client, registry, gauges) ) : axum:: extract:: State < ServiceState > ,
211+ ) -> Result < String , axum:: response:: ErrorResponse > {
212+ let result = get_data ( client. clone ( ) , gauges. iter ( ) . map ( |x| x. 0 ) ) . await ;
213+ let balances = match result {
214+ Ok ( balances) => balances,
215+ Err ( e) => {
216+ tracing:: warn!( "Query failed (retrying): {e:#}" ) ;
217+ // Sometimes we get a GoAway from the node. We retry the request once.
218+ get_data ( client. clone ( ) , gauges. iter ( ) . map ( |x| x. 0 ) )
219+ . await
220+ . map_err ( Error :: Query ) ?
210221 }
211- Err ( e) => return Err ( Error :: Query ( e) . into ( ) ) ,
222+ } ;
223+ for ( balance, gauge) in balances. into_iter ( ) . zip ( gauges. iter ( ) ) {
224+ gauge. 1 . set ( balance. micro_ccd ( ) )
212225 }
213-
214226 let encoder = TextEncoder :: new ( ) ;
215227 let metric_families = registry. gather ( ) ;
216228 Ok ( encoder
0 commit comments