Skip to content

Commit 0e9765b

Browse files
committed
Observe metrics from the Tokio runtime
1 parent e5ebb84 commit 0e9765b

File tree

5 files changed

+464
-3
lines changed

5 files changed

+464
-3
lines changed

.cargo/config.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
[build]
2+
rustflags = ["--cfg", "tokio_unstable"]
3+
14
# On x86_64, we target the x86-64-v2 psABI, as it is a good compromise between
25
# modern CPU instructions and compatibility.
36
[target.x86_64-unknown-linux-gnu]
4-
rustflags = ["-C", "target-cpu=x86-64-v2"]
7+
rustflags = ["--cfg", "tokio_unstable", "-C", "target-cpu=x86-64-v2"]

crates/cli/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
use vergen_gitcl::{Emitter, GitclBuilder, RustcBuilder};
77

88
fn main() -> anyhow::Result<()> {
9+
println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");
10+
911
// At build time, we override the version through the environment variable
1012
// VERGEN_GIT_DESCRIBE. In some contexts, it means this variable is set but
1113
// empty, so we unset it here.

crates/cli/src/main.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use anyhow::Context;
1212
use clap::Parser;
1313
use mas_config::{ConfigurationSection, TelemetryConfig};
1414
use sentry_tracing::EventFilter;
15+
use tokio::runtime::{HistogramConfiguration, LogHistogram};
1516
use tracing_subscriber::{
1617
EnvFilter, Layer, Registry, filter::LevelFilter, layer::SubscriberExt, util::SubscriberInitExt,
1718
};
@@ -49,8 +50,19 @@ impl sentry::TransportFactory for SentryTransportFactory {
4950
}
5051
}
5152

52-
#[tokio::main]
53-
async fn main() -> anyhow::Result<ExitCode> {
53+
fn main() -> anyhow::Result<ExitCode> {
54+
let runtime = tokio::runtime::Builder::new_multi_thread()
55+
.enable_all()
56+
.enable_metrics_poll_time_histogram()
57+
.metrics_poll_time_histogram_configuration(HistogramConfiguration::log(
58+
LogHistogram::default(),
59+
))
60+
.build()?;
61+
62+
runtime.block_on(async_main())
63+
}
64+
65+
async fn async_main() -> anyhow::Result<ExitCode> {
5466
// We're splitting the "fallible" part of main in another function to have a
5567
// chance to shutdown the telemetry exporters regardless of if there was an
5668
// error or not

crates/cli/src/telemetry.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
// SPDX-License-Identifier: AGPL-3.0-only
55
// Please see LICENSE in the repository root for full details.
66

7+
mod tokio;
8+
79
use std::sync::{LazyLock, OnceLock};
810

911
use anyhow::Context as _;
@@ -60,6 +62,9 @@ pub fn setup(config: &TelemetryConfig) -> anyhow::Result<()> {
6062
init_tracer(&config.tracing).context("Failed to configure traces exporter")?;
6163
init_meter(&config.metrics).context("Failed to configure metrics exporter")?;
6264

65+
let handle = ::tokio::runtime::Handle::current();
66+
self::tokio::observe(handle.metrics());
67+
6368
Ok(())
6469
}
6570

0 commit comments

Comments
 (0)