Skip to content

Commit c76d205

Browse files
authored
fix(fortuna): Refactor and improve timestamp lag gauge (#1632)
Set INF value for gauge if RPC is not accessible. This would trigger the alerts on the monitoring service. We will not do the same for other gauges (e.g. set balance to 0) since that's just duplicate alerts for the same underlying symptom.
1 parent 6535e7f commit c76d205

File tree

3 files changed

+51
-45
lines changed

3 files changed

+51
-45
lines changed

apps/fortuna/Cargo.lock

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

apps/fortuna/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "fortuna"
3-
version = "6.0.0"
3+
version = "6.0.1"
44
edition = "2021"
55

66
[dependencies]

apps/fortuna/src/command/run.rs

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,50 @@ pub struct ChainLabel {
301301
pub chain_id: String,
302302
}
303303

304+
305+
#[tracing::instrument(name = "block_timestamp_lag", skip_all, fields(chain_id = chain_id))]
306+
pub async fn check_block_timestamp_lag(
307+
chain_id: String,
308+
chain_config: EthereumConfig,
309+
metrics: Family<ChainLabel, Gauge>,
310+
) {
311+
let provider = match Provider::<Http>::try_from(&chain_config.geth_rpc_addr) {
312+
Ok(r) => r,
313+
Err(e) => {
314+
tracing::error!("Failed to create provider for chain id - {:?}", e);
315+
return;
316+
}
317+
};
318+
319+
const INF_LAG: i64 = 1000000; // value that definitely triggers an alert
320+
let lag = match provider.get_block(BlockNumber::Latest).await {
321+
Ok(block) => match block {
322+
Some(block) => {
323+
let block_timestamp = block.timestamp;
324+
let server_timestamp = SystemTime::now()
325+
.duration_since(UNIX_EPOCH)
326+
.unwrap()
327+
.as_secs();
328+
let lag: i64 = (server_timestamp as i64) - (block_timestamp.as_u64() as i64);
329+
lag
330+
}
331+
None => {
332+
tracing::error!("Block is None");
333+
INF_LAG
334+
}
335+
},
336+
Err(e) => {
337+
tracing::error!("Failed to get block - {:?}", e);
338+
INF_LAG
339+
}
340+
};
341+
metrics
342+
.get_or_create(&ChainLabel {
343+
chain_id: chain_id.clone(),
344+
})
345+
.set(lag);
346+
}
347+
304348
/// Tracks the difference between the server timestamp and the latest block timestamp for each chain
305349
pub async fn track_block_timestamp_lag(config: Config, metrics_registry: Arc<RwLock<Registry>>) {
306350
let metrics = Family::<ChainLabel, Gauge>::default();
@@ -311,49 +355,11 @@ pub async fn track_block_timestamp_lag(config: Config, metrics_registry: Arc<RwL
311355
);
312356
loop {
313357
for (chain_id, chain_config) in &config.chains {
314-
let chain_id = chain_id.clone();
315-
let chain_config = chain_config.clone();
316-
let metrics = metrics.clone();
317-
318-
spawn(async move {
319-
let chain_id = chain_id.clone();
320-
let chain_config = chain_config.clone();
321-
322-
let provider = match Provider::<Http>::try_from(&chain_config.geth_rpc_addr) {
323-
Ok(r) => r,
324-
Err(e) => {
325-
tracing::error!(
326-
"Failed to create provider for chain id {} - {:?}",
327-
&chain_id,
328-
e
329-
);
330-
return;
331-
}
332-
};
333-
334-
match provider.get_block(BlockNumber::Latest).await {
335-
Ok(b) => {
336-
if let Some(block) = b {
337-
let block_timestamp = block.timestamp;
338-
let server_timestamp = SystemTime::now()
339-
.duration_since(UNIX_EPOCH)
340-
.unwrap()
341-
.as_secs();
342-
let lag: i64 =
343-
(server_timestamp as i64) - (block_timestamp.as_u64() as i64);
344-
345-
metrics
346-
.get_or_create(&ChainLabel {
347-
chain_id: chain_id.clone(),
348-
})
349-
.set(lag);
350-
}
351-
}
352-
Err(e) => {
353-
tracing::error!("Failed to get block for chain id {} - {:?}", &chain_id, e);
354-
}
355-
};
356-
});
358+
spawn(check_block_timestamp_lag(
359+
chain_id.clone(),
360+
chain_config.clone(),
361+
metrics.clone(),
362+
));
357363
}
358364

359365
time::sleep(TRACK_INTERVAL).await;

0 commit comments

Comments
 (0)