Skip to content

Commit c690316

Browse files
committed
Move example onto the Exemplar docs
Signed-off-by: Adam Chalmers <[email protected]>
1 parent 7b0007c commit c690316

File tree

3 files changed

+62
-50
lines changed

3 files changed

+62
-50
lines changed

Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,6 @@ actix-web = "4"
3838
[build-dependencies]
3939
prost-build = { version = "0.9.0", optional = true }
4040

41-
[[example]]
42-
name = "families-exemplars"
43-
4441
[[bench]]
4542
name = "family"
4643
harness = false

examples/families-exemplars.rs

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/metrics/exemplar.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,36 @@ pub struct Exemplar<S, V> {
3232
/// counter_with_exemplar.inc_by(1, Some(vec![("user_id".to_string(), "42".to_string())]));
3333
/// let _value: (u64, _) = counter_with_exemplar.get();
3434
/// ```
35+
/// You can also use exemplars with families. Just wrap the exemplar in a Family.
36+
/// ```
37+
/// # use prometheus_client::metrics::exemplar::CounterWithExemplar;
38+
/// # use prometheus_client::metrics::histogram::exponential_buckets;
39+
/// # use prometheus_client::metrics::family::Family;
40+
/// # use prometheus_client_derive_encode::Encode;
41+
/// #[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)]
42+
/// pub struct ResultLabel {
43+
/// pub result: String,
44+
/// }
45+
///
46+
/// #[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)]
47+
/// pub struct TraceLabel {
48+
/// pub trace_id: String,
49+
/// }
50+
///
51+
/// let latency: Family<ResultLabel, CounterWithExemplar<TraceLabel>> =
52+
/// Family::default();
53+
///
54+
/// latency
55+
/// .get_or_create(&ResultLabel {
56+
/// result: "success".to_owned(),
57+
/// })
58+
/// .inc_by(
59+
/// 1,
60+
/// Some(TraceLabel {
61+
/// trace_id: "3a2f90c9f80b894f".to_owned(),
62+
/// }),
63+
/// );
64+
/// ```
3565
#[cfg(not(any(target_arch = "mips", target_arch = "powerpc")))]
3666
#[derive(Debug)]
3767
pub struct CounterWithExemplar<S, N = u64, A = AtomicU64> {
@@ -127,6 +157,38 @@ impl<S, N: Clone, A: counter::Atomic<N>> CounterWithExemplar<S, N, A> {
127157
/// let histogram = HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10));
128158
/// histogram.observe(4.2, Some(vec![("user_id".to_string(), "42".to_string())]));
129159
/// ```
160+
/// You can also use exemplars with families. Just wrap the exemplar in a Family.
161+
/// ```
162+
/// # use prometheus_client::metrics::exemplar::HistogramWithExemplars;
163+
/// # use prometheus_client::metrics::histogram::exponential_buckets;
164+
/// # use prometheus_client::metrics::family::Family;
165+
/// # use prometheus_client_derive_encode::Encode;
166+
/// #[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)]
167+
/// pub struct ResultLabel {
168+
/// pub result: String,
169+
/// }
170+
///
171+
/// #[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)]
172+
/// pub struct TraceLabel {
173+
/// pub trace_id: String,
174+
/// }
175+
///
176+
/// let latency: Family<ResultLabel, HistogramWithExemplars<TraceLabel>> =
177+
/// Family::new_with_constructor(|| {
178+
/// HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10))
179+
/// });
180+
///
181+
/// latency
182+
/// .get_or_create(&ResultLabel {
183+
/// result: "success".to_owned(),
184+
/// })
185+
/// .observe(
186+
/// 0.001345422,
187+
/// Some(TraceLabel {
188+
/// trace_id: "3a2f90c9f80b894f".to_owned(),
189+
/// }),
190+
/// );
191+
/// ```
130192
#[derive(Debug)]
131193
pub struct HistogramWithExemplars<S> {
132194
// TODO: Not ideal, as Histogram has a Mutex as well.

0 commit comments

Comments
 (0)