use std::fmt::Display;
pub trait Counter<T: MetricValue>: Send + Sync {
fn inc(&self);
fn inc_by(&self, value: T);
}
#[allow(dead_code)]
pub trait Gauge<T: MetricValue>: Send + Sync {
fn set(&self, value: T);
fn inc_by(&self, value: T);
fn dec_by(&self, value: T);
}
pub trait GaugeVec<T: MetricValue>: Send + Sync {
fn set(&self, label_values: &[&str], value: T);
fn remove_label_values(&self, label_values: &[&str]);
fn reset(&self);
}
pub trait MetricValue: Send + Sync {
const METRIC_TYPE: MetricType;
}
pub enum MetricType {
F64,
U64,
I64,
}
impl MetricValue for f64 {
const METRIC_TYPE: MetricType = MetricType::F64;
}
impl MetricValue for u64 {
const METRIC_TYPE: MetricType = MetricType::U64;
}
impl MetricValue for i64 {
const METRIC_TYPE: MetricType = MetricType::I64;
}
#[derive(Debug)]
pub enum TelemetryError {
RegisterCounterError(String),
RegisterGaugeError(String),
EncodeMetricsError(String),
ConfigValidationError(String),
}
impl Display for TelemetryError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TelemetryError::RegisterCounterError(msg) => {
write!(f, "Error registering counter: {}", msg)
}
TelemetryError::RegisterGaugeError(msg) => {
write!(f, "Error registering gauge: {}", msg)
}
TelemetryError::EncodeMetricsError(msg) => {
write!(f, "Error encoding metrics: {}", msg)
}
TelemetryError::ConfigValidationError(msg) => {
write!(f, "Error validating telemetry configuration: {}", msg)
}
}
}
}
impl std::error::Error for TelemetryError {}