Skip to content

Commit 286fe02

Browse files
committed
feat: [torrust#1128] add new metric UDP total requests
In the stats enpoint the new values are: - udp4_requests - udp6_requests
1 parent 20639e8 commit 286fe02

File tree

6 files changed

+73
-18
lines changed

6 files changed

+73
-18
lines changed

src/core/services/statistics/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,12 @@ pub async fn get_metrics(tracker: Arc<Tracker>) -> TrackerMetrics {
7373
tcp6_connections_handled: stats.tcp6_connections_handled,
7474
tcp6_announces_handled: stats.tcp6_announces_handled,
7575
tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
76+
udp4_requests: stats.udp4_requests,
7677
udp4_connections_handled: stats.udp4_connections_handled,
7778
udp4_announces_handled: stats.udp4_announces_handled,
7879
udp4_scrapes_handled: stats.udp4_scrapes_handled,
7980
udp4_errors_handled: stats.udp4_errors_handled,
81+
udp6_requests: stats.udp6_requests,
8082
udp6_connections_handled: stats.udp6_connections_handled,
8183
udp6_announces_handled: stats.udp6_announces_handled,
8284
udp6_scrapes_handled: stats.udp6_scrapes_handled,

src/core/statistics.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,12 @@ pub enum Event {
4444
Tcp4Scrape,
4545
Tcp6Announce,
4646
Tcp6Scrape,
47+
Udp4Request,
4748
Udp4Connect,
4849
Udp4Announce,
4950
Udp4Scrape,
5051
Udp4Error,
52+
Udp6Request,
5153
Udp6Connect,
5254
Udp6Announce,
5355
Udp6Scrape,
@@ -72,12 +74,16 @@ pub struct Metrics {
7274
pub tcp4_announces_handled: u64,
7375
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
7476
pub tcp4_scrapes_handled: u64,
77+
7578
/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
7679
pub tcp6_connections_handled: u64,
7780
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
7881
pub tcp6_announces_handled: u64,
7982
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
8083
pub tcp6_scrapes_handled: u64,
84+
85+
/// Total number of UDP (UDP tracker) requests from IPv4 peers.
86+
pub udp4_requests: u64,
8187
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
8288
pub udp4_connections_handled: u64,
8389
/// Total number of UDP (UDP tracker) `announce` requests from IPv4 peers.
@@ -86,6 +92,9 @@ pub struct Metrics {
8692
pub udp4_scrapes_handled: u64,
8793
/// Total number of UDP (UDP tracker) `error` requests from IPv4 peers.
8894
pub udp4_errors_handled: u64,
95+
96+
/// Total number of UDP (UDP tracker) requests from IPv4 peers.
97+
pub udp6_requests: u64,
8998
/// Total number of UDP (UDP tracker) `connection` requests from IPv6 peers.
9099
pub udp6_connections_handled: u64,
91100
/// Total number of UDP (UDP tracker) `announce` requests from IPv6 peers.
@@ -165,6 +174,9 @@ async fn event_handler(event: Event, stats_repository: &Repo) {
165174
}
166175

167176
// UDP4
177+
Event::Udp4Request => {
178+
stats_repository.increase_udp4_requests().await;
179+
}
168180
Event::Udp4Connect => {
169181
stats_repository.increase_udp4_connections().await;
170182
}
@@ -179,6 +191,9 @@ async fn event_handler(event: Event, stats_repository: &Repo) {
179191
}
180192

181193
// UDP6
194+
Event::Udp6Request => {
195+
stats_repository.increase_udp6_requests().await;
196+
}
182197
Event::Udp6Connect => {
183198
stats_repository.increase_udp6_connections().await;
184199
}
@@ -276,6 +291,12 @@ impl Repo {
276291
drop(stats_lock);
277292
}
278293

294+
pub async fn increase_udp4_requests(&self) {
295+
let mut stats_lock = self.stats.write().await;
296+
stats_lock.udp4_requests += 1;
297+
drop(stats_lock);
298+
}
299+
279300
pub async fn increase_udp4_connections(&self) {
280301
let mut stats_lock = self.stats.write().await;
281302
stats_lock.udp4_connections_handled += 1;
@@ -300,6 +321,12 @@ impl Repo {
300321
drop(stats_lock);
301322
}
302323

324+
pub async fn increase_udp6_requests(&self) {
325+
let mut stats_lock = self.stats.write().await;
326+
stats_lock.udp6_requests += 1;
327+
drop(stats_lock);
328+
}
329+
303330
pub async fn increase_udp6_connections(&self) {
304331
let mut stats_lock = self.stats.write().await;
305332
stats_lock.udp6_connections_handled += 1;

src/servers/apis/v1/context/stats/resources.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ pub struct Stats {
2626
pub tcp4_announces_handled: u64,
2727
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
2828
pub tcp4_scrapes_handled: u64,
29+
2930
/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
3031
pub tcp6_connections_handled: u64,
3132
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
3233
pub tcp6_announces_handled: u64,
3334
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
3435
pub tcp6_scrapes_handled: u64,
36+
37+
/// Total number of UDP (UDP tracker) requests from IPv4 peers.
38+
pub udp4_requests: u64,
3539
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
3640
pub udp4_connections_handled: u64,
3741
/// Total number of UDP (UDP tracker) `announce` requests from IPv4 peers.
@@ -40,6 +44,9 @@ pub struct Stats {
4044
pub udp4_scrapes_handled: u64,
4145
/// Total number of UDP (UDP tracker) `scrape` requests from IPv4 peers.
4246
pub udp4_errors_handled: u64,
47+
48+
/// Total number of UDP (UDP tracker) requests from IPv6 peers.
49+
pub udp6_requests: u64,
4350
/// Total number of UDP (UDP tracker) `connection` requests from IPv6 peers.
4451
pub udp6_connections_handled: u64,
4552
/// Total number of UDP (UDP tracker) `announce` requests from IPv6 peers.
@@ -63,10 +70,12 @@ impl From<TrackerMetrics> for Stats {
6370
tcp6_connections_handled: metrics.protocol_metrics.tcp6_connections_handled,
6471
tcp6_announces_handled: metrics.protocol_metrics.tcp6_announces_handled,
6572
tcp6_scrapes_handled: metrics.protocol_metrics.tcp6_scrapes_handled,
73+
udp4_requests: metrics.protocol_metrics.udp4_requests,
6674
udp4_connections_handled: metrics.protocol_metrics.udp4_connections_handled,
6775
udp4_announces_handled: metrics.protocol_metrics.udp4_announces_handled,
6876
udp4_scrapes_handled: metrics.protocol_metrics.udp4_scrapes_handled,
6977
udp4_errors_handled: metrics.protocol_metrics.udp4_errors_handled,
78+
udp6_requests: metrics.protocol_metrics.udp6_requests,
7079
udp6_connections_handled: metrics.protocol_metrics.udp6_connections_handled,
7180
udp6_announces_handled: metrics.protocol_metrics.udp6_announces_handled,
7281
udp6_scrapes_handled: metrics.protocol_metrics.udp6_scrapes_handled,
@@ -100,14 +109,16 @@ mod tests {
100109
tcp6_connections_handled: 8,
101110
tcp6_announces_handled: 9,
102111
tcp6_scrapes_handled: 10,
103-
udp4_connections_handled: 11,
104-
udp4_announces_handled: 12,
105-
udp4_scrapes_handled: 13,
106-
udp4_errors_handled: 14,
107-
udp6_connections_handled: 15,
108-
udp6_announces_handled: 16,
109-
udp6_scrapes_handled: 17,
110-
udp6_errors_handled: 18
112+
udp4_requests: 11,
113+
udp4_connections_handled: 12,
114+
udp4_announces_handled: 13,
115+
udp4_scrapes_handled: 14,
116+
udp4_errors_handled: 15,
117+
udp6_requests: 16,
118+
udp6_connections_handled: 17,
119+
udp6_announces_handled: 18,
120+
udp6_scrapes_handled: 19,
121+
udp6_errors_handled: 20
111122
}
112123
}),
113124
Stats {
@@ -121,14 +132,16 @@ mod tests {
121132
tcp6_connections_handled: 8,
122133
tcp6_announces_handled: 9,
123134
tcp6_scrapes_handled: 10,
124-
udp4_connections_handled: 11,
125-
udp4_announces_handled: 12,
126-
udp4_scrapes_handled: 13,
127-
udp4_errors_handled: 14,
128-
udp6_connections_handled: 15,
129-
udp6_announces_handled: 16,
130-
udp6_scrapes_handled: 17,
131-
udp6_errors_handled: 18
135+
udp4_requests: 11,
136+
udp4_connections_handled: 12,
137+
udp4_announces_handled: 13,
138+
udp4_scrapes_handled: 14,
139+
udp4_errors_handled: 15,
140+
udp6_requests: 16,
141+
udp6_connections_handled: 17,
142+
udp6_announces_handled: 18,
143+
udp6_scrapes_handled: 19,
144+
udp6_errors_handled: 20
132145
}
133146
);
134147
}

src/servers/apis/v1/context/stats/responses.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
4747
tracker_metrics.protocol_metrics.tcp6_scrapes_handled
4848
));
4949

50+
lines.push(format!("udp4_requests {}", tracker_metrics.protocol_metrics.udp4_requests));
5051
lines.push(format!(
5152
"udp4_connections_handled {}",
5253
tracker_metrics.protocol_metrics.udp4_connections_handled
@@ -64,6 +65,7 @@ pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
6465
tracker_metrics.protocol_metrics.udp4_errors_handled
6566
));
6667

68+
lines.push(format!("udp6_requests {}", tracker_metrics.protocol_metrics.udp6_requests));
6769
lines.push(format!(
6870
"udp6_connections_handled {}",
6971
tracker_metrics.protocol_metrics.udp6_connections_handled

src/servers/udp/server/launcher.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::net::SocketAddr;
1+
use std::net::{IpAddr, SocketAddr};
22
use std::sync::Arc;
33
use std::time::Duration;
44

@@ -11,7 +11,7 @@ use tracing::instrument;
1111

1212
use super::request_buffer::ActiveRequests;
1313
use crate::bootstrap::jobs::Started;
14-
use crate::core::Tracker;
14+
use crate::core::{statistics, Tracker};
1515
use crate::servers::logging::STARTED_ON;
1616
use crate::servers::registar::ServiceHealthCheckJob;
1717
use crate::servers::signals::{shutdown_signal_with_message, Halted};
@@ -140,6 +140,15 @@ impl Launcher {
140140
}
141141
};
142142

143+
match req.from.ip() {
144+
IpAddr::V4(_) => {
145+
tracker.send_stats_event(statistics::Event::Udp4Request).await;
146+
}
147+
IpAddr::V6(_) => {
148+
tracker.send_stats_event(statistics::Event::Udp6Request).await;
149+
}
150+
}
151+
143152
// We spawn the new task even if there active requests buffer is
144153
// full. This could seem counterintuitive because we are accepting
145154
// more request and consuming more memory even if the server is

tests/servers/api/v1/contract/context/stats.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,12 @@ async fn should_allow_getting_tracker_statistics() {
4040
tcp6_connections_handled: 0,
4141
tcp6_announces_handled: 0,
4242
tcp6_scrapes_handled: 0,
43+
udp4_requests: 0,
4344
udp4_connections_handled: 0,
4445
udp4_announces_handled: 0,
4546
udp4_scrapes_handled: 0,
4647
udp4_errors_handled: 0,
48+
udp6_requests: 0,
4749
udp6_connections_handled: 0,
4850
udp6_announces_handled: 0,
4951
udp6_scrapes_handled: 0,

0 commit comments

Comments
 (0)