gabotechs commented on code in PR #16195: URL: https://github.com/apache/datafusion/pull/16195#discussion_r2120615931
########## datafusion/physical-plan/src/metrics/value.rs: ########## @@ -401,6 +401,90 @@ pub enum MetricValue { StartTimestamp(Timestamp), /// The time at which execution ended EndTimestamp(Timestamp), + Custom { + /// The provided name of this metric + name: Cow<'static, str>, + /// A custom implementation of the metric value. + value: Arc<dyn CustomMetricValue>, + }, +} + +/// 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: +/// +/// 1. [`Self::aggregate`]: Defines how two metric values are combined +/// 2. [`Self::new_empty`]: Returns a new, zero-value instance for accumulation +/// 3. [`Self::as_any`]: Enables dynamic downcasting for type-specific operations +/// 4. [`Self::as_usize`]: Optionally maps the value to a `usize` (for sorting, display, etc.) +/// +/// # Examples +/// ``` +/// # use std::sync::Arc; +/// # use std::fmt::{Debug, Display}; +/// # use std::any::Any; +/// # use std::sync::atomic::{AtomicUsize, Ordering}; +/// +/// # use datafusion_physical_plan::metrics::CustomMetricValue; +/// +/// #[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) -> Option<usize> { +/// Some(self.count.load(Ordering::Relaxed)) +/// } +/// } +/// ``` +/// +/// [`MetricValue::Custom`]: super::MetricValue::Custom +pub trait CustomMetricValue: Display + Debug + Send + Sync { Review Comment: As this new type of metric is more meaty than the other ones, and we can expect people to come hear looking at the docs, what do you think about factoring it out to its own file? ``` metrics mod.rs baseline.rs builder.rs + custom.rs value.rs ``` This file is also dangerously approaching the 1000 LOC mark, so it will play in maintainability's favor ########## datafusion/physical-plan/src/metrics/value.rs: ########## @@ -443,6 +528,9 @@ impl MetricValue { .and_then(|ts| ts.timestamp_nanos_opt()) .map(|nanos| nanos as usize) .unwrap_or(0), + Self::Custom { name, value } => { + value.as_usize().unwrap_or_else(|| panic!("MetricValue::as_usize isn't supported for custom metric values. ({name}: {value:?})")) Review Comment: Usually panicking is avoided as much as possible, but it seems like it's not the only place where this can happen, and I have no better alternative without changes several function signatures. I think it's acceptable, but I'd weight other contributors opinion on this if any. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: github-unsubscr...@datafusion.apache.org For additional commands, e-mail: github-h...@datafusion.apache.org