Skip to content

Commit 5aa5771

Browse files
authored
Observe metrics from the Tokio runtime (#4284)
2 parents ceab387 + 0b764c1 commit 5aa5771

File tree

6 files changed

+469
-4
lines changed

6 files changed

+469
-4
lines changed

.cargo/config.toml

+4-1
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"]

.github/workflows/coverage.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ jobs:
116116
run: |
117117
cargo test --no-fail-fast --workspace
118118
env:
119-
RUSTFLAGS: "-Cinstrument-coverage"
119+
RUSTFLAGS: "-Cinstrument-coverage --cfg tokio_unstable"
120120
LLVM_PROFILE_FILE: "cargo-test-%p-%m.profraw"
121121
DATABASE_URL: postgresql://postgres:postgres@localhost/postgres
122122
SQLX_OFFLINE: "1"

crates/cli/build.rs

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
use vergen_gitcl::{Emitter, GitclBuilder, RustcBuilder};
77

88
fn main() -> anyhow::Result<()> {
9+
// Instruct rustc that we'll be using #[cfg(tokio_unstable)]
10+
println!("cargo::rustc-check-cfg=cfg(tokio_unstable)");
11+
912
// At build time, we override the version through the environment variable
1013
// VERGEN_GIT_DESCRIBE. In some contexts, it means this variable is set but
1114
// empty, so we unset it here.

crates/cli/src/main.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,23 @@ impl sentry::TransportFactory for SentryTransportFactory {
4949
}
5050
}
5151

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

crates/cli/src/telemetry.rs

+5
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)