Skip to content

Commit 5be04b1

Browse files
authored
feat: Make the collector module public (#63)
* feat: Make the collector module public This allows collecting the metrics without sending them to a process-wide registered metrics-rs collector. Useful for integration into other metrics clients.
1 parent e62acd8 commit 5be04b1

File tree

10 files changed

+58
-18
lines changed

10 files changed

+58
-18
lines changed

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ keywords = [
2020
all-features = true
2121

2222
[features]
23+
default = ["metrics-rs"]
24+
# Enable the metrics-rs collector integration.
25+
metrics-rs = ["dep:metrics"]
2326
# Enable a `dummy` collector that always return an empty `Metrics` for non supported platforms
2427
dummy = []
2528
# Use a Gauge on `process_cpu_seconds_total` metrics instead of Counter to represent f64 value.
@@ -30,7 +33,7 @@ use-gauge-on-cpu-seconds-total = []
3033
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
3134

3235
[dependencies]
33-
metrics = "0.24.0"
36+
metrics = { version = "0.24.0", optional = true }
3437

3538
[target.'cfg(target_os = "macos")'.dependencies]
3639
libproc = "0.14.2"

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,11 @@ async fn main() {
131131

132132
This crate offers the following features:
133133

134-
| Feature Name | Description |
135-
| -------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
136-
| `dummy` | Enables a dummy collector that returns an empty `Metrics` on non-supported platforms. |
137-
| `use-gauge-on-cpu-seconds-total` | Use a Gauge on `process_cpu_seconds_total` metrics instead of Counter to represent `f64` value. This is a previous behavior prior to version 2.0.0. |
134+
| Feature Name | Description |
135+
| --------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
136+
| `dummy` | Enables a dummy collector that returns an empty `Metrics` on non-supported platforms. |
137+
| `use-gauge-on-cpu-seconds-total` | Use a Gauge on `process_cpu_seconds_total` metrics instead of Counter to represent `f64` value. This is a previous behavior prior to version 2.0.0. |
138+
| `metrics-rs` (enabled by default) | Enables the [metrics] integration. Can be removed to reduce dependencies if unused.
138139

139140
# License
140141

src/collector.rs

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
#[cfg_attr(target_os = "macos", path = "collector/macos.rs")]
2-
#[cfg_attr(target_os = "linux", path = "collector/linux.rs")]
3-
#[cfg_attr(target_os = "windows", path = "collector/windows.rs")]
4-
#[cfg_attr(target_os = "freebsd", path = "collector/freebsd.rs")]
5-
#[cfg_attr(target_os = "openbsd", path = "collector/openbsd.rs")]
1+
//! Raw metrics for the running process.
2+
//!
3+
//! This module contains the implementation to collect the Prometheus metrics for the
4+
//! running process. This can be useful to export these metrics via custom mechanisms
5+
//! rather than via the [metrics] crate.
6+
//!
7+
//! Use the [`collect`] function to create a snapshot of the current metrics.
8+
//!
9+
//! To export these metrics via the [metrics] crate however it is recommended to use the
10+
//! [`Collector`] struct.
11+
//!
12+
//! [`Collector`]: crate::Collector
13+
14+
#[cfg_attr(target_os = "macos", path = "implementation/macos.rs")]
15+
#[cfg_attr(target_os = "linux", path = "implementation/linux.rs")]
16+
#[cfg_attr(target_os = "windows", path = "implementation/windows.rs")]
17+
#[cfg_attr(target_os = "freebsd", path = "implementation/freebsd.rs")]
18+
#[cfg_attr(target_os = "openbsd", path = "implementation/openbsd.rs")]
619
#[allow(unused_attributes)]
7-
#[cfg_attr(feature = "dummy", path = "collector/dummy.rs")]
8-
mod collector;
20+
#[cfg_attr(feature = "dummy", path = "implementation/dummy.rs")]
21+
mod implementation;
922

1023
#[cfg(all(
1124
not(feature = "dummy"),
@@ -21,22 +34,32 @@ compile_error!(
2134
"A feature \"dummy\" must be enabled to compile this crate on non supported platforms."
2235
);
2336

24-
pub use collector::collect;
37+
/// Creates a snapshot of the running process' [`Metrics`].
38+
///
39+
/// Creates a new instance of [`Metrics`] with the current values of the running process.
40+
pub use implementation::collect;
2541

26-
/// Process metrics
27-
/// https://prometheus.io/docs/instrumenting/writing_clientlibs/#process-metrics
42+
/// Standard Prometheus process metrics.
43+
///
44+
/// This struct describes the standard set of Prometheus process metrics as described at
45+
/// <https://prometheus.io/docs/instrumenting/writing_clientlibs/#process-metrics>.
46+
///
47+
/// To create a populated struct for the running process use the [`collect`] function. The
48+
/// `Default` impl does not populate any metrics.
2849
#[derive(Debug, Default, PartialEq)]
2950
pub struct Metrics {
3051
/// Total user and system CPU time spent in seconds.
3152
pub cpu_seconds_total: Option<f64>,
3253
/// Number of open file descriptors.
3354
pub open_fds: Option<u64>,
3455
/// Maximum number of open file descriptors.
56+
///
3557
/// 0 indicates 'unlimited'.
3658
pub max_fds: Option<u64>,
3759
/// Virtual memory size in bytes.
3860
pub virtual_memory_bytes: Option<u64>,
3961
/// Maximum amount of virtual memory available in bytes.
62+
///
4063
/// 0 indicates 'unlimited'.
4164
pub virtual_memory_max_bytes: Option<u64>,
4265
/// Resident memory size in bytes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

src/lib.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#![doc = include_str!("../README.md")]
2-
mod collector;
2+
pub mod collector;
33

4+
#[cfg(feature = "metrics-rs")]
45
use std::sync::Arc;
56

7+
#[cfg(feature = "metrics-rs")]
68
use metrics::{describe_gauge, gauge, Unit};
79

8-
#[cfg(not(feature = "use-gauge-on-cpu-seconds-total"))]
10+
#[cfg(all(
11+
feature = "metrics-rs",
12+
not(feature = "use-gauge-on-cpu-seconds-total")
13+
))]
914
use metrics::{counter, describe_counter};
1015

1116
/// Metrics names
17+
#[cfg(feature = "metrics-rs")]
1218
#[derive(Debug, PartialEq, Eq)]
1319
struct Metrics {
1420
cpu_seconds_total: Arc<str>,
@@ -21,6 +27,7 @@ struct Metrics {
2127
threads: Arc<str>,
2228
}
2329

30+
#[cfg(feature = "metrics-rs")]
2431
impl Metrics {
2532
// Create new Metrics, allocating prefixed strings for metrics names.
2633
fn new(prefix: impl AsRef<str>) -> Self {
@@ -38,19 +45,25 @@ impl Metrics {
3845
}
3946
}
4047

48+
#[cfg(feature = "metrics-rs")]
4149
impl Default for Metrics {
4250
// Create new Metrics, without prefixing and thus allocating.
4351
fn default() -> Self {
4452
Self::new("")
4553
}
4654
}
4755

48-
/// Prometheus style process metrics collector
56+
/// Prometheus style process metrics collector for the [metrics] crate.
57+
///
58+
/// This is a collector which will directly export the metrics to the registered [metrics]
59+
/// collector.
60+
#[cfg(feature = "metrics-rs")]
4961
#[derive(Debug, Default, PartialEq, Eq, Clone)]
5062
pub struct Collector {
5163
metrics: Arc<Metrics>,
5264
}
5365

66+
#[cfg(feature = "metrics-rs")]
5467
impl Collector {
5568
/// Add an prefix that is prepended to metric keys.
5669
/// # Examples

0 commit comments

Comments
 (0)