Skip to content

Commit

Permalink
feat: export runtime metric to promethues
Browse files Browse the repository at this point in the history
  • Loading branch information
SSebo committed Dec 23, 2023
1 parent c7b3677 commit 29a6aaf
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/common/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ snafu.workspace = true
tokio-util.workspace = true
tokio.workspace = true

tokio-metrics = "0.3"
tokio-metrics-collector = { version = "0.2" }

[dev-dependencies]
tokio-test = "0.4"
16 changes: 14 additions & 2 deletions src/common/runtime/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub struct Builder {
impl Default for Builder {
fn default() -> Self {
Self {
runtime_name: format!("runtime-{}", RUNTIME_ID.fetch_add(1, Ordering::Relaxed)),
runtime_name: format!("runtime_{}", RUNTIME_ID.fetch_add(1, Ordering::Relaxed)),
thread_name: "default-worker".to_string(),
builder: RuntimeBuilder::new_multi_thread(),
}
Expand Down Expand Up @@ -152,15 +152,18 @@ impl Builder {
.build()
.context(BuildRuntimeSnafu)?;

let name = self.runtime_name.clone();
let handle = runtime.handle().clone();
let (send_stop, recv_stop) = oneshot::channel();
// Block the runtime to shutdown.
let _ = thread::Builder::new()
.name(format!("{}-blocker", self.thread_name))
.spawn(move || runtime.block_on(recv_stop));

register_collector(name.clone(), &handle);

Ok(Runtime {
name: self.runtime_name.clone(),
name,
handle,
_dropper: Arc::new(Dropper {
close: Some(send_stop),
Expand All @@ -169,6 +172,12 @@ impl Builder {
}
}

pub fn register_collector(name: String, handle: &Handle) {
let monitor = tokio_metrics::RuntimeMonitor::new(handle);
let collector = tokio_metrics_collector::RuntimeCollector::new(monitor, name.clone());
let _ = prometheus::register(Box::new(collector));
}

fn on_thread_start(thread_name: String) -> impl Fn() + 'static {
move || {
METRIC_RUNTIME_THREADS_ALIVE
Expand Down Expand Up @@ -241,6 +250,9 @@ mod tests {

assert!(metric_text.contains("runtime_threads_idle{thread_name=\"test_runtime_metric\"}"));
assert!(metric_text.contains("runtime_threads_alive{thread_name=\"test_runtime_metric\"}"));
assert!(metric_text.contains("runtime_0_tokio_budget_forced_yield_count"));
assert!(metric_text.contains("runtime_0_tokio_injection_queue_depth"));
assert!(metric_text.contains("runtime_0_tokio_workers_count"));
}

#[test]
Expand Down

0 comments on commit 29a6aaf

Please sign in to comment.