Skip to content

Commit 4bf6ff5

Browse files
committed
better tracking
1 parent b694238 commit 4bf6ff5

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

apps/fortuna/src/keeper/block.rs

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ pub async fn process_single_block_batch(
119119
metrics: Arc<KeeperMetrics>,
120120
fulfilled_requests_cache: Arc<RwLock<HashSet<u64>>>,
121121
) {
122+
let label = ChainIdLabel {
123+
chain_id: chain_state.id.clone(),
124+
};
125+
122126
loop {
123127
let events_res = chain_state
124128
.contract
@@ -129,6 +133,31 @@ pub async fn process_single_block_batch(
129133
)
130134
.await;
131135

136+
// Only update metrics if we successfully retrieved events.
137+
if events_res.is_ok() {
138+
// Track the last time blocks were processed. If anything happens to the processing thread, the
139+
// timestamp will lag, which will trigger an alert.
140+
let server_timestamp = SystemTime::now()
141+
.duration_since(UNIX_EPOCH)
142+
.map(|duration| duration.as_secs() as i64)
143+
.unwrap_or(0);
144+
metrics
145+
.process_event_timestamp
146+
.get_or_create(&label)
147+
.set(server_timestamp);
148+
149+
let current_block = metrics
150+
.process_event_block_number
151+
.get_or_create(&label)
152+
.get();
153+
if block_range.to > current_block as u64 {
154+
metrics
155+
.process_event_block_number
156+
.get_or_create(&label)
157+
.set(block_range.to as i64);
158+
}
159+
}
160+
132161
match events_res {
133162
Ok(events) => {
134163
tracing::info!(num_of_events = &events.len(), "Processing",);
@@ -262,22 +291,8 @@ pub async fn process_new_blocks(
262291
block_delays: Vec<u64>,
263292
) {
264293
tracing::info!("Waiting for new block ranges to process");
265-
let label = ChainIdLabel {
266-
chain_id: chain_state.id.clone(),
267-
};
268294
loop {
269295
if let Some(block_range) = rx.recv().await {
270-
// Track the last time blocks were processed. If anything happens to the processing thread, the
271-
// timestamp will lag, which will trigger an alert.
272-
let server_timestamp = SystemTime::now()
273-
.duration_since(UNIX_EPOCH)
274-
.map(|duration| duration.as_secs() as i64)
275-
.unwrap_or(0);
276-
metrics
277-
.process_event_timestamp
278-
.get_or_create(&label)
279-
.set(server_timestamp);
280-
281296
// Process blocks immediately first
282297
process_block_range(
283298
block_range.clone(),

apps/fortuna/src/keeper/keeper_metrics.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ pub struct KeeperMetrics {
4646
pub block_timestamp_lag: Family<ChainIdLabel, Gauge>,
4747
pub block_timestamp: Family<ChainIdLabel, Gauge>,
4848
pub process_event_timestamp: Family<ChainIdLabel, Gauge>,
49+
pub block_number: Family<ChainIdLabel, Gauge>,
50+
pub process_event_block_number: Family<ChainIdLabel, Gauge>,
4951
}
5052

5153
impl Default for KeeperMetrics {
@@ -91,6 +93,8 @@ impl Default for KeeperMetrics {
9193
block_timestamp_lag: Family::default(),
9294
block_timestamp: Family::default(),
9395
process_event_timestamp: Family::default(),
96+
block_number: Family::default(),
97+
process_event_block_number: Family::default(),
9498
}
9599
}
96100
}
@@ -244,6 +248,18 @@ impl KeeperMetrics {
244248
keeper_metrics.process_event_timestamp.clone(),
245249
);
246250

251+
writable_registry.register(
252+
"block_number",
253+
"The current block number",
254+
keeper_metrics.block_number.clone(),
255+
);
256+
257+
writable_registry.register(
258+
"process_event_block_number",
259+
"The highest block number for which events have been successfully retrieved and processed",
260+
keeper_metrics.process_event_block_number.clone(),
261+
);
262+
247263
// *Important*: When adding a new metric:
248264
// 1. Register it above using `writable_registry.register(...)`
249265
// 2. Add a get_or_create call in the add_chain function below to initialize it for each chain/provider pair
@@ -259,6 +275,10 @@ impl KeeperMetrics {
259275
let _ = self.block_timestamp_lag.get_or_create(&chain_id_label);
260276
let _ = self.block_timestamp.get_or_create(&chain_id_label);
261277
let _ = self.process_event_timestamp.get_or_create(&chain_id_label);
278+
let _ = self.block_number.get_or_create(&chain_id_label);
279+
let _ = self
280+
.process_event_block_number
281+
.get_or_create(&chain_id_label);
262282

263283
let account_label = AccountLabel {
264284
chain_id,

apps/fortuna/src/keeper/track.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,24 @@ pub async fn track_block_timestamp_lag(
5353
};
5454

5555
let block = provider.get_block(BlockNumber::Latest).await?;
56-
let block_timestamp = block.ok_or(anyhow!("block was none"))?.timestamp.as_u64();
56+
let block = block.ok_or(anyhow!("block was none"))?;
57+
let block_timestamp = block.timestamp.as_u64();
5758
let block_timestamp = i64::try_from(block_timestamp)?;
59+
let block_number = block
60+
.number
61+
.ok_or(anyhow!("block number was none"))?
62+
.as_u64();
5863

5964
metrics
6065
.block_timestamp
6166
.get_or_create(&label)
6267
.set(block_timestamp);
6368

69+
metrics
70+
.block_number
71+
.get_or_create(&label)
72+
.set(block_number as i64);
73+
6474
let server_timestamp = i64::try_from(SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs())?;
6575

6676
let lag = server_timestamp - block_timestamp;

0 commit comments

Comments
 (0)