metrics_process/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
#![doc = include_str!("../README.md")]
mod collector;
use std::sync::Arc;
use metrics::{describe_gauge, gauge, Unit};
#[cfg(not(feature = "use-gauge-on-cpu-seconds-total"))]
use metrics::{counter, describe_counter};
/// Metrics names
#[derive(Debug, PartialEq, Eq)]
struct Metrics {
cpu_seconds_total: Arc<str>,
open_fds: Arc<str>,
max_fds: Arc<str>,
virtual_memory_bytes: Arc<str>,
virtual_memory_max_bytes: Arc<str>,
resident_memory_bytes: Arc<str>,
start_time_seconds: Arc<str>,
threads: Arc<str>,
}
impl Metrics {
// Create new Metrics, allocating prefixed strings for metrics names.
fn new(prefix: impl AsRef<str>) -> Self {
let prefix = prefix.as_ref();
Self {
cpu_seconds_total: format!("{prefix}process_cpu_seconds_total").into(),
open_fds: format!("{prefix}process_open_fds").into(),
max_fds: format!("{prefix}process_max_fds").into(),
virtual_memory_bytes: format!("{prefix}process_virtual_memory_bytes").into(),
virtual_memory_max_bytes: format!("{prefix}process_virtual_memory_max_bytes").into(),
resident_memory_bytes: format!("{prefix}process_resident_memory_bytes").into(),
start_time_seconds: format!("{prefix}process_start_time_seconds").into(),
threads: format!("{prefix}process_threads").into(),
}
}
}
impl Default for Metrics {
// Create new Metrics, without prefixing and thus allocating.
fn default() -> Self {
Self::new("")
}
}
/// Prometheus style process metrics collector
#[derive(Debug, Default, PartialEq, Eq, Clone)]
pub struct Collector {
metrics: Arc<Metrics>,
}
impl Collector {
/// Add an prefix that is prepended to metric keys.
/// # Examples
///
/// ```
/// # use metrics_process::Collector;
/// let collector = Collector::default().prefix("my_prefix_");
/// ```
///
/// # Deprecated
///
/// The new interface for creating a Collector should be utilized.
///
/// ```
/// # use metrics_process::Collector;
/// let collector = Collector::new("my_prefix_");
/// ```
#[deprecated(since = "1.1.0", note = "Use `Collector::new(prefix)`.")]
pub fn prefix(self, prefix: impl Into<String>) -> Self {
let _ = self;
Self::new(prefix.into())
}
/// Create a new Collector instance with the provided prefix that is
/// prepended to metric keys.
///
/// # Examples
///
/// ```
/// # use metrics_process::Collector;
/// let collector = Collector::default();
/// ```
pub fn new(prefix: impl AsRef<str>) -> Self {
Self {
metrics: Arc::new(Metrics::new(prefix)),
}
}
/// Describe available metrics through `describe_counter!` and `describe_gauge!` macro of `metrics` crate.
///
/// # Example
///
/// ```no_run
/// # use metrics_exporter_prometheus::PrometheusBuilder;
/// # use metrics_process::Collector;
/// # #[tokio::main]
/// # async fn main() {
/// // Recorder must be initialized prior to describe.
/// let builder = PrometheusBuilder::new();
/// builder.install().expect("failed to install recorder/exporter");
///
/// let collector = Collector::default();
/// // Describe collector
/// collector.describe();
/// # }
/// ```
pub fn describe(&self) {
let metrics = self.metrics.as_ref();
#[cfg(not(feature = "use-gauge-on-cpu-seconds-total"))]
describe_counter!(
Arc::clone(&metrics.cpu_seconds_total),
Unit::Seconds,
"Total user and system CPU time spent in seconds."
);
#[cfg(feature = "use-gauge-on-cpu-seconds-total")]
describe_gauge!(
Arc::clone(&metrics.cpu_seconds_total),
Unit::Seconds,
"Total user and system CPU time spent in seconds."
);
describe_gauge!(
Arc::clone(&metrics.open_fds),
Unit::Count,
"Number of open file descriptors."
);
describe_gauge!(
Arc::clone(&metrics.max_fds),
Unit::Count,
"Maximum number of open file descriptors."
);
describe_gauge!(
Arc::clone(&metrics.virtual_memory_bytes),
Unit::Bytes,
"Virtual memory size in bytes."
);
#[cfg(not(target_os = "windows"))]
describe_gauge!(
Arc::clone(&metrics.virtual_memory_max_bytes),
Unit::Bytes,
"Maximum amount of virtual memory available in bytes."
);
describe_gauge!(
Arc::clone(&metrics.resident_memory_bytes),
Unit::Bytes,
"Resident memory size in bytes."
);
describe_gauge!(
Arc::clone(&metrics.start_time_seconds),
Unit::Seconds,
"Start time of the process since unix epoch in seconds."
);
#[cfg(not(target_os = "windows"))]
describe_gauge!(
Arc::clone(&metrics.threads),
Unit::Count,
"Number of OS threads in the process."
);
}
/// Collect metrics and record through `counter!` and `gauge!` macro of `metrics` crate.
///
/// # Example
///
/// ```no_run
/// # use metrics_exporter_prometheus::PrometheusBuilder;
/// # use metrics_process::Collector;
/// # #[tokio::main]
/// # async fn main() {
/// // Recorder must be initialized prior to describe.
/// let builder = PrometheusBuilder::new();
/// builder.install().expect("failed to install recorder/exporter");
///
/// let collector = Collector::default();
/// collector.describe();
/// // Collect metrics
/// collector.collect();
/// # }
/// ```
pub fn collect(&self) {
let metrics = self.metrics.as_ref();
let mut m = collector::collect();
if let Some(v) = m.cpu_seconds_total.take() {
#[cfg(not(feature = "use-gauge-on-cpu-seconds-total"))]
counter!(Arc::clone(&metrics.cpu_seconds_total)).absolute(v as u64);
#[cfg(feature = "use-gauge-on-cpu-seconds-total")]
gauge!(Arc::clone(&metrics.cpu_seconds_total)).set(v);
}
if let Some(v) = m.open_fds.take() {
gauge!(Arc::clone(&metrics.open_fds)).set(v as f64);
}
if let Some(v) = m.max_fds.take() {
gauge!(Arc::clone(&metrics.max_fds)).set(v as f64);
}
if let Some(v) = m.virtual_memory_bytes.take() {
gauge!(Arc::clone(&metrics.virtual_memory_bytes)).set(v as f64);
}
#[cfg(not(target_os = "windows"))]
if let Some(v) = m.virtual_memory_max_bytes.take() {
gauge!(Arc::clone(&metrics.virtual_memory_max_bytes)).set(v as f64);
}
if let Some(v) = m.resident_memory_bytes.take() {
gauge!(Arc::clone(&metrics.resident_memory_bytes)).set(v as f64);
}
if let Some(v) = m.start_time_seconds.take() {
gauge!(Arc::clone(&metrics.start_time_seconds)).set(v as f64);
}
#[cfg(not(target_os = "windows"))]
if let Some(v) = m.threads.take() {
gauge!(Arc::clone(&metrics.threads)).set(v as f64);
}
}
}