Skip to content

Commit 0eef3a4

Browse files
DewyerBarnabas Ratki
andauthored
feat: Add tokio runtime metrics (#539)
Co-authored-by: Barnabas Ratki <barna@dourolabs.xyz>
1 parent 71d88ab commit 0eef3a4

5 files changed

Lines changed: 107 additions & 1 deletion

File tree

.cargo/config.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build]
2+
rustflags = ["--cfg", "tokio_unstable"]
3+
rustdocflags = ["--cfg", "tokio_unstable"]

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

auction-server/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ anchor-lang-idl = { version = "0.1.1", features = ["convert"] }
99

1010
[dependencies]
1111
arc-swap = "1.7.1"
12-
tokio = { workspace = true, features = ["macros", "sync", "rt-multi-thread", "signal"] }
12+
tokio = { workspace = true, features = ["macros", "sync", "rt-multi-thread", "signal", "rt"] }
1313
tokio-stream = { workspace = true }
1414
tower-http = { workspace = true, features = ["cors"] }
1515
serde = { workspace = true, features = ["derive"] }
@@ -59,6 +59,7 @@ mockall_double = "0.3.1"
5959
spl-memo-client = { workspace = true }
6060
spl-token-2022 = { workspace = true }
6161
humantime-serde = "1.1.1"
62+
tokio-metrics = { version = "0.4.2", features = ["rt"] }
6263

6364
[dev-dependencies]
6465
mockall = "0.13.1"

auction-server/src/per_metrics.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use {
2424
},
2525
time::Instant,
2626
},
27+
tokio_metrics::RuntimeMonitor,
2728
tracing::{
29+
error,
2830
field::{
2931
Field,
3032
Visit,
@@ -165,6 +167,84 @@ where
165167
}
166168
}
167169

