mirror of
https://github.com/juspay/hyperswitch.git
synced 2025-10-28 04:04:55 +08:00
refactor(metrics): use macros for constructing counter and histogram metrics (#755)
This commit is contained in:
65
crates/router_env/src/metrics.rs
Normal file
65
crates/router_env/src/metrics.rs
Normal 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());
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user