#prometheus #open-metrics #metrics

fastmetrics

OpenMetrics / Prometheus client library in Rust

19 releases (6 breaking)

Uses new Rust 2024

0.7.1 Feb 28, 2026
0.7.0 Feb 26, 2026
0.6.0 Jan 5, 2026
0.5.1 Dec 25, 2025
0.1.1 Apr 16, 2025

#262 in Encoding

Download history 1264/week @ 2025-12-05 2853/week @ 2025-12-12 1886/week @ 2025-12-19 825/week @ 2025-12-26 405/week @ 2026-01-02 323/week @ 2026-01-09 587/week @ 2026-01-16 376/week @ 2026-01-23 361/week @ 2026-01-30 205/week @ 2026-02-06 217/week @ 2026-02-13 1008/week @ 2026-02-20 154/week @ 2026-02-27 178/week @ 2026-03-06 259/week @ 2026-03-13 1022/week @ 2026-03-20

1,796 downloads per month
Used in 3 crates

Apache-2.0

395KB
8K SLoC

FastMetrics

A pure-Rust implementation of the OpenMetrics specification for transmitting cloud-native metrics at scale, and it's compatible with Prometheus.

Features

  • Full support for OpenMetrics specification
  • Fast encoding in both text and (optional) protobuf exposition format
  • Customizable metric types (currently a set of commonly used metric types are provided)
  • Hierarchical metric organization with namespaces and subsystems
  • Support for variable and constant labels
  • Derive macros to simplify code (e.g., like registering metrics, label handling, etc.)

Usage

use fastmetrics::{
    derive::*,
    error::Result,
    format::text::{self, TextProfile},
    metrics::{counter::Counter, family::Family},
    registry::*,
};

// Define label types
// Need to enable `derive` feature to use `#[derive(LabelSet)]`
// or `#[derive(EncodeLabelSet, LabelSetSchema)]`
#[derive(Clone, Eq, PartialEq, Hash, LabelSet)]
struct Labels {
    method: Method,
    status: u16,
}

// Need to enable `derive` feature to use `#[derive(EncodeLabelValue)]`
#[derive(Clone, Eq, PartialEq, Hash, EncodeLabelValue)]
enum Method {
    Get,
    Put,
}

// Need to enable `derive` feature to use `#[derive(Register)]`
#[derive(Default, Register)]
struct Metrics {
    /// Total requests processed
    requests: Counter,
    /// Total HTTP requests
    http_requests: Family<Labels, Counter>,
}

fn main() -> Result<()> {
    // Create a registry with a namespace and some constant labels
    let mut registry = Registry::builder()
        .with_namespace("myapp")
        .with_const_labels([("env", "prod")])
        .build()?;

    // Register metrics
    let metrics = Metrics::default();
    metrics.register(&mut registry)?;

    // Update the simple counter
    metrics.requests.inc();
    assert_eq!(metrics.requests.total(), 1);

    // Update the counter family
    let labels = Labels { method: Method::Get, status: 200 };
    metrics.http_requests.with_or_new(&labels, |req| req.inc());
    assert_eq!(metrics.http_requests.with(&labels, |req| req.total()), Some(1));

    // Export metrics in text format
    let mut output = String::new();
    text::encode(&mut output, &registry, TextProfile::default())?;
    println!("{}", output);

    Ok(())
}

See documentation and examples for more details.

Performance

See benchmarks for more details

Acknowledgment

I drew a lot of inspiration from the following libraries, retaining the designs I thought were good and experimenting with some different ones.

License

This project is licensed under the Apache License, Version 2.0 - see the LICENSE file for details.

Dependencies

~0.5–2.7MB
~39K SLoC