Skip to content

Commit

Permalink
Bugfix: Impl TypedMetric for Exemplar types
Browse files Browse the repository at this point in the history
This allows my families-exemplars example to compile. The `latency` metric in my example can have a family label and an exemplar label:

```
latency_sum{result="success"} 0.001345422
latency_count{result="success"} 1
latency_bucket{result="success",le="1.0"} 1 # {trace_id="3a2f90c9f80b894f"} 0.001345422
```
  • Loading branch information
Adam Chalmers committed Sep 19, 2022
1 parent 3712300 commit a4dcecc
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
set from a family. See [PR 85].
- Added a `clear` method to `Family` to allow the removal of all label sets
from a family. See [PR 85].
- Impl `TypedMetric` for `CounterWithExemplar` and `HistogramWithExemplar`, so that they can be used with `Family`. See [PR 96].

### Changed

Expand Down
27 changes: 19 additions & 8 deletions examples/families-exemplars.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use prometheus_client::{
encoding::text::SendSyncEncodeMetric,
encoding::text::encode,
metrics::{exemplar::HistogramWithExemplars, family::Family, histogram::exponential_buckets},
registry::Registry,
};
Expand All @@ -16,13 +16,24 @@ fn main() {
HistogramWithExemplars::new(exponential_buckets(1.0, 2.0, 10))
});

// This line doesn't compile:
//
// the trait `TypedMetric` is not implemented for `HistogramWithExemplar<TraceLabel>`
let b: Box<dyn SendSyncEncodeMetric> = Box::new(latency);

// Register metrics.
registry.register("latency", "help text goes here", b);
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, &registry).unwrap();
let line = std::str::from_utf8(buf.as_slice()).unwrap().to_string();
println!("{line}");
}

#[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)]
Expand All @@ -32,5 +43,5 @@ pub struct ResultLabel {

#[derive(Clone, Hash, PartialEq, Eq, Encode, Debug, Default)]
pub struct TraceLabel {
trace_id: String,
pub trace_id: String,
}
9 changes: 9 additions & 0 deletions src/metrics/exemplar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use super::counter::{self, Counter};
use super::histogram::Histogram;
use super::{MetricType, TypedMetric};
use parking_lot::{MappedRwLockReadGuard, RwLock, RwLockReadGuard};
use std::collections::HashMap;
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
Expand Down Expand Up @@ -37,6 +38,10 @@ pub struct CounterWithExemplar<S, N = u64, A = AtomicU64> {
pub(crate) inner: Arc<RwLock<CounterWithExemplarInner<S, N, A>>>,
}

impl<S> TypedMetric for CounterWithExemplar<S> {
const TYPE: MetricType = MetricType::Counter;
}

/// Open Metrics [`Counter`] with an [`Exemplar`] to both measure discrete
/// events and track references to data outside of the metric set.
#[cfg(any(target_arch = "mips", target_arch = "powerpc"))]
Expand Down Expand Up @@ -128,6 +133,10 @@ pub struct HistogramWithExemplars<S> {
pub(crate) inner: Arc<RwLock<HistogramWithExemplarsInner<S>>>,
}

impl<S> TypedMetric for HistogramWithExemplars<S> {
const TYPE: MetricType = MetricType::Histogram;
}

impl<S> Clone for HistogramWithExemplars<S> {
fn clone(&self) -> Self {
Self {
Expand Down

0 comments on commit a4dcecc

Please sign in to comment.