Skip to content

Commit 70fd119

Browse files
committed
Merge #1584: Overhaul stats: Remove internal non-labeled metrics
a552482 refactor: [#1581] finished. Global metrics in API loaded from labeled metrics (Jose Celano) 6183eba refactor: [#1581] remove non-labeled metrics in udp-tracker-server pkg (Jose Celano) f008a0a fix: test for request counters in http-tracker-core (Jose Celano) 0284bef refactor: [#1581] remove non-labeled metrics in udp-tracker-core pkg (Jose Celano) a5c5a89 refactor: [#1581] remove unused code (Jose Celano) 44c1848 refactor: [#1581] remove non-labeled metrics in http-tracker-core pkg (Jose Celano) 42850f3 refactor: [#1581] extract methods (Jose Celano) Pull request description: Remove internal non-labeled metrics. ### Subtasks - [x] In `http-tracker-core` package. - [x] In `udp-tracker-core` package. - [x] In `udp-tracker-server` package. ACKs for top commit: josecelano: ACK a552482 Tree-SHA512: 6e92ac2a3c421282a96cd8d01ce1752f7427b351e9d9374050147cfd591477f88df72f2491ceb34f80644e77cccd2c70a74c9f523be230b792b11d5b6a21ae86
2 parents 86e6406 + a552482 commit 70fd119

File tree

25 files changed

+494
-929
lines changed

25 files changed

+494
-929
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/axum-http-tracker-server/tests/server/v1/contract.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -704,7 +704,7 @@ mod for_all_config_modes {
704704

705705
let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;
706706

707-
assert_eq!(stats.tcp4_announces_handled, 1);
707+
assert_eq!(stats.tcp4_announces_handled(), 1);
708708

709709
drop(stats);
710710

@@ -730,7 +730,7 @@ mod for_all_config_modes {
730730

731731
let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;
732732

733-
assert_eq!(stats.tcp6_announces_handled, 1);
733+
assert_eq!(stats.tcp6_announces_handled(), 1);
734734

735735
drop(stats);
736736

@@ -755,7 +755,7 @@ mod for_all_config_modes {
755755

756756
let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;
757757

758-
assert_eq!(stats.tcp6_announces_handled, 0);
758+
assert_eq!(stats.tcp6_announces_handled(), 0);
759759

760760
drop(stats);
761761

@@ -1149,7 +1149,7 @@ mod for_all_config_modes {
11491149

11501150
let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;
11511151

1152-
assert_eq!(stats.tcp4_scrapes_handled, 1);
1152+
assert_eq!(stats.tcp4_scrapes_handled(), 1);
11531153

11541154
drop(stats);
11551155

@@ -1181,7 +1181,7 @@ mod for_all_config_modes {
11811181

11821182
let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;
11831183

1184-
assert_eq!(stats.tcp6_scrapes_handled, 1);
1184+
assert_eq!(stats.tcp6_scrapes_handled(), 1);
11851185

11861186
drop(stats);
11871187

packages/axum-rest-tracker-api-server/src/v1/context/stats/handlers.rs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,13 @@ pub struct QueryParams {
4141
pub async fn get_stats_handler(
4242
State(state): State<(
4343
Arc<InMemoryTorrentRepository>,
44-
Arc<RwLock<BanService>>,
4544
Arc<bittorrent_tracker_core::statistics::repository::Repository>,
4645
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
4746
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
4847
)>,
4948
params: Query<QueryParams>,
5049
) -> Response {
51-
let metrics = get_metrics(
52-
state.0.clone(),
53-
state.1.clone(),
54-
state.2.clone(),
55-
state.3.clone(),
56-
state.4.clone(),
57-
)
58-
.await;
50+
let metrics = get_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;
5951

6052
match params.0.format {
6153
Some(format) => match format {

packages/axum-rest-tracker-api-server/src/v1/context/stats/routes.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
1818
&format!("{prefix}/stats"),
1919
get(get_stats_handler).with_state((
2020
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
21-
http_api_container.ban_service.clone(),
2221
http_api_container.tracker_core_container.stats_repository.clone(),
2322
http_api_container.http_stats_repository.clone(),
2423
http_api_container.udp_server_stats_repository.clone(),

packages/http-tracker-core/src/statistics/event/handler.rs

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use std::net::IpAddr;
21
use std::sync::Arc;
32

43
use torrust_tracker_metrics::label::{LabelSet, LabelValue};
@@ -12,19 +11,6 @@ use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
1211
pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now: DurationSinceUnixEpoch) {
1312
match event {
1413
Event::TcpAnnounce { connection, .. } => {
15-
// Global fixed metrics
16-
17-
match connection.client_ip_addr() {
18-
IpAddr::V4(_) => {
19-
stats_repository.increase_tcp4_announces().await;
20-
}
21-
IpAddr::V6(_) => {
22-
stats_repository.increase_tcp6_announces().await;
23-
}
24-
}
25-
26-
// Extendable metrics
27-
2814
let mut label_set = LabelSet::from(connection);
2915
label_set.upsert(label_name!("request_kind"), LabelValue::new("announce"));
3016

@@ -42,19 +28,6 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
4228
};
4329
}
4430
Event::TcpScrape { connection } => {
45-
// Global fixed metrics
46-
47-
match connection.client_ip_addr() {
48-
IpAddr::V4(_) => {
49-
stats_repository.increase_tcp4_scrapes().await;
50-
}
51-
IpAddr::V6(_) => {
52-
stats_repository.increase_tcp6_scrapes().await;
53-
}
54-
}
55-
56-
// Extendable metrics
57-
5831
let mut label_set = LabelSet::from(connection);
5932
label_set.upsert(label_name!("request_kind"), LabelValue::new("scrape"));
6033

@@ -113,7 +86,7 @@ mod tests {
11386

11487
let stats = stats_repository.get_stats().await;
11588

116-
assert_eq!(stats.tcp4_announces_handled, 1);
89+
assert_eq!(stats.tcp4_announces_handled(), 1);
11790
}
11891

11992
#[tokio::test]
@@ -137,7 +110,7 @@ mod tests {
137110

138111
let stats = stats_repository.get_stats().await;
139112

140-
assert_eq!(stats.tcp4_scrapes_handled, 1);
113+
assert_eq!(stats.tcp4_scrapes_handled(), 1);
141114
}
142115

143116
#[tokio::test]
@@ -150,7 +123,7 @@ mod tests {
150123
Event::TcpAnnounce {
151124
connection: ConnectionContext::new(
152125
RemoteClientAddr::new(ResolvedIp::FromSocketAddr(remote_client_ip), Some(8080)),
153-
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 7070)).unwrap(),
126+
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 7070)).unwrap(),
154127
),
155128
info_hash: sample_info_hash(),
156129
announcement: peer,
@@ -162,7 +135,7 @@ mod tests {
162135

163136
let stats = stats_repository.get_stats().await;
164137

165-
assert_eq!(stats.tcp6_announces_handled, 1);
138+
assert_eq!(stats.tcp6_announces_handled(), 1);
166139
}
167140

168141
#[tokio::test]
@@ -178,7 +151,7 @@ mod tests {
178151
))),
179152
Some(8080),
180153
),
181-
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 7070)).unwrap(),
154+
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 7070)).unwrap(),
182155
),
183156
},
184157
&stats_repository,
@@ -188,6 +161,6 @@ mod tests {
188161

189162
let stats = stats_repository.get_stats().await;
190163

191-
assert_eq!(stats.tcp6_scrapes_handled, 1);
164+
assert_eq!(stats.tcp6_scrapes_handled(), 1);
192165
}
193166
}

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

Lines changed: 62 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
use serde::Serialize;
22
use torrust_tracker_metrics::label::LabelSet;
33
use torrust_tracker_metrics::metric::MetricName;
4+
use torrust_tracker_metrics::metric_collection::aggregate::Sum;
45
use torrust_tracker_metrics::metric_collection::{Error, MetricCollection};
6+
use torrust_tracker_metrics::metric_name;
57
use torrust_tracker_primitives::DurationSinceUnixEpoch;
68

9+
use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
10+
711
/// Metrics collected by the tracker.
812
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
913
pub struct Metrics {
10-
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
11-
pub tcp4_announces_handled: u64,
12-
13-
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
14-
pub tcp4_scrapes_handled: u64,
15-
16-
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
17-
pub tcp6_announces_handled: u64,
18-
19-
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
20-
pub tcp6_scrapes_handled: u64,
21-
2214
/// A collection of metrics.
2315
pub metric_collection: MetricCollection,
2416
}
@@ -49,3 +41,61 @@ impl Metrics {
4941
self.metric_collection.set_gauge(metric_name, labels, value, now)
5042
}
5143
}
44+
45+
impl Metrics {
46+
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
47+
#[must_use]
48+
#[allow(clippy::cast_sign_loss)]
49+
#[allow(clippy::cast_possible_truncation)]
50+
pub fn tcp4_announces_handled(&self) -> u64 {
51+
self.metric_collection
52+
.sum(
53+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
54+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
55+
)
56+
.unwrap_or_default()
57+
.value() as u64
58+
}
59+
60+
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
61+
#[must_use]
62+
#[allow(clippy::cast_sign_loss)]
63+
#[allow(clippy::cast_possible_truncation)]
64+
pub fn tcp4_scrapes_handled(&self) -> u64 {
65+
self.metric_collection
66+
.sum(
67+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
68+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
69+
)
70+
.unwrap_or_default()
71+
.value() as u64
72+
}
73+
74+
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
75+
#[must_use]
76+
#[allow(clippy::cast_sign_loss)]
77+
#[allow(clippy::cast_possible_truncation)]
78+
pub fn tcp6_announces_handled(&self) -> u64 {
79+
self.metric_collection
80+
.sum(
81+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
82+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
83+
)
84+
.unwrap_or_default()
85+
.value() as u64
86+
}
87+
88+
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
89+
#[must_use]
90+
#[allow(clippy::cast_sign_loss)]
91+
#[allow(clippy::cast_possible_truncation)]
92+
pub fn tcp6_scrapes_handled(&self) -> u64 {
93+
self.metric_collection
94+
.sum(
95+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
96+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
97+
)
98+
.unwrap_or_default()
99+
.value() as u64
100+
}
101+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub mod event;
22
pub mod metrics;
33
pub mod repository;
4-
pub mod services;
54

65
use metrics::Metrics;
76
use torrust_tracker_metrics::metric::description::MetricDescription;

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

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,30 +33,6 @@ impl Repository {
3333
self.stats.read().await
3434
}
3535

36-
pub async fn increase_tcp4_announces(&self) {
37-
let mut stats_lock = self.stats.write().await;
38-
stats_lock.tcp4_announces_handled += 1;
39-
drop(stats_lock);
40-
}
41-
42-
pub async fn increase_tcp4_scrapes(&self) {
43-
let mut stats_lock = self.stats.write().await;
44-
stats_lock.tcp4_scrapes_handled += 1;
45-
drop(stats_lock);
46-
}
47-
48-
pub async fn increase_tcp6_announces(&self) {
49-
let mut stats_lock = self.stats.write().await;
50-
stats_lock.tcp6_announces_handled += 1;
51-
drop(stats_lock);
52-
}
53-
54-
pub async fn increase_tcp6_scrapes(&self) {
55-
let mut stats_lock = self.stats.write().await;
56-
stats_lock.tcp6_scrapes_handled += 1;
57-
drop(stats_lock);
58-
}
59-
6036
/// # Errors
6137
///
6238
/// This function will return an error if the metric collection fails to

0 commit comments

Comments
 (0)