Skip to content

Commit e1d96dd

Browse files
committed
feat: clean up tracing in robust provider
1 parent 9ef094f commit e1d96dd

File tree

2 files changed

+45
-101
lines changed

2 files changed

+45
-101
lines changed

src/robust_provider/provider.rs

Lines changed: 32 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use alloy::{
1111
use backon::{ExponentialBuilder, Retryable};
1212
use thiserror::Error;
1313
use tokio::time::{error as TokioError, timeout};
14-
use tracing::{error, info};
14+
use tracing::{error, info, instrument, warn};
1515

1616
use crate::robust_provider::RobustSubscription;
1717

@@ -111,20 +111,17 @@ impl<N: Network> RobustProvider<N> {
111111
/// # Errors
112112
///
113113
/// See [retry errors](#retry-errors).
114+
#[instrument(level = "debug", skip(self))]
114115
pub async fn get_block_by_number(
115116
&self,
116117
number: BlockNumberOrTag,
117118
) -> Result<N::BlockResponse, Error> {
118-
info!("eth_getBlockByNumber called");
119119
let result = self
120120
.try_operation_with_failover(
121121
move |provider| async move { provider.get_block_by_number(number).await },
122122
false,
123123
)
124124
.await;
125-
if let Err(e) = &result {
126-
error!(error = %e, "eth_getByBlockNumber failed");
127-
}
128125

129126
result?.ok_or_else(|| Error::BlockNotFound(number.into()))
130127
}
@@ -136,17 +133,14 @@ impl<N: Network> RobustProvider<N> {
136133
/// # Errors
137134
///
138135
/// See [retry errors](#retry-errors).
136+
#[instrument(level = "debug", skip(self))]
139137
pub async fn get_block(&self, id: BlockId) -> Result<N::BlockResponse, Error> {
140-
info!("eth_getBlock called");
141138
let result = self
142139
.try_operation_with_failover(
143140
|provider| async move { provider.get_block(id).await },
144141
false,
145142
)
146143
.await;
147-
if let Err(e) = &result {
148-
error!(error = %e, "eth_getByBlockNumber failed");
149-
}
150144
result?.ok_or_else(|| Error::BlockNotFound(id))
151145
}
152146

@@ -157,19 +151,14 @@ impl<N: Network> RobustProvider<N> {
157151
/// # Errors
158152
///
159153
/// See [retry errors](#retry-errors).
154+
#[instrument(level = "debug", skip(self))]
160155
pub async fn get_block_number(&self) -> Result<BlockNumber, Error> {
161-
info!("eth_getBlockNumber called");
162-
let result = self
163-
.try_operation_with_failover(
164-
move |provider| async move { provider.get_block_number().await },
165-
false,
166-
)
167-
.await
168-
.map_err(Error::from);
169-
if let Err(e) = &result {
170-
error!(error = %e, "eth_getBlockNumber failed");
171-
}
172-
result
156+
self.try_operation_with_failover(
157+
move |provider| async move { provider.get_block_number().await },
158+
false,
159+
)
160+
.await
161+
.map_err(Error::from)
173162
}
174163

175164
/// Get the block number for a given block identifier.
@@ -183,17 +172,14 @@ impl<N: Network> RobustProvider<N> {
183172
/// # Errors
184173
///
185174
/// See [retry errors](#retry-errors).
175+
#[instrument(level = "debug", skip(self))]
186176
pub async fn get_block_number_by_id(&self, block_id: BlockId) -> Result<BlockNumber, Error> {
187-
info!("get_block_number_by_id called");
188177
let result = self
189178
.try_operation_with_failover(
190179
move |provider| async move { provider.get_block_number_by_id(block_id).await },
191180
false,
192181
)
193182
.await;
194-
if let Err(e) = &result {
195-
error!(error = %e, "get_block_number_by_id failed");
196-
}
197183
result?.ok_or_else(|| Error::BlockNotFound(block_id))
198184
}
199185

@@ -210,8 +196,8 @@ impl<N: Network> RobustProvider<N> {
210196
/// # Errors
211197
///
212198
/// See [retry errors](#retry-errors).
199+
#[instrument(level = "debug", skip(self))]
213200
pub async fn get_latest_confirmed(&self, confirmations: u64) -> Result<u64, Error> {
214-
info!("get_latest_confirmed called with confirmations={}", confirmations);
215201
let latest_block = self.get_block_number().await?;
216202
let confirmed_block = latest_block.saturating_sub(confirmations);
217203
Ok(confirmed_block)
@@ -224,17 +210,14 @@ impl<N: Network> RobustProvider<N> {
224210
/// # Errors
225211
///
226212
/// See [retry errors](#retry-errors).
213+
#[instrument(level = "debug", skip(self))]
227214
pub async fn get_block_by_hash(&self, hash: BlockHash) -> Result<N::BlockResponse, Error> {
228-
info!("eth_getBlockByHash called");
229215
let result = self
230216
.try_operation_with_failover(
231217
move |provider| async move { provider.get_block_by_hash(hash).await },
232218
false,
233219
)
234220
.await;
235-
if let Err(e) = &result {
236-
error!(error = %e, "eth_getBlockByHash failed");
237-
}
238221

239222
result?.ok_or_else(|| Error::BlockNotFound(hash.into()))
240223
}
@@ -246,19 +229,14 @@ impl<N: Network> RobustProvider<N> {
246229
/// # Errors
247230
///
248231
/// See [retry errors](#retry-errors).
232+
#[instrument(level = "debug", skip(self, filter))]
249233
pub async fn get_logs(&self, filter: &Filter) -> Result<Vec<Log>, Error> {
250-
info!("eth_getLogs called");
251-
let result = self
252-
.try_operation_with_failover(
253-
move |provider| async move { provider.get_logs(filter).await },
254-
false,
255-
)
256-
.await
257-
.map_err(Error::from);
258-
if let Err(e) = &result {
259-
error!(error = %e, "eth_getLogs failed");
260-
}
261-
result
234+
self.try_operation_with_failover(
235+
move |provider| async move { provider.get_logs(filter).await },
236+
false,
237+
)
238+
.await
239+
.map_err(Error::from)
262240
}
263241

264242
/// Subscribe to new block headers with automatic failover and reconnection.
@@ -273,24 +251,18 @@ impl<N: Network> RobustProvider<N> {
273251
/// # Errors
274252
///
275253
/// see [retry errors](#retry-errors).
254+
#[instrument(level = "debug", skip(self))]
276255
pub async fn subscribe_blocks(&self) -> Result<RobustSubscription<N>, Error> {
277-
info!("eth_subscribe called");
278256
let subscription = self
279257
.try_operation_with_failover(
280258
move |provider| async move {
281259
provider.subscribe_blocks().channel_size(MAX_CHANNEL_SIZE).await
282260
},
283261
true,
284262
)
285-
.await;
263+
.await?;
286264

287-
match subscription {
288-
Ok(sub) => Ok(RobustSubscription::new(sub, self.clone())),
289-
Err(e) => {
290-
error!(error = %e, "eth_subscribe failed");
291-
Err(e.into())
292-
}
293-
}
265+
Ok(RobustSubscription::new(subscription, self.clone()))
294266
}
295267

296268
/// Execute `operation` with exponential backoff and a total timeout.
@@ -361,31 +333,30 @@ impl<N: Network> RobustProvider<N> {
361333
Fut: Future<Output = Result<T, RpcError<TransportErrorKind>>>,
362334
{
363335
let num_fallbacks = self.fallback_providers.len();
364-
if num_fallbacks > 0 && start_index == 0 {
365-
info!("Primary provider failed, trying fallback provider(s)");
366-
}
367336

368337
let fallback_providers = self.fallback_providers.iter().enumerate().skip(start_index);
369338
for (fallback_idx, provider) in fallback_providers {
370339
if require_pubsub && !Self::supports_pubsub(provider) {
371-
info!("Fallback provider {} doesn't support pubsub, skipping", fallback_idx + 1);
340+
warn!(provider_num = fallback_idx + 1, "Fallback provider doesn't support pubsub, skipping");
372341
continue;
373342
}
374-
info!("Attempting fallback provider {}/{}", fallback_idx + 1, num_fallbacks);
375343

376344
match self.try_provider_with_timeout(provider, &operation).await {
377345
Ok(value) => {
378-
info!(provider_num = fallback_idx + 1, "Fallback provider succeeded");
346+
info!(
347+
provider_num = fallback_idx + 1,
348+
total_fallbacks = num_fallbacks,
349+
"Switched to fallback provider"
350+
);
379351
return Ok((value, fallback_idx));
380352
}
381353
Err(e) => {
382-
error!(provider_num = fallback_idx + 1, err = %e, "Fallback provider failed");
383354
last_error = e;
384355
}
385356
}
386357
}
387-
// All fallbacks failed / skipped, return the last error
388-
error!("All providers failed or timed out - returning the last providers attempt's error");
358+
359+
error!("All providers exhausted");
389360
Err(last_error)
390361
}
391362

@@ -405,12 +376,7 @@ impl<N: Network> RobustProvider<N> {
405376

406377
timeout(
407378
self.call_timeout,
408-
(|| operation(provider.clone()))
409-
.retry(retry_strategy)
410-
.notify(|err: &RpcError<TransportErrorKind>, dur: Duration| {
411-
info!(error = %err, "RPC error retrying after {:?}", dur);
412-
})
413-
.sleep(tokio::time::sleep),
379+
(|| operation(provider.clone())).retry(retry_strategy).sleep(tokio::time::sleep),
414380
)
415381
.await
416382
.map_err(CoreError::from)?

src/robust_provider/subscription.rs

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use thiserror::Error;
1515
use tokio::{sync::broadcast::error::RecvError, time::timeout};
1616
use tokio_stream::Stream;
1717
use tokio_util::sync::ReusableBoxFuture;
18-
use tracing::{error, info, warn};
18+
use tracing::{info, warn};
1919

2020
use crate::robust_provider::{RobustProvider, provider::CoreError};
2121

@@ -44,14 +44,8 @@ impl From<CoreError> for Error {
4444
impl From<RecvError> for Error {
4545
fn from(err: RecvError) -> Self {
4646
match err {
47-
RecvError::Closed => {
48-
error!("Provider closed the subscription channel");
49-
Error::Closed
50-
}
51-
RecvError::Lagged(count) => {
52-
error!(skipped = count, "Receiver lagged");
53-
Error::Lagged(count)
54-
}
47+
RecvError::Closed => Error::Closed,
48+
RecvError::Lagged(count) => Error::Lagged(count),
5549
}
5650
}
5751
}
@@ -123,17 +117,7 @@ impl<N: Network> RobustSubscription<N> {
123117
}
124118
return Ok(header);
125119
}
126-
Err(recv_error) => {
127-
match recv_error {
128-
RecvError::Closed => {
129-
error!("Provider closed the subscription channel");
130-
}
131-
RecvError::Lagged(count) => {
132-
error!(skipped = count, "Receiver lagged");
133-
}
134-
}
135-
return Err(recv_error.into());
136-
}
120+
Err(recv_error) => return Err(recv_error.into()),
137121
},
138122
Err(elapsed_err) => {
139123
warn!(
@@ -163,28 +147,22 @@ impl<N: Network> RobustSubscription<N> {
163147
return false;
164148
}
165149

166-
info!("Attempting to reconnect to primary provider");
167-
168150
let operation =
169151
move |provider: RootProvider<N>| async move { provider.subscribe_blocks().await };
170152

171153
let primary = self.robust_provider.primary();
172154
let subscription =
173155
self.robust_provider.try_provider_with_timeout(primary, &operation).await;
174156

175-
match subscription {
176-
Ok(sub) => {
177-
info!("Successfully reconnected to primary provider");
178-
self.subscription = sub;
179-
self.current_fallback_index = None;
180-
self.last_reconnect_attempt = None;
181-
true
182-
}
183-
Err(e) => {
184-
self.last_reconnect_attempt = Some(Instant::now());
185-
warn!(error = %e, "Failed to reconnect to primary provider");
186-
false
187-
}
157+
if let Ok(sub) = subscription {
158+
info!("Reconnected to primary provider");
159+
self.subscription = sub;
160+
self.current_fallback_index = None;
161+
self.last_reconnect_attempt = None;
162+
true
163+
} else {
164+
self.last_reconnect_attempt = Some(Instant::now());
165+
false
188166
}
189167
}
190168

0 commit comments

Comments
 (0)