use opentelemetry::metrics::{Counter, Gauge, Histogram, Meter, UpDownCounter};
#[must_use]
pub fn meter() -> Meter {
meter_named("autumn-plugin-otel")
}
#[must_use]
pub fn meter_named(name: &'static str) -> Meter {
opentelemetry::global::meter(name)
}
pub struct OtelMetrics {
meter: Meter,
}
impl OtelMetrics {
#[must_use]
pub fn new() -> Self {
Self::with_meter(meter())
}
#[must_use]
pub const fn with_meter(meter: Meter) -> Self {
Self { meter }
}
#[must_use]
pub fn counter(&self, name: &str, description: &str) -> Counter<u64> {
self.meter
.u64_counter(name.to_owned())
.with_description(description.to_owned())
.build()
}
#[must_use]
pub fn histogram(&self, name: &str, description: &str) -> Histogram<f64> {
self.meter
.f64_histogram(name.to_owned())
.with_description(description.to_owned())
.build()
}
#[must_use]
pub fn gauge(&self, name: &str, description: &str) -> Gauge<f64> {
self.meter
.f64_gauge(name.to_owned())
.with_description(description.to_owned())
.build()
}
#[must_use]
pub fn up_down_counter(&self, name: &str, description: &str) -> UpDownCounter<i64> {
self.meter
.i64_up_down_counter(name.to_owned())
.with_description(description.to_owned())
.build()
}
}
impl Default for OtelMetrics {
fn default() -> Self {
Self::new()
}
}
impl std::fmt::Debug for OtelMetrics {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OtelMetrics").finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests;