-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Impl TypedMetric for WithExemplar types, so a metric can use…
… both families and exemplars Fixes issue #95. Signed-off-by: Adam Chalmers <adam.s.chalmers@gmail.com>
- Loading branch information
1 parent
5b3aa2c
commit 9bd6291
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use prometheus_client::{ | ||
encoding::text::encode, | ||
metrics::{exemplar::HistogramWithExemplars, family::Family, histogram::exponential_buckets}, | ||
registry::Registry, | ||
}; | ||
use prometheus_client_derive_encode::Encode; | ||
|
||
fn main() { | ||
// Create a metric registry. | ||
let mut registry = <Registry>::default(); | ||
|
||
// Metric will be used something like this: | ||
// latency_bucket{result="success",le="0.00256"} 27300 # {trace_id="3a2f90c9f80b894f"} 0.001345422 1.6188203823429482e+09 | ||
let latency: Family<ResultLabel, HistogramWithExemplars<TraceLabel>> = | ||
Family::new_with_constructor(|| { | ||
HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10)) | ||
}); | ||
|
||
// Register metrics. | ||
registry.register("latency", "help text goes here", Box::new(latency.clone())); | ||
|
||
latency | ||
.get_or_create(&ResultLabel { | ||
result: "success".to_owned(), | ||
}) | ||
.observe( | ||
0.001345422, | ||
Some(TraceLabel { | ||
trace_id: "3a2f90c9f80b894f".to_owned(), | ||
}), | ||
); | ||
|
||
let mut buf = Vec::new(); | ||
encode(&mut buf, ®istry).unwrap(); | ||
let line = std::str::from_utf8(buf.as_slice()).unwrap().to_string(); | ||
println!("{line}"); | ||
} | ||
|
||
#[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)] | ||
pub struct ResultLabel { | ||
pub result: String, | ||
} | ||
|
||
#[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)] | ||
pub struct TraceLabel { | ||
pub trace_id: String, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters