Skip to content

Commit a7904fc

Browse files
authored
rich-indexer: Record tip metrics (#5078)
### Related changes - In RichIndexer, record tip in ckb_metrics ### Check List <!--REMOVE the items that are not applicable--> Tests <!-- At least one of them must be included. --> - Unit test - Integration test - Manual test (add detailed scripts or steps below) - No code Side effects - None
1 parent a6dffef commit a7904fc

3 files changed

Lines changed: 32 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

util/rich-indexer/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ ckb-indexer-sync.workspace = true
1919
ckb-jsonrpc-types.workspace = true
2020
ckb-notify.workspace = true
2121
ckb-types.workspace = true
22+
ckb-metrics.workspace = true
2223
futures.workspace = true
2324
log.workspace = true
2425
sql-builder.workspace = true

util/rich-indexer/src/indexer/mod.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,20 @@ pub(crate) struct RichIndexer {
3939
request_limit: usize,
4040
}
4141

42+
fn set_indexer_tip_metric(block_number: BlockNumber) {
43+
let Ok(block_number) = i64::try_from(block_number) else {
44+
log::warn!(
45+
"skip updating ckb_indexer_tip: block number {} exceeds i64::MAX",
46+
block_number
47+
);
48+
return;
49+
};
50+
51+
if let Some(metrics) = ckb_metrics::handle() {
52+
metrics.ckb_indexer_tip.set(block_number);
53+
}
54+
}
55+
4256
impl RichIndexer {
4357
/// Construct new Rich Indexer instance
4458
pub fn new(
@@ -74,13 +88,27 @@ impl IndexerSync for RichIndexer {
7488
/// Appends a new block to the indexer
7589
fn append(&self, block: &BlockView) -> Result<(), Error> {
7690
let future = self.async_rich_indexer.append(block);
77-
self.async_runtime.block_on(future)
91+
let append_ret = self.async_runtime.block_on(future);
92+
93+
if append_ret.is_ok() {
94+
set_indexer_tip_metric(block.number());
95+
}
96+
97+
append_ret
7898
}
7999

80100
/// Rollback the indexer to a previous state
81101
fn rollback(&self) -> Result<(), Error> {
82102
let future = self.async_rich_indexer.rollback();
83-
self.async_runtime.block_on(future)
103+
let rollback_ret = self.async_runtime.block_on(future);
104+
105+
if rollback_ret.is_ok()
106+
&& let Some((number, _)) = self.tip().ok().flatten()
107+
{
108+
set_indexer_tip_metric(number);
109+
}
110+
111+
rollback_ret
84112
}
85113

86114
/// Return identity

0 commit comments

Comments
 (0)