pub trait CustomMetricValue:
Display
+ Debug
+ Send
+ Sync {
// Required methods
fn new_empty(&self) -> Arc<dyn CustomMetricValue>;
fn aggregate(&self, other: Arc<dyn CustomMetricValue + 'static>);
fn as_any(&self) -> &dyn Any;
fn is_eq(&self, other: &Arc<dyn CustomMetricValue>) -> bool;
// Provided method
fn as_usize(&self) -> usize { ... }
}Expand description
A trait for implementing custom metric values.
This trait enables defining application- or operator-specific metric types
that can be aggregated and displayed alongside standard metrics. These
custom metrics integrate with MetricValue::Custom and support
aggregation logic, introspection, and optional numeric representation.
§Requirements
Implementations of CustomMetricValue must satisfy the following:
Self::aggregate: Defines how two metric values are combinedSelf::new_empty: Returns a new, zero-value instance for accumulationSelf::as_any: Enables dynamic downcasting for type-specific operationsSelf::as_usize: Optionally maps the value to ausize(for sorting, display, etc.)Self::is_eq: Implements comparison between two values, this isn’t reusing the std PartialEq trait because this trait is used dynamically in the context ofMetricValue::Custom
§Examples
#[derive(Debug, Default)]
struct MyCounter {
count: AtomicUsize,
}
impl Display for MyCounter {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "count: {}", self.count.load(Ordering::Relaxed))
}
}
impl CustomMetricValue for MyCounter {
fn new_empty(&self) -> Arc<dyn CustomMetricValue> {
Arc::new(Self::default())
}
fn aggregate(&self, other: Arc<dyn CustomMetricValue>) {
let other = other.as_any().downcast_ref::<Self>().unwrap();
self.count
.fetch_add(other.count.load(Ordering::Relaxed), Ordering::Relaxed);
}
fn as_any(&self) -> &dyn Any {
self
}
fn as_usize(&self) -> usize {
self.count.load(Ordering::Relaxed)
}
fn is_eq(&self, other: &Arc<dyn CustomMetricValue>) -> bool {
let Some(other) = other.as_any().downcast_ref::<Self>() else {
return false;
};
self.count.load(Ordering::Relaxed) == other.count.load(Ordering::Relaxed)
}
}Required Methods§
Sourcefn new_empty(&self) -> Arc<dyn CustomMetricValue>
fn new_empty(&self) -> Arc<dyn CustomMetricValue>
Returns a new, zero-initialized version of this metric value.
This value is used during metric aggregation to accumulate results.
Sourcefn aggregate(&self, other: Arc<dyn CustomMetricValue + 'static>)
fn aggregate(&self, other: Arc<dyn CustomMetricValue + 'static>)
Merges another metric value into this one.
The type of other could be of a different custom type as long as it’s aggregatable into self.
Sourcefn is_eq(&self, other: &Arc<dyn CustomMetricValue>) -> bool
fn is_eq(&self, other: &Arc<dyn CustomMetricValue>) -> bool
Compares this value with another custom value.