Skip to main content

Crate metrics_lib

Crate metrics_lib 

Source
Expand description

§Ultimate Metrics Library

The most powerful, lightweight, and efficient metrics library ever built.

§Features

  • Sub-nanosecond operations - Counter increments in ~2-3ns
  • Lock-free everything - No locks anywhere in hot paths
  • System health monitoring - Built-in CPU/memory tracking
  • Dynamic configuration - Runtime tuning without restarts
  • Circuit breakers - Fault tolerance with auto-recovery
  • Dead simple API - METRICS.counter("requests").inc()

§Quick Start

use metrics_lib::{init, metrics};

// Initialize metrics (do this once at startup)
init();

// Counters (sub-nanosecond)
#[cfg(feature = "count")]
{
metrics().counter("requests").inc();
metrics().counter("errors").add(5);
}

// Gauges (atomic)  
#[cfg(feature = "gauge")]
{
metrics().gauge("cpu_usage").set(87.3);
metrics().gauge("memory_mb").set(1024.5);
}

// Timers (high precision)
#[cfg(feature = "timer")]
{
let timer_metric = metrics().timer("api_call");
let timer = timer_metric.start();
// ... do work ...
timer.stop(); // Auto-records

// Or even simpler
let result = metrics().time("db_query", || {
    // Simulated database query
    "user data"
});
let _ = result;
}

// System health
let cpu_pct = metrics().system().cpu_used();
let mem_mb = metrics().system().mem_used_mb();
let _ = (cpu_pct, mem_mb);

// Rate limiting
#[cfg(feature = "meter")]
{
metrics().rate("api_calls").tick();
let rate_per_sec = metrics().rate("api_calls").rate();
let _ = rate_per_sec;
}

Modules§

gauge_specialized
Specialized gauge types for common use cases
prelude
Prelude for convenient glob imports.
utils
Utility functions for timing

Macros§

time_block
Convenience macros
time_fn
Macro to time a function call and record the result

Structs§

Counter
Ultra-fast atomic counter
CounterStats
Counter statistics
Gauge
Ultra-fast atomic gauge for f64 values
GaugeStats
Gauge statistics
MetricsCore
Main metrics interface - the core of everything
ProcessStats
Process-specific resource usage
Registry
A thread-safe registry for storing metrics by name.
RunningTimer
Running timer instance (RAII)
SystemHealth
System health monitor with process introspection
SystemSnapshot
System resource usage snapshot
Timer
High-precision timer with automatic statistics
TimerStats
Timer statistics

Enums§

HealthStatus
System health status
MetricsError
Metrics errors

Statics§

METRICS
Global metrics instance - initialize once, use everywhere

Functions§

init
Initialize the global metrics instance
metrics
Get the global metrics instance

Type Aliases§

Result
Common result type for metrics operations