Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for tokio runtime metrics #59

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions autometrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ readme = "README.md"
default = ["opentelemetry"]
metrics = ["dep:metrics"]
opentelemetry = ["opentelemetry_api"]
prometheus-tokio = [ "dep:prometheus_tokio", "tokio" ]
prometheus = ["const_format", "dep:prometheus", "once_cell"]
prometheus-exporter = [
"metrics-exporter-prometheus",
Expand Down Expand Up @@ -45,6 +46,10 @@ prometheus = { version = "0.13", default-features = false, optional = true }
# Used for prometheus feature
const_format = { version = "0.2", features = ["rust_1_51"], optional = true }

# Used for prometheus-tokio feature
prometheus_tokio = { version = "0.2", optional = true }
tokio = { version = "1.26", features = ["rt"], optional = true }

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]
29 changes: 29 additions & 0 deletions autometrics/src/prometheus_exporter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ impl GlobalPrometheus {

Ok(output)
}

/// Add tokio runtime metrics from the given [Runtime handle](tokio::runtime::Handle)
///
/// On top of the feature, you also need to add the RUSTFLAGS to [enable
/// `tokio_unstable`](https://docs.rs/tokio/latest/tokio/#unstable-features)
/// in order to use this method.
#[cfg(feature = "prometheus-tokio")]
pub fn with_tokio(&self, handle: &tokio::runtime::Handle) -> &Self {
use prometheus_tokio::TokioCollector;
self.exporter
.registry()
.register(Box::new(TokioCollector::new(handle, "autometrics")));
&self
}
}

/// Initialize the global Prometheus metrics collector and exporter.
Expand All @@ -48,6 +62,21 @@ impl GlobalPrometheus {
/// let _exporter = global_metrics_exporter();
/// # }
/// ```
///
/// # Tokio runtime metrics support
///
/// If you both:
/// - enabled the `prometheus-tokio` feature of autometrics, and
/// - added the RUSTFLAGS to [enable `tokio_unstable`](https://docs.rs/tokio/latest/tokio/#unstable-features), then
///
/// you can also add the Tokio related runtime metrics to the exporter:
/// ```rust,ignore
/// #[tokio::main]
/// async fn main() {
/// let _exporter = global_metrics_exporter().with_tokio(&tokio::runtime::Handle::current());
/// // ...
/// }
/// ```
pub fn global_metrics_exporter() -> GlobalPrometheus {
GLOBAL_EXPORTER.clone()
}
Expand Down