Skip to content

Commit 42ffb7f

Browse files
committed
feat(sv-api): Add metric to report the latest synced block height.
1 parent 554ecd3 commit 42ffb7f

File tree

7 files changed

+66
-2
lines changed

7 files changed

+66
-2
lines changed

.env.sample

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ TESTNET_RELAYER_V2_LISTENING_CONTRACTS=0x01855B78C1f8868DE70e84507ec735983bf262d
6060
TESTNET_RELAYER_DA_DEPLOY_HEIGHT=5827607
6161
TESTNET_RELAYER_LOG_PAGE_SIZE=2000
6262
TESTNET_SYNC_HEADER_BATCH_SIZE=100
63-
TESTNET_RESERVED_NODES=/dns4/p2p-testnet.fuel.network/tcp/30333/p2p/16Uiu2HAmDxoChB7AheKNvCVpD4PHJwuDGn8rifMBEHmEynGHvHrf,/dns4/p2p-testnet.fuel.network/tcp/30333/p2p/16Uiu2HAmHnANNk4HjAxQV66BNCRxd2MBUU89ijboZkE69aLuSn1g,/dns4/p2p-testnet.fuel.network/tcp/30333/p2p/16Uiu2HAmVE468rpkh2X1kzz8qQXmqNFiPxU5Lrya28nZdbRUdVJX
63+
TESTNET_RESERVED_NODES=/dns/p2p-testnet.fuel.network/tcp/30333/p2p/16Uiu2HAm37z1CHm5XSyi1ChCm9wCxYg98wBn5o5LSXA4RdYnaL33,/dns/p2p-testnet.fuel.network/tcp/30334/p2p/16Uiu2HAmEcoBoXEeakKAuCdQ1rbh9WLdkJc5zM3rfdJbccagCY9F,/dns/p2p-testnet.fuel.network/tcp/30335/p2p/16Uiu2HAmH3gTJU5hekJxSmMugkXLzJV8tHhmiwmJi2rnZ2QkrdXM

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.

services/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dotenvy.workspace = true
2525
fuel-data-parser.workspace = true
2626
fuel-streams-core = { workspace = true, features = ["openapi"] }
2727
fuel-streams-domains.workspace = true
28+
fuel-streams-types.workspace = true
2829
fuel-web-utils.workspace = true
2930
num_cpus.workspace = true
3031
paste = "1.0.15"

services/api/src/main.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use fuel_web_utils::{
44
};
55
use sv_api::{
66
config::Config,
7-
server::{routes::create_routes, state::ServerState},
7+
server::{db_metrics, routes::create_routes, state::ServerState},
88
};
99

1010
#[tokio::main]
@@ -18,6 +18,8 @@ async fn main() -> anyhow::Result<()> {
1818
let config = Config::load()?;
1919
let state = ServerState::new(&config).await?;
2020
let router = create_routes(&state);
21+
db_metrics::spawn_block_height_monitor(&state).await;
22+
2123
ServerBuilder::build(&state, config.api.port)
2224
.with_router(router)
2325
.run()

services/api/src/metrics.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ use std::time::Duration;
33
use async_trait::async_trait;
44
use fuel_web_utils::{api_key::ApiKeyId, telemetry::metrics::TelemetryMetrics};
55
use prometheus::{
6+
register_gauge,
67
register_histogram_vec,
78
register_int_counter_vec,
9+
Gauge,
810
HistogramVec,
911
IntCounterVec,
1012
Registry,
@@ -15,6 +17,7 @@ pub struct Metrics {
1517
pub registry: Registry,
1618
pub db_queries_error_rates: IntCounterVec,
1719
pub connection_duration: HistogramVec,
20+
pub latest_block_height: Gauge,
1821
}
1922

2023
impl Default for Metrics {
@@ -60,6 +63,12 @@ impl Metrics {
6063
)
6164
.expect("metric must be created");
6265

66+
let latest_block_height = register_gauge!(
67+
format!("{}api_latest_block_height", metric_prefix),
68+
"The latest block height in the database"
69+
)
70+
.expect("metric must be created");
71+
6372
let registry =
6473
Registry::new_custom(prefix, None).expect("registry to be created");
6574
registry.register(Box::new(db_queries_error_rates.clone()))?;
@@ -69,6 +78,7 @@ impl Metrics {
6978
registry,
7079
db_queries_error_rates,
7180
connection_duration,
81+
latest_block_height,
7282
})
7383
}
7484

@@ -88,6 +98,10 @@ impl Metrics {
8898
.with_label_values(&[&user_id.to_string(), user_name])
8999
.observe(duration.as_secs_f64());
90100
}
101+
102+
pub fn update_latest_block_height(&self, height: u32) {
103+
self.latest_block_height.set(height as f64);
104+
}
91105
}
92106

93107
#[cfg(test)]
@@ -124,4 +138,21 @@ mod tests {
124138
assert!(output.contains("timeout"));
125139
assert!(output.contains("1"));
126140
}
141+
142+
#[test]
143+
fn test_latest_block_height_metric() {
144+
let metrics = Metrics::random();
145+
146+
metrics.update_latest_block_height(123);
147+
148+
let metric_families = gather();
149+
let mut buffer = Vec::new();
150+
let encoder = TextEncoder::new();
151+
encoder.encode(&metric_families, &mut buffer).unwrap();
152+
153+
let output = String::from_utf8(buffer.clone()).unwrap();
154+
155+
assert!(output.contains("api_latest_block_height"));
156+
assert!(output.contains("123"));
157+
}
127158
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use fuel_streams_domains::blocks::Block;
2+
use fuel_streams_types::BlockHeight;
3+
use tokio::time::{sleep, Duration};
4+
5+
use crate::server::state::ServerState;
6+
7+
pub async fn spawn_block_height_monitor(state: &ServerState) {
8+
let db = state.db.clone();
9+
let telemetry = state.telemetry.clone();
10+
tokio::spawn(async move {
11+
let mut last_height = BlockHeight::from(0);
12+
loop {
13+
if let Ok(new_height) =
14+
Block::find_last_block_height(&db, &Default::default()).await
15+
{
16+
if new_height > last_height {
17+
if let Some(metrics) = telemetry.base_metrics() {
18+
metrics.update_latest_block_height(
19+
new_height.into_inner(),
20+
);
21+
}
22+
last_height = new_height;
23+
}
24+
}
25+
sleep(Duration::from_secs(10)).await;
26+
}
27+
});
28+
}

services/api/src/server/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod db_metrics;
12
pub mod errors;
23
pub mod handlers;
34
pub mod middleware;

0 commit comments

Comments
 (0)