170+
pub async fn update_tokio_runtime_metrics(runtime_monitor: &RuntimeMonitor) {
171+
let Some(metrics) = runtime_monitor.intervals().next() else {
172+
error!("No tokio runtime metrics available");
173+
return;
174+
};
175+
176+
// Worker metrics
177+
metrics::gauge!("tokio_workers_count").set(metrics.workers_count as f64);
178+
179+
// Park count metrics
180+
metrics::gauge!("tokio_total_park_count").set(metrics.total_park_count as f64);
181+
metrics::gauge!("tokio_max_park_count").set(metrics.max_park_count as f64);
182+
metrics::gauge!("tokio_min_park_count").set(metrics.min_park_count as f64);
183+
184+
// Poll duration metrics
185+
metrics::gauge!("tokio_mean_poll_duration_ns")
186+
.set(metrics.mean_poll_duration.as_nanos() as f64);
187+
metrics::gauge!("tokio_mean_poll_duration_worker_min_ns")
188+
.set(metrics.mean_poll_duration_worker_min.as_nanos() as f64);
189+
metrics::gauge!("tokio_mean_poll_duration_worker_max_ns")
190+
.set(metrics.mean_poll_duration_worker_max.as_nanos() as f64);
191+
192+
// Noop metrics
193+
metrics::gauge!("tokio_total_noop_count").set(metrics.total_noop_count as f64);
194+
metrics::gauge!("tokio_max_noop_count").set(metrics.max_noop_count as f64);
195+
metrics::gauge!("tokio_min_noop_count").set(metrics.min_noop_count as f64);
196+
197+
// Steal metrics
198+
metrics::gauge!("tokio_total_steal_count").set(metrics.total_steal_count as f64);
199+
metrics::gauge!("tokio_max_steal_count").set(metrics.max_steal_count as f64);
200+
metrics::gauge!("tokio_min_steal_count").set(metrics.min_steal_count as f64);
201+
metrics::gauge!("tokio_total_steal_operations").set(metrics.total_steal_operations as f64);
202+
metrics::gauge!("tokio_max_steal_operations").set(metrics.max_steal_operations as f64);
203+
metrics::gauge!("tokio_min_steal_operations").set(metrics.min_steal_operations as f64);
204+
205+
// Schedule metrics
206+
metrics::gauge!("tokio_num_remote_schedules").set(metrics.num_remote_schedules as f64);
207+
metrics::gauge!("tokio_total_local_schedule_count")
208+
.set(metrics.total_local_schedule_count as f64);
209+
metrics::gauge!("tokio_max_local_schedule_count").set(metrics.max_local_schedule_count as f64);
210+
metrics::gauge!("tokio_min_local_schedule_count").set(metrics.min_local_schedule_count as f64);
211+
212+
// Overflow metrics
213+
metrics::gauge!("tokio_total_overflow_count").set(metrics.total_overflow_count as f64);
214+
metrics::gauge!("tokio_max_overflow_count").set(metrics.max_overflow_count as f64);
215+
metrics::gauge!("tokio_min_overflow_count").set(metrics.min_overflow_count as f64);
216+
217+
// Polls metrics
218+
metrics::gauge!("tokio_total_polls_count").set(metrics.total_polls_count as f64);
219+
metrics::gauge!("tokio_max_polls_count").set(metrics.max_polls_count as f64);
220+
metrics::gauge!("tokio_min_polls_count").set(metrics.min_polls_count as f64);
221+
222+
// Busy duration metrics
223+
metrics::gauge!("tokio_total_busy_duration_ns")
224+
.set(metrics.total_busy_duration.as_nanos() as f64);
225+
metrics::gauge!("tokio_max_busy_duration_ns").set(metrics.max_busy_duration.as_nanos() as f64);
226+
metrics::gauge!("tokio_min_busy_duration_ns").set(metrics.min_busy_duration.as_nanos() as f64);
227+
228+
// Queue depth metrics
229+
metrics::gauge!("tokio_global_queue_depth").set(metrics.global_queue_depth as f64);
230+
metrics::gauge!("tokio_total_local_queue_depth").set(metrics.total_local_queue_depth as f64);
231+
metrics::gauge!("tokio_max_local_queue_depth").set(metrics.max_local_queue_depth as f64);
232+
metrics::gauge!("tokio_min_local_queue_depth").set(metrics.min_local_queue_depth as f64);
233+
metrics::gauge!("tokio_blocking_queue_depth").set(metrics.blocking_queue_depth as f64);
234+
235+
// Task and thread metrics
236+
metrics::gauge!("tokio_live_tasks_count").set(metrics.live_tasks_count as f64);
237+
metrics::gauge!("tokio_blocking_threads_count").set(metrics.blocking_threads_count as f64);
238+
metrics::gauge!("tokio_idle_blocking_threads_count")
239+
.set(metrics.idle_blocking_threads_count as f64);
240+
241+
// Other metrics
242+
metrics::gauge!("tokio_elapsed_us").set(metrics.elapsed.as_micros() as f64);
243+
metrics::gauge!("tokio_budget_forced_yield_count")
244+
.set(metrics.budget_forced_yield_count as f64);
245+
metrics::gauge!("tokio_io_driver_ready_count").set(metrics.io_driver_ready_count as f64);
246+
}
247+
168248
pub async fn start_metrics(run_options: RunOptions, server_state: Arc<ServerState>) -> Result<()> {
169249
tracing::info!("Starting Metrics Server...");
170250

auction-server/src/server.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,15 @@ pub async fn start_server(run_options: RunOptions) -> Result<()> {
478478
let service = store_new.opportunity_service_svm.clone();
479479
async move { service.update_metrics().await }
480480
}),
481+
metric_collector("tokio runtime metrics".to_string(), || {
482+
let handle = tokio::runtime::Handle::current();
483+
let runtime_monitor = tokio_metrics::RuntimeMonitor::new(&handle);
484+
485+
async move {
486+
let rt = runtime_monitor;
487+
per_metrics::update_tokio_runtime_metrics(&rt).await
488+
}
489+
}),
481490
fault_tolerant_handler("start api".to_string(), || api::start_api(
482491
run_options.clone(),
483492
store_new.clone(),

0 commit comments

Comments
 (0)