diff --git a/src/encoding/text.rs b/src/encoding/text.rs index c492fd52..0d605038 100644 --- a/src/encoding/text.rs +++ b/src/encoding/text.rs @@ -40,6 +40,8 @@ use std::ops::Deref; pub use prometheus_client_derive_text_encode::*; +/// Encode the metrics registered with the provided [`Registry`] into the +/// provided [`Write`]r using the OpenMetrics text format. pub fn encode(writer: &mut W, registry: &Registry) -> Result<(), std::io::Error> where W: Write, @@ -92,7 +94,9 @@ where Ok(()) } +/// OpenMetrics text encoding for a value. pub trait Encode { + /// Encode to OpenMetrics text encoding. fn encode(&self, writer: &mut dyn Write) -> Result<(), std::io::Error>; } @@ -304,6 +308,7 @@ impl<'a, 'b> Encoder<'a, 'b> { } } +/// Used to encode an OpenMetrics Histogram bucket. #[allow(missing_debug_implementations)] #[must_use] pub struct BucketEncoder<'a> { @@ -344,6 +349,7 @@ impl<'a> BucketEncoder<'a> { } } +/// Used to encode an OpenMetrics metric value. #[allow(missing_debug_implementations)] #[must_use] pub struct ValueEncoder<'a> { @@ -362,6 +368,7 @@ impl<'a> ValueEncoder<'a> { } } +/// Used to encode an OpenMetrics Exemplar. #[allow(missing_debug_implementations)] #[must_use] pub struct ExemplarEncoder<'a> { @@ -389,10 +396,12 @@ impl<'a> ExemplarEncoder<'a> { } } -/// Trait implemented by each metric type, e.g. [`Counter`], to implement its encoding. +/// Trait implemented by each metric type, e.g. [`Counter`], to implement its encoding in the OpenMetric text format. pub trait EncodeMetric { + /// Encode the given instance in the OpenMetrics text encoding. fn encode(&self, encoder: Encoder) -> Result<(), std::io::Error>; + /// The OpenMetrics metric type of the instance. // One can not use [`TypedMetric`] directly, as associated constants are not // object safe and thus can not be used with dynamic dispatching. fn metric_type(&self) -> MetricType; @@ -408,6 +417,7 @@ impl EncodeMetric for Box { } } +/// Trait combining [`EncodeMetric`], [`Send`] and [`Sync`]. pub trait SendSyncEncodeMetric: EncodeMetric + Send + Sync {} impl SendSyncEncodeMetric for T {} diff --git a/src/lib.rs b/src/lib.rs index 058832f2..080d7446 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,7 @@ -#![forbid(unsafe_code)] -#![deny(unused)] #![deny(dead_code)] +#![deny(missing_docs)] +#![deny(unused)] +#![forbid(unsafe_code)] #![warn(missing_debug_implementations)] //! Client library implementation of the [Open Metrics diff --git a/src/metrics.rs b/src/metrics.rs index e09504e3..647fa5c7 100644 --- a/src/metrics.rs +++ b/src/metrics.rs @@ -9,10 +9,13 @@ pub mod info; /// A metric that is aware of its Open Metrics metric type. pub trait TypedMetric { + /// The OpenMetrics metric type. const TYPE: MetricType = MetricType::Unknown; } +/// OpenMetrics metric type. #[derive(Clone, Copy, Debug)] +#[allow(missing_docs)] pub enum MetricType { Counter, Gauge, diff --git a/src/metrics/counter.rs b/src/metrics/counter.rs index 689723d8..bd886c5e 100644 --- a/src/metrics/counter.rs +++ b/src/metrics/counter.rs @@ -45,6 +45,7 @@ pub struct Counter { phantom: PhantomData, } +/// Open Metrics [`Counter`] to measure discrete events. #[cfg(any(target_arch = "mips", target_arch = "powerpc"))] #[derive(Debug)] pub struct Counter { @@ -99,11 +100,15 @@ impl> Counter { } } +/// Atomic operations for a [`Counter`] value store. pub trait Atomic { + /// Increase the value by `1`. fn inc(&self) -> N; + /// Increase the value. fn inc_by(&self, v: N) -> N; + /// Get the the value. fn get(&self) -> N; } diff --git a/src/metrics/exemplar.rs b/src/metrics/exemplar.rs index b08fdc39..84462003 100644 --- a/src/metrics/exemplar.rs +++ b/src/metrics/exemplar.rs @@ -12,6 +12,7 @@ use std::sync::atomic::AtomicU32; use std::sync::atomic::AtomicU64; use std::sync::Arc; +/// An OpenMetrics exemplar. #[derive(Debug)] pub struct Exemplar { pub(crate) label_set: S, @@ -36,6 +37,8 @@ pub struct CounterWithExemplar { pub(crate) inner: Arc>>, } +/// 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"))] #[derive(Debug)] pub struct CounterWithExemplar { @@ -50,6 +53,7 @@ impl Clone for CounterWithExemplar { } } +/// An OpenMetrics [`Counter`] in combination with an OpenMetrics [`Exemplar`]. #[derive(Debug)] pub struct CounterWithExemplarInner { pub(crate) exemplar: Option>, @@ -132,6 +136,7 @@ impl Clone for HistogramWithExemplars { } } +/// An OpenMetrics [`Histogram`] in combination with an OpenMetrics [`Exemplar`]. #[derive(Debug)] pub struct HistogramWithExemplarsInner { pub(crate) exemplars: HashMap>, @@ -139,6 +144,7 @@ pub struct HistogramWithExemplarsInner { } impl HistogramWithExemplars { + /// Create a new [`HistogramWithExemplars`]. pub fn new(buckets: impl Iterator) -> Self { Self { inner: Arc::new(RwLock::new(HistogramWithExemplarsInner { @@ -148,6 +154,8 @@ impl HistogramWithExemplars { } } + /// Observe the given value, optionally providing a label set and thus + /// setting the [`Exemplar`] value. pub fn observe(&self, v: f64, label_set: Option) { let mut inner = self.inner.write(); let bucket = inner.histogram.observe_and_bucket(v); diff --git a/src/metrics/family.rs b/src/metrics/family.rs index fb5dbeb6..b5f54a62 100644 --- a/src/metrics/family.rs +++ b/src/metrics/family.rs @@ -136,6 +136,7 @@ pub struct Family M> { /// let metric = Family::<(), Histogram, CustomBuilder>::new_with_constructor(custom_builder); /// ``` pub trait MetricConstructor { + /// Create a new instance of the metric type. fn new_metric(&self) -> M; } diff --git a/src/metrics/gauge.rs b/src/metrics/gauge.rs index a8e41140..ad49d81a 100644 --- a/src/metrics/gauge.rs +++ b/src/metrics/gauge.rs @@ -45,6 +45,7 @@ pub struct Gauge { phantom: PhantomData, } +/// Open Metrics [`Gauge`] to record current measurements. #[cfg(any(target_arch = "mips", target_arch = "powerpc"))] #[derive(Debug)] pub struct Gauge { @@ -110,17 +111,24 @@ impl> Gauge { } } +/// Atomic operations for a [`Gauge`] value store. pub trait Atomic { + /// Increase the value by `1`. fn inc(&self) -> N; + /// Increase the value. fn inc_by(&self, v: N) -> N; + /// Decrease the value by `1`. fn dec(&self) -> N; + /// Decrease the value. fn dec_by(&self, v: N) -> N; + /// Set the value. fn set(&self, v: N) -> N; + /// Get the value. fn get(&self) -> N; } diff --git a/src/metrics/histogram.rs b/src/metrics/histogram.rs index e641d768..5922a283 100644 --- a/src/metrics/histogram.rs +++ b/src/metrics/histogram.rs @@ -54,6 +54,7 @@ pub(crate) struct Inner { } impl Histogram { + /// Create a new [`Histogram`]. pub fn new(buckets: impl Iterator) -> Self { Self { inner: Arc::new(RwLock::new(Inner { @@ -68,6 +69,7 @@ impl Histogram { } } + /// Observe the given value. pub fn observe(&self, v: f64) { self.observe_and_bucket(v); } @@ -110,6 +112,7 @@ impl TypedMetric for Histogram { const TYPE: MetricType = MetricType::Histogram; } +/// Exponential bucket distribution. pub fn exponential_buckets(start: f64, factor: f64, length: u16) -> impl Iterator { iter::repeat(()) .enumerate() @@ -117,6 +120,7 @@ pub fn exponential_buckets(start: f64, factor: f64, length: u16) -> impl Iterato .take(length.into()) } +/// Linear bucket distribution. pub fn linear_buckets(start: f64, width: f64, length: u16) -> impl Iterator { iter::repeat(()) .enumerate() diff --git a/src/metrics/info.rs b/src/metrics/info.rs index 8457207f..6201cdf9 100644 --- a/src/metrics/info.rs +++ b/src/metrics/info.rs @@ -16,6 +16,7 @@ use crate::metrics::{MetricType, TypedMetric}; pub struct Info(pub(crate) S); impl Info { + /// Create [`Info`] metric with the provided label set. pub fn new(label_set: S) -> Self { Self(label_set) } diff --git a/src/registry.rs b/src/registry.rs index 5c4dbfad..22249d3f 100644 --- a/src/registry.rs +++ b/src/registry.rs @@ -274,6 +274,7 @@ impl Registry { .expect("sub_registries not to be empty.") } + /// [`Iterator`] over all metrics registered with the [`Registry`]. pub fn iter(&self) -> std::iter::Chain, CollectorIterator> { return self.iter_metrics().chain(self.iter_collectors()); } @@ -409,6 +410,7 @@ impl From for Prefix { } } +/// OpenMetrics metric descriptor. #[derive(Debug, Clone)] pub struct Descriptor { name: String, @@ -418,6 +420,7 @@ pub struct Descriptor { } impl Descriptor { + /// Creates a new [`Descriptor`]. pub fn new, H: Into>( name: N, help: H, @@ -440,18 +443,22 @@ impl Descriptor { } } + /// Returns the name of the OpenMetrics metric [`Descriptor`]. pub fn name(&self) -> &str { &self.name } + /// Returns the help text of the OpenMetrics metric [`Descriptor`]. pub fn help(&self) -> &str { &self.help } + /// Returns the unit of the OpenMetrics metric [`Descriptor`]. pub fn unit(&self) -> &Option { &self.unit } + /// Returns the label set of the OpenMetrics metric [`Descriptor`]. pub fn labels(&self) -> &[(Cow<'static, str>, Cow<'static, str>)] { &self.labels } @@ -461,6 +468,7 @@ impl Descriptor { /// /// See [`Unit::Other`] to specify alternative units. #[derive(Debug, Clone)] +#[allow(missing_docs)] pub enum Unit { Amperes, Bytes,