|
4 | 4 | chain::ethereum::InstrumentedPythContract, |
5 | 5 | command::register_provider::CommitmentMetadata, |
6 | 6 | config::{Commitment, Config, EthereumConfig, RunOptions}, |
7 | | - eth_utils::traced_client::{RpcMetrics, TracedClient}, |
| 7 | + eth_utils::traced_client::RpcMetrics, |
8 | 8 | keeper::{self, keeper_metrics::KeeperMetrics}, |
9 | 9 | state::{HashChainState, PebbleHashChain}, |
10 | 10 | }, |
11 | 11 | anyhow::{anyhow, Error, Result}, |
12 | 12 | axum::Router, |
13 | | - ethers::{ |
14 | | - middleware::Middleware, |
15 | | - types::{Address, BlockNumber}, |
16 | | - }, |
17 | | - prometheus_client::{ |
18 | | - encoding::EncodeLabelSet, |
19 | | - metrics::{family::Family, gauge::Gauge}, |
20 | | - registry::Registry, |
21 | | - }, |
22 | | - std::{ |
23 | | - collections::HashMap, |
24 | | - net::SocketAddr, |
25 | | - sync::Arc, |
26 | | - time::{Duration, SystemTime, UNIX_EPOCH}, |
27 | | - }, |
| 13 | + ethers::types::Address, |
| 14 | + prometheus_client::{encoding::EncodeLabelSet, registry::Registry}, |
| 15 | + std::{collections::HashMap, net::SocketAddr, sync::Arc}, |
28 | 16 | tokio::{ |
29 | 17 | spawn, |
30 | 18 | sync::{watch, RwLock}, |
31 | | - time, |
32 | 19 | }, |
33 | 20 | tower_http::cors::CorsLayer, |
34 | 21 | utoipa::OpenApi, |
35 | 22 | utoipa_swagger_ui::SwaggerUi, |
36 | 23 | }; |
37 | 24 |
|
38 | | -/// Track metrics in this interval |
39 | | -const TRACK_INTERVAL: Duration = Duration::from_secs(10); |
40 | | - |
41 | 25 | pub async fn run_api( |
42 | 26 | socket_addr: SocketAddr, |
43 | 27 | chains: Arc<RwLock<HashMap<String, api::BlockchainState>>>, |
@@ -158,16 +142,7 @@ pub async fn run(opts: &RunOptions) -> Result<()> { |
158 | 142 | Ok::<(), Error>(()) |
159 | 143 | }); |
160 | 144 |
|
161 | | - // Spawn a thread to track latest block lag. This helps us know if the rpc is up and updated with the latest block. |
162 | | - //TODO: only run this after the chain is setup |
163 | | - spawn(track_block_timestamp_lag( |
164 | | - config, |
165 | | - metrics_registry.clone(), |
166 | | - rpc_metrics.clone(), |
167 | | - )); |
168 | | - |
169 | 145 | run_api(opts.addr, chains.clone(), metrics_registry.clone(), rx_exit).await?; |
170 | | - tracing::info!("Shut down server"); |
171 | 146 | Ok(()) |
172 | 147 | } |
173 | 148 |
|
@@ -271,74 +246,3 @@ async fn setup_chain_state( |
271 | 246 | pub struct ChainLabel { |
272 | 247 | pub chain_id: String, |
273 | 248 | } |
274 | | - |
275 | | -#[tracing::instrument(name = "block_timestamp_lag", skip_all, fields(chain_id = chain_id))] |
276 | | -pub async fn check_block_timestamp_lag( |
277 | | - chain_id: String, |
278 | | - chain_config: EthereumConfig, |
279 | | - metrics: Family<ChainLabel, Gauge>, |
280 | | - rpc_metrics: Arc<RpcMetrics>, |
281 | | -) { |
282 | | - let provider = |
283 | | - match TracedClient::new(chain_id.clone(), &chain_config.geth_rpc_addr, rpc_metrics) { |
284 | | - Ok(r) => r, |
285 | | - Err(e) => { |
286 | | - tracing::error!("Failed to create provider for chain id - {:?}", e); |
287 | | - return; |
288 | | - } |
289 | | - }; |
290 | | - |
291 | | - const INF_LAG: i64 = 1000000; // value that definitely triggers an alert |
292 | | - let lag = match provider.get_block(BlockNumber::Latest).await { |
293 | | - Ok(block) => match block { |
294 | | - Some(block) => { |
295 | | - let block_timestamp = block.timestamp; |
296 | | - let server_timestamp = SystemTime::now() |
297 | | - .duration_since(UNIX_EPOCH) |
298 | | - .unwrap() |
299 | | - .as_secs(); |
300 | | - let lag: i64 = (server_timestamp as i64) - (block_timestamp.as_u64() as i64); |
301 | | - lag |
302 | | - } |
303 | | - None => { |
304 | | - tracing::error!("Block is None"); |
305 | | - INF_LAG |
306 | | - } |
307 | | - }, |
308 | | - Err(e) => { |
309 | | - tracing::error!("Failed to get block - {:?}", e); |
310 | | - INF_LAG |
311 | | - } |
312 | | - }; |
313 | | - metrics |
314 | | - .get_or_create(&ChainLabel { |
315 | | - chain_id: chain_id.clone(), |
316 | | - }) |
317 | | - .set(lag); |
318 | | -} |
319 | | - |
320 | | -/// Tracks the difference between the server timestamp and the latest block timestamp for each chain |
321 | | -pub async fn track_block_timestamp_lag( |
322 | | - config: Config, |
323 | | - metrics_registry: Arc<RwLock<Registry>>, |
324 | | - rpc_metrics: Arc<RpcMetrics>, |
325 | | -) { |
326 | | - let metrics = Family::<ChainLabel, Gauge>::default(); |
327 | | - metrics_registry.write().await.register( |
328 | | - "block_timestamp_lag", |
329 | | - "The difference between server timestamp and latest block timestamp", |
330 | | - metrics.clone(), |
331 | | - ); |
332 | | - loop { |
333 | | - for (chain_id, chain_config) in &config.chains { |
334 | | - spawn(check_block_timestamp_lag( |
335 | | - chain_id.clone(), |
336 | | - chain_config.clone(), |
337 | | - metrics.clone(), |
338 | | - rpc_metrics.clone(), |
339 | | - )); |
340 | | - } |
341 | | - |
342 | | - time::sleep(TRACK_INTERVAL).await; |
343 | | - } |
344 | | -} |
0 commit comments