Utility macro to build static metrics for the rust-prometheus library.
MetricVec
(i.e. CounterVec
, GaugeVec
or HistogramVec
) is slow. However if every possible values for labels are
known, each metric in the MetricVec
can be cached to avoid the runtime cost.
For example, the following code can be slow when it is invoked multiple times:
rust
some_counter_vec.with_label_values(&["label_1_foo", "label_2_bar"]).inc();
It is because we are retriving a specific Counter
according to values each time and to ensure thread-safety there is
a lock inside which makes things worse.
We can optimize it by caching the counter by label values:
```rust // init before hand let foobarcounter = somecounter.withlabelvalues(&["label1foo", "label2_bar"]);
foobarcounter.inc(); ```
So far everything seems good. We achieve the same performance as Counter
for CounterVec
. But what if there are many
labels and each of them has many values? We need to hand-craft a lot of code in this way.
That's what this crate solves. This crate provides a macro that helps you do the optimization above without really introducing a lot of templating code.
Add to Cargo.toml
:
toml
[dependencies]
prometheus-static-metric = "0.4"
Add to lib.rs
:
rust
extern crate prometheus_static_metric;
Use the make_static_metric!
to define all possible values for each label. Your definition will be expanded to a real
struct
for easy access while keeping high-performance.
```rust use prometheusstaticmetric::makestaticmetric;
makestaticmetric! { pub struct MyStaticCounterVec: Counter { "method" => { post, get, put, delete, }, "product" => { foo, bar, }, } }
fn main() { let vec = CounterVec::new(Opts::new("foo", "bar"), &["method", "product"]).unwrap(); let staticcountervec = MyStaticCounterVec::from(&vec);
static_counter_vec.post.foo.inc();
static_counter_vec.delete.bar.inc_by(4.0);
assert_eq!(static_counter_vec.post.bar.get(), 0.0);
assert_eq!(vec.with_label_values(&["post", "foo"]).get(), 1.0);
assert_eq!(vec.with_label_values(&["delete", "bar"]).get(), 4.0);
} ```
For heavier scenario that a global shared static-metric might not be effecient enough, you can use make_auto_flush_static_metric!
macro, which will store data in local thread storage, with a custom rate to flush to global MetricVec
.
```rust
extern crate lazystatic; extern crate prometheus; extern crate prometheusstatic_metric;
use prometheus::*; use prometheusstaticmetric::autoflushfrom; use prometheusstaticmetric::makeautoflushstaticmetric;
makeautoflushstaticmetric! {
pub label_enum FooBar {
foo,
bar,
}
pub label_enum Methods {
post,
get,
put,
delete,
}
pub struct Lhrs: LocalIntCounter {
"product" => FooBar,
"method" => Methods,
"version" => {
http1: "HTTP/1",
http2: "HTTP/2",
},
}
}
lazystatic! { pub static ref HTTPCOUNTERVEC: IntCounterVec = registerintcountervec ! ( "httprequeststotal", "Number of HTTP requests.", & ["product", "method", "version"] // it doesn't matter for the label order ).unwrap(); }
lazystatic! { // You can also use default flush duration which is 1 second. // pub static ref TLSHTTPCOUNTER: Lhrs = autoflushfrom!(HTTPCOUNTERVEC, Lhrs); pub static ref TLSHTTPCOUNTER: Lhrs = autoflushfrom!(HTTPCOUNTERVEC, Lhrs, std::time::Duration::fromsecs(1)); }
fn main() { TLSHTTPCOUNTER.foo.post.http1.inc(); TLSHTTPCOUNTER.foo.post.http1.inc();
assert_eq!(
HTTP_COUNTER_VEC
.with_label_values(&["foo", "post", "HTTP/1"])
.get(),
0
);
::std::thread::sleep(::std::time::Duration::from_secs(2));
TLS_HTTP_COUNTER.foo.post.http1.inc();
assert_eq!(
HTTP_COUNTER_VEC
.with_label_values(&["foo", "post", "HTTP/1"])
.get(),
3
);
} ```
Please take a look at examples directory for more.