Skip to content

Commit c5c8cbd

Browse files
prestwichclaude
andcommitted
feat(host-rpc): instrument drain_backfill with dynamic fields and metrics
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 159ea6b commit c5c8cbd

1 file changed

Lines changed: 38 additions & 8 deletions

File tree

crates/host-rpc/src/notifier.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -492,50 +492,80 @@ where
492492
///
493493
/// Backfills by number up to `(latest - buffer_capacity)` to leave room
494494
/// for hash-based frontfill of recent blocks.
495-
#[tracing::instrument(skip_all)]
495+
#[tracing::instrument(
496+
level = "info",
497+
skip_all,
498+
fields(
499+
from = tracing::field::Empty,
500+
to = tracing::field::Empty,
501+
batch_size = tracing::field::Empty,
502+
)
503+
)]
496504
async fn drain_backfill(
497505
&mut self,
498506
) -> Option<Result<HostNotification<RpcChainSegment>, RpcHostError>> {
499507
let from = self.backfill_from?;
508+
let start = Instant::now();
509+
510+
let span = tracing::Span::current();
511+
span.record("from", from);
512+
span.record("batch_size", self.backfill_batch_size);
513+
500514
let tip = match self.provider.get_block_number().await {
501515
Ok(n) => n,
502-
Err(e) => return Some(Err(e.into())),
516+
Err(e) => {
517+
crate::metrics::inc_rpc_errors();
518+
return Some(Err(e.into()));
519+
}
503520
};
504521

505-
// Stop backfill before the live zone.
506522
let backfill_ceiling = tip.saturating_sub(self.buffer_capacity as u64);
507523
if from > backfill_ceiling {
508-
// Backfill complete — switch to hash-based frontfill.
509524
self.backfill_from = None;
525+
info!("backfill complete, switching to frontfill");
510526
return None;
511527
}
512528

513529
let to = backfill_ceiling.min(from + self.backfill_batch_size - 1);
530+
span.record("to", to);
514531

515532
let blocks = match self.fetch_range(from, to).await {
516533
Ok(b) => b,
517534
Err(e) => return Some(Err(e)),
518535
};
519536

520-
// Add to chain view.
521537
let view_entries: Vec<(u64, B256)> =
522538
blocks.iter().map(|b| (b.number(), b.hash())).collect();
523539
self.extend_view(&view_entries);
524540

525-
// Advance or clear backfill.
526-
if to >= backfill_ceiling {
541+
let backfill_done = to >= backfill_ceiling;
542+
if backfill_done {
527543
self.backfill_from = None;
528544
} else {
529545
self.backfill_from = Some(to + 1);
530546
}
531547

532-
// Refresh tags using the last block's timestamp.
533548
if let Some(last) = blocks.last()
534549
&& let Err(e) = self.maybe_refresh_tags(last.block.timestamp()).await
535550
{
536551
return Some(Err(e));
537552
}
538553

554+
// Metrics.
555+
let count = blocks.len() as u64;
556+
crate::metrics::inc_blocks_fetched(count, "backfill");
557+
crate::metrics::record_backfill_batch(start.elapsed());
558+
if let Some((tip_num, _)) = self.tip() {
559+
crate::metrics::set_tip(tip_num);
560+
}
561+
crate::metrics::set_chain_view_len(self.chain_view.len());
562+
563+
if backfill_done {
564+
info!("backfill complete, switching to frontfill");
565+
} else {
566+
debug!(blocks_fetched = count, next_from = to + 1, "backfill batch complete");
567+
}
568+
539569
let segment = Arc::new(RpcChainSegment::new(blocks));
540570
Some(Ok(HostNotification {
541571
kind: HostNotificationKind::ChainCommitted { new: segment },

0 commit comments

Comments
 (0)