use crate::KeyValue;
use core::fmt;
use std::sync::Arc;
#[cfg(feature = "experimental_metrics_bound_instruments")]
use super::BoundSyncInstrument;
use super::SyncInstrument;
#[derive(Clone)]
#[non_exhaustive]
pub struct Counter<T>(Arc<dyn SyncInstrument<T> + Send + Sync>);
impl<T> fmt::Debug for Counter<T>
where
T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("Counter<{}>", std::any::type_name::<T>()))
}
}
impl<T> Counter<T> {
pub fn new(inner: Arc<dyn SyncInstrument<T> + Send + Sync>) -> Self {
Counter(inner)
}
pub fn add(&self, value: T, attributes: &[KeyValue]) {
self.0.measure(value, attributes)
}
#[cfg(feature = "experimental_metrics_bound_instruments")]
pub fn bind(&self, attributes: &[KeyValue]) -> BoundCounter<T> {
BoundCounter(Arc::from(self.0.bind(attributes)))
}
}
#[derive(Clone)]
#[non_exhaustive]
pub struct ObservableCounter<T> {
_marker: std::marker::PhantomData<T>,
}
impl<T> ObservableCounter<T> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
ObservableCounter {
_marker: std::marker::PhantomData,
}
}
}
impl<T> fmt::Debug for ObservableCounter<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!(
"ObservableCounter<{}>",
std::any::type_name::<T>()
))
}
}
#[cfg(feature = "experimental_metrics_bound_instruments")]
#[derive(Clone)]
#[must_use = "dropping a BoundCounter immediately is a no-op; store it to benefit from pre-bound attributes"]
pub struct BoundCounter<T>(Arc<dyn BoundSyncInstrument<T> + Send + Sync>);
#[cfg(feature = "experimental_metrics_bound_instruments")]
impl<T> fmt::Debug for BoundCounter<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_fmt(format_args!("BoundCounter<{}>", std::any::type_name::<T>()))
}
}
#[cfg(feature = "experimental_metrics_bound_instruments")]
impl<T> BoundCounter<T> {
pub fn add(&self, value: T) {
self.0.measure(value)
}
}