Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow family to use constructors that do not coerce to function pointers #21

Merged
merged 6 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/encoding/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

use crate::metrics::counter::{self, Counter};
use crate::metrics::exemplar::{CounterWithExemplar, Exemplar, HistogramWithExemplars};
use crate::metrics::family::Family;
use crate::metrics::family::{Family, MetricConstructor};
use crate::metrics::gauge::{self, Gauge};
use crate::metrics::histogram::Histogram;
use crate::metrics::info::Info;
Expand Down Expand Up @@ -487,10 +487,11 @@ where
/////////////////////////////////////////////////////////////////////////////////
// Family

impl<S, M> EncodeMetric for Family<S, M>
impl<S, M, C> EncodeMetric for Family<S, M, C>
where
S: Clone + std::hash::Hash + Eq + Encode,
M: EncodeMetric + TypedMetric,
C: MetricConstructor<M>,
{
fn encode(&self, mut encoder: Encoder) -> Result<(), std::io::Error> {
let guard = self.read();
Expand Down
82 changes: 72 additions & 10 deletions src/metrics/family.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ use std::sync::{Arc, RwLock, RwLockReadGuard};
/// # assert_eq!(expected, String::from_utf8(buffer).unwrap());
/// ```
// TODO: Consider exposing hash algorithm.
pub struct Family<S, M> {
pub struct Family<S, M, C = fn() -> M> {
metrics: Arc<RwLock<HashMap<S, M>>>,
/// Function that when called constructs a new metric.
///
Expand All @@ -107,7 +107,53 @@ pub struct Family<S, M> {
/// [`Histogram`](crate::metrics::histogram::Histogram) in order to set
/// specific buckets, a custom constructor is set via
/// [`Family::new_with_constructor`].
constructor: fn() -> M,
constructor: C,
}

/// A constructor for creating new metrics in a [`Family`] when calling
/// [`Family::get_or_create`]. Such constructor is provided via
/// [`Family::new_with_constructor`].
///
/// This is mostly used when creating histograms using constructors that need to
/// capture variables.
///
/// ```
/// # use open_metrics_client::metrics::family::{Family, MetricConstructor};
/// # use open_metrics_client::metrics::histogram::Histogram;
/// struct CustomBuilder {
/// buckets: Vec<f64>,
/// }
///
/// impl MetricConstructor<Histogram> for CustomBuilder {
/// fn new_metric(&self) -> Histogram {
/// // When a new histogram is created, this function will be called.
/// Histogram::new(self.buckets.iter().cloned())
/// }
/// }
///
/// let custom_builder = CustomBuilder { buckets: vec![0.0, 10.0, 100.0] };
/// let metric = Family::<(), Histogram, CustomBuilder>::new_with_constructor(custom_builder);
/// ```
pub trait MetricConstructor<M> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding some documentation pointing to Family::new_with_constructor and a doc example to this type?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added docs and an example to the trait, and also to the Fn() -> M implementor for completeness. Thanks for the suggestion

fn new_metric(&self) -> M;
}

/// In cases in which the explicit type of the metric is not required, it is
/// posible to directly provide a closure even if it captures variables.
///
/// ```
/// # use open_metrics_client::metrics::family::{Family};
/// # use open_metrics_client::metrics::histogram::Histogram;
/// let custom_buckets = vec![0.0, 10.0, 100.0];
/// let metric = Family::<(), Histogram, _>::new_with_constructor(|| {
/// Histogram::new(custom_buckets.clone().into_iter())
/// });
/// # metric.get_or_create(&());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting here because I think you'll wonder about this line. This helps test that the constructor above is a Fn() -> M and not a FnOnce and thus, that this is a valid example for the implementor

/// ```
impl<M, F: Fn() -> M> MetricConstructor<M> for F {
fn new_metric(&self) -> M {
self()
}
}

impl<S: Clone + std::hash::Hash + Eq, M: Default> Default for Family<S, M> {
Expand All @@ -119,7 +165,7 @@ impl<S: Clone + std::hash::Hash + Eq, M: Default> Default for Family<S, M> {
}
}

impl<S: Clone + std::hash::Hash + Eq, M> Family<S, M> {
impl<S: Clone + std::hash::Hash + Eq, M, C> Family<S, M, C> {
/// Create a metric family using a custom constructor to construct new
/// metrics.
///
Expand All @@ -131,7 +177,8 @@ impl<S: Clone + std::hash::Hash + Eq, M> Family<S, M> {
/// [`Histogram`](crate::metrics::histogram::Histogram) one might want
/// [`Family`] to construct a
/// [`Histogram`](crate::metrics::histogram::Histogram) with custom buckets
/// (see example below). For such case one can use this method.
/// (see example below). For such case one can use this method. For more
/// involved constructors see [`MetricConstructor`].
///
/// ```
/// # use open_metrics_client::metrics::family::Family;
Expand All @@ -140,15 +187,15 @@ impl<S: Clone + std::hash::Hash + Eq, M> Family<S, M> {
/// Histogram::new(exponential_buckets(1.0, 2.0, 10))
/// });
/// ```
pub fn new_with_constructor(constructor: fn() -> M) -> Self {
pub fn new_with_constructor(constructor: C) -> Self {
Self {
metrics: Arc::new(RwLock::new(Default::default())),
constructor,
}
}
}

impl<S: Clone + std::hash::Hash + Eq, M> Family<S, M> {
impl<S: Clone + std::hash::Hash + Eq, M, C: MetricConstructor<M>> Family<S, M, C> {
/// Access a metric with the given label set, creating it if one does not
/// yet exist.
///
Expand All @@ -175,7 +222,7 @@ impl<S: Clone + std::hash::Hash + Eq, M> Family<S, M> {
}

let mut write_guard = self.metrics.write().unwrap();
write_guard.insert(label_set.clone(), (self.constructor)());
write_guard.insert(label_set.clone(), self.constructor.new_metric());

drop(write_guard);

Expand All @@ -192,16 +239,16 @@ impl<S: Clone + std::hash::Hash + Eq, M> Family<S, M> {
}
}

impl<S, M> Clone for Family<S, M> {
impl<S, M, C: Clone> Clone for Family<S, M, C> {
fn clone(&self) -> Self {
Family {
metrics: self.metrics.clone(),
constructor: self.constructor,
constructor: self.constructor.clone(),
}
}
}

impl<S, M: TypedMetric> TypedMetric for Family<S, M> {
impl<S, M: TypedMetric, C> TypedMetric for Family<S, M, C> {
const TYPE: MetricType = <M as TypedMetric>::TYPE;
}

Expand Down Expand Up @@ -233,4 +280,19 @@ mod tests {
Histogram::new(exponential_buckets(1.0, 2.0, 10))
});
}

#[test]
fn histogram_family_with_struct_constructor() {
struct CustomBuilder {
custom_start: f64,
}
impl MetricConstructor<Histogram> for CustomBuilder {
fn new_metric(&self) -> Histogram {
Histogram::new(exponential_buckets(self.custom_start, 2.0, 10))
}
}

let custom_builder = CustomBuilder { custom_start: 1.0 };
Family::<(), Histogram, CustomBuilder>::new_with_constructor(custom_builder);
}
}