textfile-metrics 0.1.0

Non-blocking Prometheus textfile metrics writer with Counter and Gauge helpers
Documentation

Non-blocking Prometheus textfile metrics writer.

This crate provides a production-ready metrics system that writes to Prometheus textfile format with a non-blocking, thread-safe design. Metrics are snapshot under lock, then written to disk after releasing the lock.

Features

  • Non-blocking I/O pattern: Takes snapshot under lock, drops lock, then writes
  • Thread-safe: Uses DashMap for lock-free metric updates
  • Lazy configuration: Reads METRICS_TEXTFILE_PATH env var (default: /var/lib/node_exporter/textfile_collector/)
  • Prometheus format: Writes valid .prom files
  • Type-safe helpers: Counter and Gauge abstractions with label support

Quick Start

use textfile_metrics::MetricsWriter;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let metrics = MetricsWriter::new()?;

    // Increment a counter with labels
    metrics.counter("requests_total", vec![
        ("method".to_string(), "GET".to_string()),
        ("status".to_string(), "200".to_string()),
    ], 1.0)?;

    // Set a gauge value
    metrics.gauge("temperature_celsius", vec![
        ("location".to_string(), "warehouse_a".to_string()),
    ], 23.5)?;

    // Flush metrics to disk
    metrics.flush().await?;

    Ok(())
}

Environment Variables

  • METRICS_TEXTFILE_PATH: Directory to write metrics files (default: /var/lib/node_exporter/textfile_collector/)
  • METRICS_DEBUG: Enable debug logging (optional)