refactor(metrics): use macros for constructing counter and histogram metrics (#755)

This commit is contained in:
Sanchith Hegde
2023-03-16 02:27:38 +05:30
committed by GitHub
parent 5ae2f63fbb
commit 58106d91f0
5 changed files with 101 additions and 97 deletions

View File

@ -0,0 +1,65 @@
//! Utilities to easily create opentelemetry contexts, meters and metrics.
/// Create a metrics [`Context`][Context] with the specified name.
///
/// [Context]: opentelemetry::Context
#[macro_export]
macro_rules! metrics_context {
($name:ident) => {
pub(crate) static $name: once_cell::sync::Lazy<$crate::opentelemetry::Context> =
once_cell::sync::Lazy::new($crate::opentelemetry::Context::current);
};
}
/// Create a global [`Meter`][Meter] with the specified name and and an optional description.
///
/// [Meter]: opentelemetry::metrics::Meter
#[macro_export]
macro_rules! global_meter {
($name:ident) => {
static $name: once_cell::sync::Lazy<$crate::opentelemetry::metrics::Meter> =
once_cell::sync::Lazy::new(|| $crate::opentelemetry::global::meter(stringify!($name)));
};
($name:ident, $description:literal) => {
static $name: once_cell::sync::Lazy<$crate::opentelemetry::metrics::Meter> =
once_cell::sync::Lazy::new(|| $crate::opentelemetry::global::meter($description));
};
}
/// Create a [`Counter`][Counter] metric with the specified name and an optional description,
/// associated with the specified meter. Note that the meter must be to a valid [`Meter`][Meter].
///
/// [Counter]: opentelemetry::metrics::Counter
/// [Meter]: opentelemetry::metrics::Meter
#[macro_export]
macro_rules! counter_metric {
($name:ident, $meter:ident) => {
pub(crate) static $name: once_cell::sync::Lazy<
$crate::opentelemetry::metrics::Counter<u64>,
> = once_cell::sync::Lazy::new(|| $meter.u64_counter(stringify!($name)).init());
};
($name:ident, $meter:ident, description:literal) => {
pub(crate) static $name: once_cell::sync::Lazy<
$crate::opentelemetry::metrics::Counter<u64>,
> = once_cell::sync::Lazy::new(|| $meter.u64_counter($description).init());
};
}
/// Create a [`Histogram`][Histogram] metric with the specified name and an optional description,
/// associated with the specified meter. Note that the meter must be to a valid [`Meter`][Meter].
///
/// [Histogram]: opentelemetry::metrics::Histogram
/// [Meter]: opentelemetry::metrics::Meter
#[macro_export]
macro_rules! histogram_metric {
($name:ident, $meter:ident) => {
pub(crate) static $name: once_cell::sync::Lazy<
$crate::opentelemetry::metrics::Histogram<f64>,
> = once_cell::sync::Lazy::new(|| $meter.f64_histogram(stringify!($name)).init());
};
($name:ident, $meter:ident, $description:literal) => {
pub(crate) static $name: once_cell::sync::Lazy<
$crate::opentelemetry::metrics::Histogram<f64>,
> = once_cell::sync::Lazy::new(|| $meter.f64_histogram($description).init());
};
}