Skip to content

Commit

Permalink
feature: Impl TypedMetric for WithExemplar types, so a metric can use…
Browse files Browse the repository at this point in the history
… both families and exemplars

Fixes issue #95.

Signed-off-by: Adam Chalmers <adam.s.chalmers@gmail.com>
  • Loading branch information
adamchalmers committed Sep 19, 2022
1 parent 5b3aa2c commit 9bd6291
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@ 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

- Move`Encode` trait from `prometheus_client::encoding::text` to `prometheus_client::encoding`. See [PR 83].

[PR 83]: https://github.com/prometheus/client_rust/pull/83
[PR 85]: https://github.com/prometheus/client_rust/pull/85
[PR 96]: https://github.com/prometheus/client_rust/pull/96

## [0.18.0]

Expand Down
47 changes: 47 additions & 0 deletions examples/families-exemplars.rs
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, &registry).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,
}
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 9bd6291

Please sign in to comment.