#cross-platform #metrics #prometheus #process #open-metrics

metrics-process

Cross-platform Prometheus style process metrics collector of metrics crate

12 stable releases

new 1.0.11 May 25, 2023
1.0.8 Mar 15, 2023
1.0.4 Nov 28, 2022
0.1.0 Aug 22, 2022

#201 in Debugging

Download history 170/week @ 2023-02-06 258/week @ 2023-02-13 238/week @ 2023-02-20 255/week @ 2023-02-27 162/week @ 2023-03-06 189/week @ 2023-03-13 299/week @ 2023-03-20 261/week @ 2023-03-27 2076/week @ 2023-04-03 2734/week @ 2023-04-10 5522/week @ 2023-04-17 2845/week @ 2023-04-24 3155/week @ 2023-05-01 2252/week @ 2023-05-08 2106/week @ 2023-05-15 2282/week @ 2023-05-22

10,027 downloads per month
Used in kubert

MIT license

30KB
311 lines

crates.io docs.rs MIT License Build Test Audit codecov

⏱ metrics-process

This crate provides Prometheus style process metrics collector of metrics crate for Linux, macOS, and Windows. Collector code is manually re-written to Rust from an official prometheus client of go (client_golang)

Supported metrics

This crate supports the following metrics, equal to what official prometheus client of go (client_golang) provides.

Metric name Help string Linux macOS Windows
process_cpu_seconds_total Total user and system CPU time spent in seconds. x x x
process_open_fds Number of open file descriptors. x x x
process_max_fds Maximum number of open file descriptors. x x x
process_virtual_memory_bytes Virtual memory size in bytes. x x x
process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes. x x
process_resident_memory_bytes Resident memory size in bytes. x x x
process_heap_bytes Process heap size in bytes.
process_start_time_seconds Start time of the process since unix epoch in seconds. x x x
process_threads Number of OS threads in the process. x x

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, Server};
use metrics_exporter_prometheus::PrometheusBuilder;
use metrics_process::Collector;

#[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 addr = "127.0.0.1:9000".parse().unwrap();
    let app = Router::new().route(
        "/metrics",
        get(move || {
            // Collect information just before handle '/metrics'
            collector.collect();
            std::future::ready(handle.render())
        }),
    );
    Server::bind(&addr)
        .serve(app.into_make_service())
        .await
        .unwrap();
}

Difference from metrics-process-promstyle

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

License

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

Dependencies

~0.7–48MB
~809K SLoC