Skip to content

Commit 00ac210

Browse files
committed
refactor(metrics): [#1580] remove AggregateValue wrapper, return primitive types from aggregates
- Remove AggregateValue struct and its entire module from metrics package - Simplify Sum trait in metric collections to return Option<f64> directly - Update MetricKindCollection implementations to cast counter values to f64 - Remove AggregateValue dependencies from http-tracker-core, udp-tracker-core, and udp-tracker-server - Eliminate unnecessary wrapper overhead in aggregate operations - Maintain backward compatibility by converting all aggregate results to f64 This change completes the metrics package refactoring by removing the generic AggregateValue wrapper that added no value when aggregate functions can return mathematically appropriate primitive types directly.
1 parent db6b491 commit 00ac210

File tree

6 files changed

+42
-220
lines changed

6 files changed

+42
-220
lines changed

packages/http-tracker-core/src/statistics/metrics.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ impl Metrics {
5353
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
5454
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
5555
)
56-
.unwrap_or_default()
57-
.value() as u64
56+
.unwrap_or_default() as u64
5857
}
5958

6059
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
@@ -67,8 +66,7 @@ impl Metrics {
6766
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
6867
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
6968
)
70-
.unwrap_or_default()
71-
.value() as u64
69+
.unwrap_or_default() as u64
7270
}
7371

7472
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
@@ -81,8 +79,7 @@ impl Metrics {
8179
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
8280
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
8381
)
84-
.unwrap_or_default()
85-
.value() as u64
82+
.unwrap_or_default() as u64
8683
}
8784

8885
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
@@ -95,7 +92,6 @@ impl Metrics {
9592
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
9693
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
9794
)
98-
.unwrap_or_default()
99-
.value() as u64
95+
.unwrap_or_default() as u64
10096
}
10197
}

packages/metrics/src/aggregate.rs

Lines changed: 0 additions & 143 deletions
This file was deleted.

packages/metrics/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod aggregate;
21
pub mod counter;
32
pub mod gauge;
43
pub mod label;

packages/metrics/src/metric_collection/aggregate.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::aggregate::AggregateValue;
21
use crate::counter::Counter;
32
use crate::gauge::Gauge;
43
use crate::label::LabelSet;
@@ -7,39 +6,34 @@ use crate::metric::MetricName;
76
use crate::metric_collection::{MetricCollection, MetricKindCollection};
87

98
pub trait Sum {
10-
type Output;
11-
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Self::Output;
9+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<f64>;
1210
}
1311

1412
impl Sum for MetricCollection {
15-
type Output = Option<AggregateValue>;
16-
17-
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Self::Output {
13+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<f64> {
1814
if let Some(value) = self.counters.sum(metric_name, label_set_criteria) {
19-
#[allow(clippy::cast_precision_loss)]
20-
return Some(AggregateValue::new(value as f64));
15+
return Some(value);
2116
}
2217

2318
if let Some(value) = self.gauges.sum(metric_name, label_set_criteria) {
24-
return Some(AggregateValue::new(value));
19+
return Some(value);
2520
}
2621

2722
None
2823
}
2924
}
3025

3126
impl Sum for MetricKindCollection<Counter> {
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))
27+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<f64> {
28+
#[allow(clippy::cast_precision_loss)]
29+
self.metrics
30+
.get(metric_name)
31+
.map(|metric| metric.sum(label_set_criteria) as f64)
3632
}
3733
}
3834

3935
impl Sum for MetricKindCollection<Gauge> {
40-
type Output = Option<f64>;
41-
42-
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Self::Output {
36+
fn sum(&self, metric_name: &MetricName, label_set_criteria: &LabelSet) -> Option<f64> {
4337
self.metrics.get(metric_name).map(|metric| metric.sum(label_set_criteria))
4438
}
4539
}
@@ -81,10 +75,10 @@ mod tests {
8175
)
8276
.unwrap();
8377

84-
assert_eq!(collection.sum(&metric_name, &LabelSet::empty()), Some(2.0.into()));
78+
assert_eq!(collection.sum(&metric_name, &LabelSet::empty()), Some(2.0));
8579
assert_eq!(
8680
collection.sum(&metric_name, &(label_name!("label_1"), LabelValue::new("value_1")).into()),
87-
Some(1.0.into())
81+
Some(1.0)
8882
);
8983
}
9084

@@ -114,10 +108,10 @@ mod tests {
114108
)
115109
.unwrap();
116110

117-
assert_eq!(collection.sum(&metric_name, &LabelSet::empty()), Some(2.0.into()));
111+
assert_eq!(collection.sum(&metric_name, &LabelSet::empty()), Some(2.0));
118112
assert_eq!(
119113
collection.sum(&metric_name, &(label_name!("label_1"), LabelValue::new("value_1")).into()),
120-
Some(1.0.into())
114+
Some(1.0)
121115
);
122116
}
123117
}

packages/udp-tracker-core/src/statistics/metrics.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ impl Metrics {
5454
&metric_name!(UDP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
5555
&[("server_binding_address_ip_family", "inet"), ("request_kind", "connect")].into(),
5656
)
57-
.unwrap_or_default()
58-
.value() as u64
57+
.unwrap_or_default() as u64
5958
}
6059

6160
/// Total number of UDP (UDP tracker) `announce` requests from IPv4 peers.
@@ -68,8 +67,7 @@ impl Metrics {
6867
&metric_name!(UDP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
6968
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
7069
)
71-
.unwrap_or_default()
72-
.value() as u64
70+
.unwrap_or_default() as u64
7371
}
7472

7573
/// Total number of UDP (UDP tracker) `scrape` requests from IPv4 peers.
@@ -82,8 +80,7 @@ impl Metrics {
8280
&metric_name!(UDP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
8381
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
8482
)
85-
.unwrap_or_default()
86-
.value() as u64
83+
.unwrap_or_default() as u64
8784
}
8885

8986
/// Total number of UDP (UDP tracker) `connection` requests from IPv6 peers.
@@ -96,8 +93,7 @@ impl Metrics {
9693
&metric_name!(UDP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
9794
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "connect")].into(),
9895
)
99-
.unwrap_or_default()
100-
.value() as u64
96+
.unwrap_or_default() as u64
10197
}
10298

10399
/// Total number of UDP (UDP tracker) `announce` requests from IPv6 peers.
@@ -110,8 +106,7 @@ impl Metrics {
110106
&metric_name!(UDP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
111107
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
112108
)
113-
.unwrap_or_default()
114-
.value() as u64
109+
.unwrap_or_default() as u64
115110
}
116111

117112
/// Total number of UDP (UDP tracker) `scrape` requests from IPv6 peers.
@@ -124,7 +119,6 @@ impl Metrics {
124119
&metric_name!(UDP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
125120
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
126121
)
127-
.unwrap_or_default()
128-
.value() as u64
122+
.unwrap_or_default() as u64
129123
}
130124
}

0 commit comments

Comments
 (0)