Performance First
World-class performance with industry-leading benchmarks:
- Counter: 17.26ns/op (57.93M ops/sec) - 5x faster than competitors
- Gauge: 0.23ns/op (4303.60M ops/sec) - 30x faster than competitors
- Timer: 45.66ns/op (21.90M ops/sec) - 10x faster than competitors
- Memory: 64 bytes per metric (cache-aligned, 4x smaller footprint)
Features
Core Metrics
- 🔢 Counters - Atomic increment/decrement with overflow protection
- 📊 Gauges - IEEE 754 atomic floating-point with mathematical operations
- ⏱️ Timers - Nanosecond precision with RAII guards and batch recording
- 📈 Rate Meters - Sliding window rates with burst detection and API limiting
- 💾 System Health - Built-in CPU, memory, and process monitoring
Advanced Features
- Lock-Free - Zero locks in hot paths, pure atomic operations
- Async Native - First-class async/await support with zero-cost abstractions
- Resilience - Circuit breakers, adaptive sampling, and backpressure control
- Cross-Platform - Linux, macOS, Windows with optimized system integrations
- Cache-Aligned - 64-byte alignment prevents false sharing
API Overview
For a complete reference with examples, see docs/API.md
.
Counter
— ultra-fast atomic counters with batch and conditional opsGauge
— atomic f64 gauges with math ops, EMA, and min/max helpersTimer
— nanosecond timers, RAII guards, and closure/async timingRateMeter
— sliding-window rate tracking and burstsSystemHealth
— CPU, memory, load, threads, FDs, health score- Async support —
AsyncTimerExt
,AsyncMetricBatch
- Adaptive controls — sampling, circuit breaker, backpressure
- Prelude — convenient re-exports
Installation
Add to your Cargo.toml
:
[]
= "0.8.0"
# Optional features
= { = "0.8.0", = ["async"] }
Quick Start
use ;
// Initialize once at startup
init;
// Counters - fastest operations (18ns)
metrics.counter.inc;
metrics.counter.add;
// Gauges - sub-nanosecond operations (0.6ns)
metrics.gauge.set;
metrics.gauge.add;
// Timers - automatic RAII timing
// Or time a closure
let result = metrics.time;
// System health monitoring
let cpu = metrics.system.cpu_used;
let memory_gb = metrics.system.mem_used_gb;
// Rate metering
metrics.rate.tick;
Advanced Usage
Async Support
use Duration;
use ;
// Async timing with zero overhead and typed result
let result: &str = metrics
.timer
.time_async
.await;
// Batched async updates (flush takes &MetricsCore)
let mut batch = new;
batch.counter_inc;
batch.gauge_set;
batch.flush;
Resilience Features
use ;
// Adaptive sampling under load
let sampler = new;
if sampler.should_sample
// Circuit breaker protection
let breaker = new;
if breaker.is_allowed else
System Monitoring
let health = metrics.system;
println!;
println!;
println!;
println!;
Benchmarks
Run the included benchmarks to see performance on your system:
# Basic performance comparison
# Comprehensive benchmarks
# Cross-platform system tests
Sample Results (M1 MacBook Pro):
Counter Increment: 17.26 ns/op (57.93 M ops/sec)
Gauge Set: 0.23 ns/op (4303.60 M ops/sec)
Timer Record: 45.66 ns/op (21.90 M ops/sec)
Mixed Operations: 106.39 ns/op (9.40 M ops/sec)
Architecture
Lock-Free Design
- Atomic Operations: All metrics use
Relaxed
ordering for maximum performance - Cache-Line Alignment: 64-byte alignment eliminates false sharing
- Compare-and-Swap: Lock-free min/max tracking in timers
- Thread-Local Storage: Fast random number generation for sampling
Memory Layout
Zero-Cost Abstractions
- RAII Timers: Compile-time guaranteed cleanup
- Async Guards: No allocation futures for timing
- Batch Operations: Vectorized updates for efficiency
Testing
Comprehensive test suite with 87 unit tests and 2 documentation tests:
# Run all tests
# Test with all features
# Run only bench-gated tests (feature-flagged and ignored by default)
# Run benchmarks (Criterion)
# Check for memory leaks (with valgrind)
Cross-Platform Support
Tier 1 Support:
- ✅ Linux (x86_64, aarch64)
- ✅ macOS (x86_64, Apple Silicon)
- ✅ Windows (x86_64)
System Integration:
- Linux:
/proc
filesystem,sysinfo
APIs - macOS:
mach
system calls,sysctl
APIs - Windows: Performance counters, WMI integration
Graceful Fallbacks:
- Unsupported platforms default to portable implementations
- Feature detection at runtime
- No panics on missing system features
Comparison
Library | Counter ns/op | Gauge ns/op | Timer ns/op | Memory/Metric | Features |
---|---|---|---|---|---|
metrics-lib | 17.26 | 0.23 | 45.66 | 64B | ✅ Async, Circuit breakers, System monitoring |
metrics-rs | 85.2 | 23.1 | 167.8 | 256B | ⚠️ No circuit breakers |
prometheus | 156.7 | 89.4 | 298.3 | 1024B+ | ⚠️ HTTP overhead |
statsd | 234.1 | 178.9 | 445.2 | 512B+ | ⚠️ Network overhead |
Configuration
Feature Flags
[]
= { = "0.8.0", = [
"async", # Async/await support (requires tokio)
"histogram", # Advanced histogram support
"all" # Enable all features
]}
Runtime Configuration
use ;
let config = Config ;
init_with_config;
Contributing
We welcome contributions! Please see our Contributing Guide.
Development Setup
# Clone repository
# Run tests
# Run benchmarks
# Check formatting and lints
License
This project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Links
- 📚 Documentation
- 📦 Crates.io
- 🐛 Issues
- 💬 Discussions