Crate metrics_process

source ·
Expand description

crates.io docs.rs MIT License Build Test Audit

⏱ metrics-process

This crate provides a Prometheus-style process metrics collector for the metrics crate, supporting Linux, macOS, and Windows. The collector code is manually rewritten in Rust from the official Prometheus client for Go (client_golang).

Supported Metrics

This crate supports the following metrics provided by Prometheus for process metrics.

Metric nameHelp string
process_cpu_seconds_totalTotal user and system CPU time spent in seconds.
process_open_fdsNumber of open file descriptors.
process_max_fdsMaximum number of open file descriptors.
process_virtual_memory_bytesVirtual memory size in bytes.
process_virtual_memory_max_bytesMaximum amount of virtual memory available in bytes.
process_resident_memory_bytesResident memory size in bytes.
process_heap_bytesProcess heap size in bytes.
process_start_time_secondsStart time of the process since the Unix epoch in seconds.
process_threadsNumber of OS threads in the process.

For each platform, it is equivalent to what the official Prometheus client for Go (client_golang) provides.

Metric nameLinuxmacOSWindows
process_cpu_seconds_totalxxx
process_open_fdsxxx
process_max_fdsxxx
process_virtual_memory_bytesxxx
process_virtual_memory_max_bytesxx
process_resident_memory_bytesxxx
process_heap_bytes
process_start_time_secondsxxx
process_threadsxx

Please note that if you only need to compile this crate on non-supported platforms, you can use the dummy feature. Enabling this feature activates a dummy collector, which returns an empty Metrics.

Usage

Use this crate with metrics-exporter-prometheus as an exporter like:

use std::thread;
use std::time::{Duration, Instant};

use metrics_exporter_prometheus::PrometheusBuilder;
use metrics_process::Collector;

let builder = PrometheusBuilder::new();
builder
    .install()
    .expect("failed to install Prometheus recorder");

let collector = Collector::default();
// Call `describe()` method to register help string.
collector.describe();

loop {
    let s = Instant::now();
    // Periodically call `collect()` method to update information.
    collector.collect();
    thread::sleep(Duration::from_millis(750));
}

Or with axum (or any web application framework you like) to collect metrics whenever the /metrics endpoint is invoked like:

use axum::{routing::get, Router};
use metrics_exporter_prometheus::PrometheusBuilder;
use metrics_process::Collector;
use tokio::net::TcpListener;

#[tokio::main]
async fn main() {
    let builder = PrometheusBuilder::new();
    let handle = builder
        .install_recorder()
        .expect("failed to install Prometheus recorder");

    let collector = Collector::default();
    // Call `describe()` method to register help string.
    collector.describe();

    let app = Router::new().route(
        "/metrics",
        get(move || {
            // Collect information just before handling '/metrics'
            collector.collect();
            std::future::ready(handle.render())
        }),
    );
    let listener = TcpListener::bind("127.0.0.1:3000").await.unwrap();
    axum::serve(listener, app).await.unwrap();
}

Features

This crate offers the following features:

Feature NameDescription
dummyEnables a dummy collector that returns an empty Metrics on non-supported platforms.

Difference from metrics-process-promstyle

It appears that metrics-process-promstyle only supports Linux, but this crate (metrics-process) supports Linux, macOS, and Windows. Additionally, this crate supports process_open_fds and process_max_fds in addition to what metrics-process-promstyle supports.

License

The code follows the MIT license written in LICENSE. Contributors need to agree that any modifications sent to this repository follow the license.

Structs

  • Prometheus style process metrics collector