metrics-process 0.1.0

metrics plugin to support prometheus process metrics
Documentation

crates.io docs.rs MIT License Build Test Audit

⏱ metrics-process

This crate provides Prometheus style process metrics collector of metrics crate.

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;

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

    let collector = Collector::new("");
    // 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::new("");
    // 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();
}

License

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