Skip to content

Commit eb3dd71

Browse files
committed
chore: move task to MetricsRunner
1 parent e61c2c6 commit eb3dd71

File tree

5 files changed

+44
-77
lines changed

5 files changed

+44
-77
lines changed

src/core.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ pub async fn main(cli: &Cli) -> Result<(), Error> {
134134
custom_domain_providers,
135135
&mut tasks,
136136
http_client.clone(),
137-
route_provider,
137+
Arc::clone(&route_provider),
138138
&registry,
139139
clickhouse.clone(),
140140
vector.clone(),
@@ -183,7 +183,7 @@ pub async fn main(cli: &Cli) -> Result<(), Error> {
183183

184184
// Setup metrics
185185
if let Some(addr) = cli.metrics.metrics_listen {
186-
let router = metrics::setup(&registry, &mut tasks);
186+
let router = metrics::setup(&registry, &mut tasks, route_provider);
187187

188188
let srv = Arc::new(http::Server::new(
189189
http::server::Addr::Tcp(addr),

src/metrics/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use axum::{
1616
Router,
1717
};
1818
use http::header::{CONTENT_TYPE, ORIGIN, REFERER, USER_AGENT};
19+
use ic_agent::agent::route_provider::RouteProvider;
1920
use ic_bn_lib::{
2021
http::{
2122
body::CountingBody,
@@ -54,9 +55,9 @@ pub const HTTP_DURATION_BUCKETS: &[f64] = &[0.05, 0.2, 1.0, 2.0];
5455
pub const HTTP_REQUEST_SIZE_BUCKETS: &[f64] = &[128.0, KB, 2.0 * KB, 4.0 * KB, 8.0 * KB];
5556
pub const HTTP_RESPONSE_SIZE_BUCKETS: &[f64] = &[1.0 * KB, 8.0 * KB, 64.0 * KB, 256.0 * KB];
5657

57-
pub fn setup(registry: &Registry, tasks: &mut TaskManager) -> Router {
58+
pub fn setup(registry: &Registry, tasks: &mut TaskManager, route_provider: Arc<dyn RouteProvider>) -> Router {
5859
let cache = Arc::new(runner::MetricsCache::new());
59-
let runner = Arc::new(runner::MetricsRunner::new(cache.clone(), registry));
60+
let runner = Arc::new(runner::MetricsRunner::new(cache.clone(), registry, route_provider));
6061
tasks.add_interval("metrics_runner", runner, Duration::from_secs(5));
6162

6263
Router::new()

src/metrics/runner.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use async_trait::async_trait;
66
use axum::{extract::State, response::IntoResponse};
77
use bytes::{BufMut, Bytes, BytesMut};
88
use http::header::CONTENT_TYPE;
9+
use ic_agent::agent::route_provider::RouteProvider;
910
use ic_bn_lib::tasks::Run;
10-
use prometheus::{register_int_gauge_with_registry, Encoder, IntGauge, Registry, TextEncoder};
11+
use prometheus::{Encoder, IntGauge, Registry, TextEncoder, register_int_gauge_with_registry};
1112
use tikv_jemalloc_ctl::{epoch, stats};
1213
use tokio_util::sync::CancellationToken;
1314
use tracing::{debug, warn};
@@ -31,15 +32,22 @@ pub struct MetricsRunner {
3132
metrics_cache: Arc<MetricsCache>,
3233
registry: Registry,
3334
encoder: TextEncoder,
34-
35+
route_provider: Arc<dyn RouteProvider>,
3536
// Metrics
3637
mem_allocated: IntGauge,
3738
mem_resident: IntGauge,
39+
// API boundary nodes metrics
40+
total_api_boundary_nodes: IntGauge,
41+
healthy_api_boundary_nodes: IntGauge,
3842
}
3943

4044
// Snapshots & encodes the metrics for the handler to export
4145
impl MetricsRunner {
42-
pub fn new(metrics_cache: Arc<MetricsCache>, registry: &Registry) -> Self {
46+
pub fn new(
47+
metrics_cache: Arc<MetricsCache>,
48+
registry: &Registry,
49+
route_provider: Arc<dyn RouteProvider>,
50+
) -> Self {
4351
let mem_allocated = register_int_gauge_with_registry!(
4452
format!("memory_allocated"),
4553
format!("Allocated memory in bytes"),
@@ -54,12 +62,29 @@ impl MetricsRunner {
5462
)
5563
.unwrap();
5664

65+
let total_api_boundary_nodes = register_int_gauge_with_registry!(
66+
format!("total_api_boundary_nodes"),
67+
format!("Total number of existing API boundary nodes (both healthy and unhealthy)."),
68+
registry
69+
)
70+
.unwrap();
71+
72+
let healthy_api_boundary_nodes = register_int_gauge_with_registry!(
73+
format!("healthy_api_boundary_nodes"),
74+
format!("Number of currently healthy API boundary nodes"),
75+
registry,
76+
)
77+
.unwrap();
78+
5779
Self {
5880
metrics_cache,
5981
registry: registry.clone(),
6082
encoder: TextEncoder::new(),
6183
mem_allocated,
6284
mem_resident,
85+
route_provider,
86+
healthy_api_boundary_nodes,
87+
total_api_boundary_nodes,
6388
}
6489
}
6590
}
@@ -85,6 +110,12 @@ impl MetricsRunner {
85110
.buffer
86111
.store(Arc::new(buffer.into_inner().freeze()));
87112

113+
// Update API boundary nodes stats
114+
let stats = self.route_provider.routes_stats();
115+
self.total_api_boundary_nodes.set(stats.total as i64);
116+
self.healthy_api_boundary_nodes
117+
.set(stats.healthy.unwrap_or(0) as i64);
118+
88119
Ok(())
89120
}
90121
}

src/routing/ic/route_provider.rs

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,23 @@
1-
use std::{sync::Arc, time::Duration};
1+
use std::sync::Arc;
2+
23
use anyhow::anyhow;
3-
use async_trait::async_trait;
44
use candid::Principal;
55
use ic_agent::agent::http_transport::reqwest_transport::reqwest::Client as AgentClient;
66
use ic_agent::agent::route_provider::{
7-
RoundRobinRouteProvider, RouteProvider,
87
dynamic_routing::{
98
dynamic_route_provider::DynamicRouteProviderBuilder, node::Node,
109
snapshot::latency_based_routing::LatencyRoutingSnapshot,
1110
},
11+
RoundRobinRouteProvider, RouteProvider,
1212
};
13-
use ic_bn_lib::tasks::Run;
14-
use prometheus::{IntGauge, Registry, register_int_gauge_with_registry};
15-
use tokio_util::sync::CancellationToken;
1613
use tracing::info;
1714
use url::Url;
1815

1916
use crate::routing::ic::{
20-
health_check::{CHECK_TIMEOUT, HealthChecker},
21-
nodes_fetcher::{MAINNET_ROOT_SUBNET_ID, NodesFetcher},
17+
health_check::{HealthChecker, CHECK_TIMEOUT},
18+
nodes_fetcher::{NodesFetcher, MAINNET_ROOT_SUBNET_ID},
2219
};
2320

24-
pub const API_BOUNDARY_NODES_STATS_REFRESH_INTERVAL: Duration = Duration::from_secs(30);
25-
2621
pub async fn setup_route_provider(
2722
urls: &[Url],
2823
ic_use_discovery: bool,
@@ -70,56 +65,3 @@ pub async fn setup_route_provider(
7065

7166
Ok(route_provider)
7267
}
73-
74-
struct ApiBoundaryNodesMetrics {
75-
total_nodes: IntGauge,
76-
healthy_nodes: IntGauge,
77-
}
78-
79-
pub struct ApiBoundaryNodesStats {
80-
route_provider: Arc<dyn RouteProvider>,
81-
metrics: ApiBoundaryNodesMetrics,
82-
}
83-
84-
impl ApiBoundaryNodesMetrics {
85-
fn new(registry: &Registry) -> Self {
86-
Self {
87-
total_nodes: register_int_gauge_with_registry!(
88-
format!("total_api_boundary_nodes"),
89-
format!(
90-
"Total number of existing API boundary nodes (both healthy and unhealthy)."
91-
),
92-
registry
93-
)
94-
.unwrap(),
95-
96-
healthy_nodes: register_int_gauge_with_registry!(
97-
format!("healthy_api_boundary_nodes"),
98-
format!("Number of currently healthy API boundary nodes"),
99-
registry,
100-
)
101-
.unwrap(),
102-
}
103-
}
104-
}
105-
106-
impl ApiBoundaryNodesStats {
107-
pub fn new(route_provider: Arc<dyn RouteProvider>, registry: &Registry) -> Self {
108-
Self {
109-
route_provider,
110-
metrics: ApiBoundaryNodesMetrics::new(registry),
111-
}
112-
}
113-
}
114-
115-
#[async_trait]
116-
impl Run for ApiBoundaryNodesStats {
117-
async fn run(&self, _: CancellationToken) -> Result<(), anyhow::Error> {
118-
let stats = self.route_provider.routes_stats();
119-
self.metrics.total_nodes.set(stats.total as i64);
120-
self.metrics
121-
.healthy_nodes
122-
.set(stats.healthy.unwrap_or(0) as i64);
123-
Ok(())
124-
}
125-
}

src/routing/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use domain::{CustomDomainStorage, DomainResolver, ProvidesCustomDomains};
2121
use fqdn::FQDN;
2222
use http::{method::Method, uri::PathAndQuery, StatusCode, Uri};
2323
use ic_agent::agent::route_provider::RouteProvider;
24-
use ic::route_provider::{ApiBoundaryNodesStats, API_BOUNDARY_NODES_STATS_REFRESH_INTERVAL};
2524
use ic_bn_lib::{
2625
http::{
2726
cache::{Cache, KeyExtractorUriRange, Opts},
@@ -319,12 +318,6 @@ pub fn setup_router(
319318
// Prepare the HTTP->IC library
320319
let client = ic::setup(cli, http_client.clone(), route_provider.clone())?;
321320

322-
tasks.add_interval(
323-
"api_boundary_nodes_stats",
324-
Arc::new(ApiBoundaryNodesStats::new(route_provider.clone(), registry)),
325-
API_BOUNDARY_NODES_STATS_REFRESH_INTERVAL,
326-
);
327-
328321
// Prepare the states
329322
let state_handler = Arc::new(handler::HandlerState::new(
330323
client,

0 commit comments

Comments
 (0)