Skip to content

Commit db6b491

Browse files
committed
refactor(metrics): [#1580] add associated types to collection-level Sum trait
- Convert collection Sum trait from fixed return type to associated Output type - MetricKindCollection<Counter> now returns Option<u64> preserving integer precision - MetricKindCollection<Gauge> now returns Option<f64> for direct float access - MetricCollection maintains Option<AggregateValue> for backward compatibility - Simplify implementation by directly delegating to metric-level sum methods - Remove intermediate conversions in metric kind collections This completes the associated types pattern across both metric-level and collection-level Sum traits, allowing each implementation to return the most mathematically appropriate type while maintaining API compatibility.
1 parent 0d9f883 commit db6b491

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

packages/metrics/src/metric_collection/aggregate.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,40 @@ use crate::metric::MetricName;
77
use crate::metric_collection::{MetricCollection, MetricKindCollection};
88

99
pub trait Sum {
10-
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<AggregateValue>;
10+
type Output;
11+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Self::Output;
1112
}
1213

1314
impl Sum for MetricCollection {
14-
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<AggregateValue> {
15+
type Output = Option<AggregateValue>;
16+
17+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Self::Output {
1518
if let Some(value) = self.counters.sum(metric_name, label_set_criteria) {
16-
return Some(value);
19+
#[allow(clippy::cast_precision_loss)]
20+
return Some(AggregateValue::new(value as f64));
21+
}
22+
23+
if let Some(value) = self.gauges.sum(metric_name, label_set_criteria) {
24+
return Some(AggregateValue::new(value));
1725
}
1826

19-
self.gauges.sum(metric_name, label_set_criteria)
27+
None
2028
}
2129
}
2230

2331
impl Sum for MetricKindCollection<Counter> {
24-
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<AggregateValue> {
25-
self.metrics.get(metric_name).map(|metric| {
26-
let sum: u64 = metric.sum(label_set_criteria);
27-
#[allow(clippy::cast_precision_loss)]
28-
AggregateValue::new(sum as f64)
29-
})
32+
type Output = Option<u64>;
33+
34+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Self::Output {
35+
self.metrics.get(metric_name).map(|metric| metric.sum(label_set_criteria))
3036
}
3137
}
3238

3339
impl Sum for MetricKindCollection<Gauge> {
34-
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<AggregateValue> {
35-
self.metrics.get(metric_name).map(|metric| {
36-
let sum: f64 = metric.sum(label_set_criteria);
37-
AggregateValue::new(sum)
38-
})
40+
type Output = Option<f64>;
41+
42+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Self::Output {
43+
self.metrics.get(metric_name).map(|metric| metric.sum(label_set_criteria))
3944
}
4045
}
4146

0 commit comments

Comments
 (0)