https://github.com/koushiro/fastmetrics
OpenMetrics client library for Rust
https://github.com/koushiro/fastmetrics
metrics openmetrics prometheus rust
Last synced: about 1 year ago
JSON representation
OpenMetrics client library for Rust
- Host: GitHub
- URL: https://github.com/koushiro/fastmetrics
- Owner: koushiro
- License: apache-2.0
- Created: 2024-12-16T07:04:37.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-04-21T14:57:39.000Z (about 1 year ago)
- Last Synced: 2025-05-05T09:11:23.287Z (about 1 year ago)
- Topics: metrics, openmetrics, prometheus, rust
- Language: Rust
- Homepage:
- Size: 232 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastmetrics
[](https://github.com/koushiro/fastmetrics/actions)
[](https://crates.io/crates/fastmetrics)
[](https://docs.rs/fastmetrics)
[](https://www.whatrustisit.com)
[](LICENSE)
A pure-Rust implementation of the [OpenMetrics] specification for transmitting cloud-native metrics at scale,
and it's compatible with Prometheus.
[OpenMetrics]: https://github.com/prometheus/OpenMetrics/blob/main/specification/OpenMetrics.md
## Features
- Full support for [OpenMetrics] specification
- Fast encoding in both text and 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 label handling, stateset value handling, etc.)
## Usage
```rust
use fastmetrics::{
encoder::{EncodeLabelSet, EncodeLabelValue},
format::text,
metrics::{counter::Counter, family::Family},
registry::Registry,
};
// Define label types
// Need to enable `derive` feature to use `#[derive(EncodeLabelSet)]`
#[derive(Clone, Eq, PartialEq, Hash, EncodeLabelSet)]
struct Labels {
method: Method,
status: u32,
}
// Need to enable `derive` feature to use `#[derive(EncodeLabelValue)]`
#[derive(Clone, Eq, PartialEq, Hash, EncodeLabelValue)]
enum Method {
Get,
Put,
}
fn main() -> Box {
// 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 a simple counter
let requests = ::default();
registry.register("requests", "Total requests processed", requests.clone())?;
// Register a counter metric family for tracking requests with labels
let http_requests = Family::::default();
registry.register(
"http_requests",
"Total HTTP requests",
http_requests.clone()
)?;
// Update the simple counter
requests.inc();
assert_eq!(requests.total(), 1);
// Update the counter family
let labels = Labels { method: Method::Get, status: 200 };
http_requests.with_or_new(&labels, |req| req.inc());
assert_eq!(http_requests.with(&labels, |req| req.total()), Some(1));
// Export metrics in text format
let mut output = String::new();
text::encode(&mut output, ®istry)?;
println!("{}", output);
Ok(())
}
```
See [documentation](https://docs.rs/fastmetrics) and [examples](./examples) for more details.
## Performance
Compared with the existing rust client libraries, its text encoding is about 20%~30% faster than the fastest rust library (prometheus-client),
while its Protobuf encoding is on par with the fastest rust library (prometheus).
See [benchmarks](./benchmarks/README.md) 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.
- [prometheus/client_golang](https://github.com/prometheus/client_golang): Official prometheus client library for Golang applications.
- [prometheus/client_rust](https://github.com/prometheus/client_rust): Official prometheus client library for Rust applications.
- [tikv/rust-prometheus](https://github.com/tikv/rust-prometheus): Another prometheus instrumentation library for Rust applications.
## License
This project is licensed under the Apache License, Version 2.0 - see the [LICENSE](LICENSE) file for details.