Skip to content

Commit 8fbcf90

Browse files
committed
refactor(metrics): extract collect_matching_samples to Metric<T> impl
Improve AI-generated code. Moves the collect_matching_samples helper method from individual aggregate implementations to the generic Metric<T> implementation, making it reusable across all aggregate functions. - Add collect_matching_samples method to Metric<T> for filtering samples by label criteria - Remove code duplication between Sum and Avg aggregate implementations - Improve code organization by centralizing sample collection logic - Maintain backward compatibility and all existing functionality This refactoring improves maintainability by providing a single, well-tested implementation of sample filtering that can be used by current and future aggregate functions.
1 parent 384b887 commit 8fbcf90

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

packages/metrics/src/metric/aggregate/avg.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::counter::Counter;
22
use crate::gauge::Gauge;
33
use crate::label::LabelSet;
4+
use crate::metric::aggregate::sum::Sum;
45
use crate::metric::Metric;
56

67
pub trait Avg {
@@ -12,20 +13,13 @@ impl Avg for Metric<Counter> {
1213
type Output = f64;
1314

1415
fn avg(&self, label_set_criteria: &LabelSet) -> Self::Output {
15-
let matching_samples: Vec<_> = self
16-
.sample_collection
17-
.iter()
18-
.filter(|(label_set, _measurement)| label_set.matches(label_set_criteria))
19-
.collect();
16+
let matching_samples = self.collect_matching_samples(label_set_criteria);
2017

2118
if matching_samples.is_empty() {
2219
return 0.0;
2320
}
2421

25-
let sum: u64 = matching_samples
26-
.iter()
27-
.map(|(_label_set, measurement)| measurement.value().primitive())
28-
.sum();
22+
let sum = self.sum(label_set_criteria);
2923

3024
#[allow(clippy::cast_precision_loss)]
3125
(sum as f64 / matching_samples.len() as f64)
@@ -36,20 +30,13 @@ impl Avg for Metric<Gauge> {
3630
type Output = f64;
3731

3832
fn avg(&self, label_set_criteria: &LabelSet) -> Self::Output {
39-
let matching_samples: Vec<_> = self
40-
.sample_collection
41-
.iter()
42-
.filter(|(label_set, _measurement)| label_set.matches(label_set_criteria))
43-
.collect();
33+
let matching_samples = self.collect_matching_samples(label_set_criteria);
4434

4535
if matching_samples.is_empty() {
4636
return 0.0;
4737
}
4838

49-
let sum: f64 = matching_samples
50-
.iter()
51-
.map(|(_label_set, measurement)| measurement.value().primitive())
52-
.sum();
39+
let sum = self.sum(label_set_criteria);
5340

5441
#[allow(clippy::cast_precision_loss)]
5542
(sum / matching_samples.len() as f64)

packages/metrics/src/metric/mod.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,17 @@ impl<T> Metric<T> {
7878
pub fn is_empty(&self) -> bool {
7979
self.sample_collection.is_empty()
8080
}
81+
82+
#[must_use]
83+
pub fn collect_matching_samples(
84+
&self,
85+
label_set_criteria: &LabelSet,
86+
) -> Vec<(&crate::label::LabelSet, &crate::sample::Measurement<T>)> {
87+
self.sample_collection
88+
.iter()
89+
.filter(|(label_set, _measurement)| label_set.matches(label_set_criteria))
90+
.collect()
91+
}
8192
}
8293

8394
impl Metric<Counter> {

0 commit comments

Comments
 (0)