Skip to content

Commit 08c0c38

Browse files
committed
refactor: [#1446] Calculate global metrics from labeled metrics
1 parent 3f5216e commit 08c0c38

File tree

2 files changed

+262
-51
lines changed
  • packages
    • rest-tracker-api-core/src/statistics
    • udp-tracker-server/src/statistics

2 files changed

+262
-51
lines changed

packages/rest-tracker-api-core/src/statistics/services.rs

Lines changed: 254 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,16 @@ use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepo
55
use bittorrent_udp_tracker_core::services::banning::BanService;
66
use bittorrent_udp_tracker_core::{self};
77
use tokio::sync::RwLock;
8+
use torrust_tracker_metrics::label::LabelSet;
89
use torrust_tracker_metrics::metric_collection::aggregate::Sum;
910
use torrust_tracker_metrics::metric_collection::MetricCollection;
1011
use torrust_tracker_metrics::metric_name;
11-
use torrust_udp_tracker_server::statistics::{self as udp_server_statistics, UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL};
12+
use torrust_udp_tracker_server::statistics::{
13+
self as udp_server_statistics, UDP_TRACKER_SERVER_ERRORS_TOTAL, UDP_TRACKER_SERVER_IPS_BANNED_TOTAL,
14+
UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS, UDP_TRACKER_SERVER_REQUESTS_ABORTED_TOTAL,
15+
UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL, UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL,
16+
UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL, UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL,
17+
};
1218

1319
use super::metrics::TorrentsMetrics;
1420
use crate::statistics::metrics::ProtocolMetrics;
@@ -42,12 +48,8 @@ pub async fn get_metrics(
4248
)
4349
.await;
4450

45-
let protocol_metrics_from_labeled_metrics = get_protocol_metrics_from_labeled_metrics(
46-
ban_service.clone(),
47-
http_stats_repository.clone(),
48-
udp_server_stats_repository.clone(),
49-
)
50-
.await;
51+
let protocol_metrics_from_labeled_metrics =
52+
get_protocol_metrics_from_labeled_metrics(http_stats_repository.clone(), udp_server_stats_repository.clone()).await;
5153

5254
// todo:
5355
// We keep both metrics until we deploy to production and we can
@@ -58,9 +60,9 @@ pub async fn get_metrics(
5860
let protocol_metrics = if protocol_metrics_from_global_metrics == protocol_metrics_from_labeled_metrics {
5961
protocol_metrics_from_labeled_metrics
6062
} else {
61-
// tracing::warn!("The protocol metrics from global metrics and labeled metrics are different");
62-
// tracing::warn!("Global metrics: {:?}", protocol_metrics_from_global_metrics);
63-
// tracing::warn!("Labeled metrics: {:?}", protocol_metrics_from_labeled_metrics);
63+
tracing::warn!("The protocol metrics from global metrics and labeled metrics are different");
64+
tracing::warn!("Global metrics: {:?}", protocol_metrics_from_global_metrics);
65+
tracing::warn!("Labeled metrics: {:?}", protocol_metrics_from_labeled_metrics);
6466
protocol_metrics_from_global_metrics
6567
};
6668

@@ -132,22 +134,141 @@ async fn get_protocol_metrics(
132134
}
133135

134136
#[allow(deprecated)]
137+
#[allow(clippy::too_many_lines)]
135138
async fn get_protocol_metrics_from_labeled_metrics(
136-
ban_service: Arc<RwLock<BanService>>,
137139
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
138140
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
139141
) -> ProtocolMetrics {
140-
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
141142
let http_stats = http_stats_repository.get_stats().await;
142143
let udp_server_stats = udp_server_stats_repository.get_stats().await;
143144

145+
// TCPv4
146+
144147
#[allow(clippy::cast_sign_loss)]
145148
#[allow(clippy::cast_possible_truncation)]
146149
let tcp4_announces_handled = http_stats
147150
.metric_collection
148151
.sum(
149152
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
150-
&[("request_kind", "announce")].into(), // todo: add label for `server_binding_ip_family` with value `inet` (inet/inet6)
153+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
154+
)
155+
.unwrap_or_default()
156+
.value() as u64;
157+
158+
#[allow(clippy::cast_sign_loss)]
159+
#[allow(clippy::cast_possible_truncation)]
160+
let tcp4_scrapes_handled = http_stats
161+
.metric_collection
162+
.sum(
163+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
164+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
165+
)
166+
.unwrap_or_default()
167+
.value() as u64;
168+
169+
// TCPv6
170+
171+
#[allow(clippy::cast_sign_loss)]
172+
#[allow(clippy::cast_possible_truncation)]
173+
let tcp6_announces_handled = http_stats
174+
.metric_collection
175+
.sum(
176+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
177+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
178+
)
179+
.unwrap_or_default()
180+
.value() as u64;
181+
182+
#[allow(clippy::cast_sign_loss)]
183+
#[allow(clippy::cast_possible_truncation)]
184+
let tcp6_scrapes_handled = http_stats
185+
.metric_collection
186+
.sum(
187+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
188+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
189+
)
190+
.unwrap_or_default()
191+
.value() as u64;
192+
193+
// UDP
194+
195+
#[allow(clippy::cast_sign_loss)]
196+
#[allow(clippy::cast_possible_truncation)]
197+
let udp_requests_aborted = udp_server_stats
198+
.metric_collection
199+
.sum(&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ABORTED_TOTAL), &LabelSet::empty())
200+
.unwrap_or_default()
201+
.value() as u64;
202+
203+
#[allow(clippy::cast_sign_loss)]
204+
#[allow(clippy::cast_possible_truncation)]
205+
let udp_requests_banned = udp_server_stats
206+
.metric_collection
207+
.sum(&metric_name!(UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL), &LabelSet::empty())
208+
.unwrap_or_default()
209+
.value() as u64;
210+
211+
#[allow(clippy::cast_sign_loss)]
212+
#[allow(clippy::cast_possible_truncation)]
213+
let udp_banned_ips_total = udp_server_stats
214+
.metric_collection
215+
.sum(&metric_name!(UDP_TRACKER_SERVER_IPS_BANNED_TOTAL), &LabelSet::empty())
216+
.unwrap_or_default()
217+
.value() as u64;
218+
219+
#[allow(clippy::cast_sign_loss)]
220+
#[allow(clippy::cast_possible_truncation)]
221+
let udp_avg_connect_processing_time_ns = udp_server_stats
222+
.metric_collection
223+
.sum(
224+
&metric_name!(UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS),
225+
&[("request_kind", "connect")].into(),
226+
)
227+
.unwrap_or_default()
228+
.value() as u64;
229+
230+
#[allow(clippy::cast_sign_loss)]
231+
#[allow(clippy::cast_possible_truncation)]
232+
let udp_avg_announce_processing_time_ns = udp_server_stats
233+
.metric_collection
234+
.sum(
235+
&metric_name!(UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS),
236+
&[("request_kind", "announce")].into(),
237+
)
238+
.unwrap_or_default()
239+
.value() as u64;
240+
241+
#[allow(clippy::cast_sign_loss)]
242+
#[allow(clippy::cast_possible_truncation)]
243+
let udp_avg_scrape_processing_time_ns = udp_server_stats
244+
.metric_collection
245+
.sum(
246+
&metric_name!(UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS),
247+
&[("request_kind", "scrape")].into(),
248+
)
249+
.unwrap_or_default()
250+
.value() as u64;
251+
252+
// UDPv4
253+
254+
#[allow(clippy::cast_sign_loss)]
255+
#[allow(clippy::cast_possible_truncation)]
256+
let udp4_requests = udp_server_stats
257+
.metric_collection
258+
.sum(
259+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL),
260+
&[("server_binding_address_ip_family", "inet")].into(),
261+
)
262+
.unwrap_or_default()
263+
.value() as u64;
264+
265+
#[allow(clippy::cast_sign_loss)]
266+
#[allow(clippy::cast_possible_truncation)]
267+
let udp4_connections_handled = udp_server_stats
268+
.metric_collection
269+
.sum(
270+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL),
271+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "connect")].into(),
151272
)
152273
.unwrap_or_default()
153274
.value() as u64;
@@ -158,21 +279,111 @@ async fn get_protocol_metrics_from_labeled_metrics(
158279
.metric_collection
159280
.sum(
160281
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL),
161-
&[("request_kind", "announce")].into(), // todo: add label for `server_binding_ip_family` with value `inet` (inet/inet6)
282+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
283+
)
284+
.unwrap_or_default()
285+
.value() as u64;
286+
287+
#[allow(clippy::cast_sign_loss)]
288+
#[allow(clippy::cast_possible_truncation)]
289+
let udp4_scrapes_handled = udp_server_stats
290+
.metric_collection
291+
.sum(
292+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL),
293+
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
294+
)
295+
.unwrap_or_default()
296+
.value() as u64;
297+
298+
#[allow(clippy::cast_sign_loss)]
299+
#[allow(clippy::cast_possible_truncation)]
300+
let udp4_responses = udp_server_stats
301+
.metric_collection
302+
.sum(
303+
&metric_name!(UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL),
304+
&[("server_binding_address_ip_family", "inet")].into(),
162305
)
163306
.unwrap_or_default()
164307
.value() as u64;
165308

166-
/*
309+
#[allow(clippy::cast_sign_loss)]
310+
#[allow(clippy::cast_possible_truncation)]
311+
let udp4_errors_handled = udp_server_stats
312+
.metric_collection
313+
.sum(
314+
&metric_name!(UDP_TRACKER_SERVER_ERRORS_TOTAL),
315+
&[("server_binding_address_ip_family", "inet")].into(),
316+
)
317+
.unwrap_or_default()
318+
.value() as u64;
167319

168-
todo:
320+
// UDPv6
169321

170-
- Add a label for `server_binding_ip_family` with value `inet` (inet/inet6)
171-
to all metrics containing an IP address. This will allow us to distinguish
172-
between IPv4 and IPv6 metrics.
173-
- Continue replacing the other metrics with the labeled metrics.
322+
#[allow(clippy::cast_sign_loss)]
323+
#[allow(clippy::cast_possible_truncation)]
324+
let udp6_requests = udp_server_stats
325+
.metric_collection
326+
.sum(
327+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL),
328+
&[("server_binding_address_ip_family", "inet6")].into(),
329+
)
330+
.unwrap_or_default()
331+
.value() as u64;
174332

175-
*/
333+
#[allow(clippy::cast_sign_loss)]
334+
#[allow(clippy::cast_possible_truncation)]
335+
let udp6_connections_handled = udp_server_stats
336+
.metric_collection
337+
.sum(
338+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL),
339+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "connect")].into(),
340+
)
341+
.unwrap_or_default()
342+
.value() as u64;
343+
344+
#[allow(clippy::cast_sign_loss)]
345+
#[allow(clippy::cast_possible_truncation)]
346+
let udp6_announces_handled = udp_server_stats
347+
.metric_collection
348+
.sum(
349+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL),
350+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
351+
)
352+
.unwrap_or_default()
353+
.value() as u64;
354+
355+
#[allow(clippy::cast_sign_loss)]
356+
#[allow(clippy::cast_possible_truncation)]
357+
let udp6_scrapes_handled = udp_server_stats
358+
.metric_collection
359+
.sum(
360+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL),
361+
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
362+
)
363+
.unwrap_or_default()
364+
.value() as u64;
365+
366+
#[allow(clippy::cast_sign_loss)]
367+
#[allow(clippy::cast_possible_truncation)]
368+
let udp6_responses = udp_server_stats
369+
.metric_collection
370+
.sum(
371+
&metric_name!(UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL),
372+
&[("server_binding_address_ip_family", "inet6")].into(),
373+
)
374+
.unwrap_or_default()
375+
.value() as u64;
376+
377+
#[allow(clippy::cast_sign_loss)]
378+
#[allow(clippy::cast_possible_truncation)]
379+
let udp6_errors_handled = udp_server_stats
380+
.metric_collection
381+
.sum(
382+
&metric_name!(UDP_TRACKER_SERVER_ERRORS_TOTAL),
383+
&[("server_binding_address_ip_family", "inet6")].into(),
384+
)
385+
.unwrap_or_default()
386+
.value() as u64;
176387

177388
// For backward compatibility we keep the `tcp4_connections_handled` and
178389
// `tcp6_connections_handled` metrics. They don't make sense for the HTTP
@@ -181,34 +392,34 @@ async fn get_protocol_metrics_from_labeled_metrics(
181392

182393
ProtocolMetrics {
183394
// TCPv4
184-
tcp4_connections_handled: tcp4_announces_handled + http_stats.tcp4_scrapes_handled,
395+
tcp4_connections_handled: tcp4_announces_handled + tcp4_scrapes_handled,
185396
tcp4_announces_handled,
186-
tcp4_scrapes_handled: http_stats.tcp4_scrapes_handled,
397+
tcp4_scrapes_handled,
187398
// TCPv6
188-
tcp6_connections_handled: http_stats.tcp6_announces_handled + http_stats.tcp6_scrapes_handled,
189-
tcp6_announces_handled: http_stats.tcp6_announces_handled,
190-
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled,
399+
tcp6_connections_handled: tcp6_announces_handled + tcp6_scrapes_handled,
400+
tcp6_announces_handled,
401+
tcp6_scrapes_handled,
191402
// UDP
192-
udp_requests_aborted: udp_server_stats.udp_requests_aborted,
193-
udp_requests_banned: udp_server_stats.udp_requests_banned,
194-
udp_banned_ips_total: udp_banned_ips_total as u64,
195-
udp_avg_connect_processing_time_ns: udp_server_stats.udp_avg_connect_processing_time_ns,
196-
udp_avg_announce_processing_time_ns: udp_server_stats.udp_avg_announce_processing_time_ns,
197-
udp_avg_scrape_processing_time_ns: udp_server_stats.udp_avg_scrape_processing_time_ns,
403+
udp_requests_aborted,
404+
udp_requests_banned,
405+
udp_banned_ips_total,
406+
udp_avg_connect_processing_time_ns,
407+
udp_avg_announce_processing_time_ns,
408+
udp_avg_scrape_processing_time_ns,
198409
// UDPv4
199-
udp4_requests: udp_server_stats.udp4_requests,
200-
udp4_connections_handled: udp_server_stats.udp4_connections_handled,
410+
udp4_requests,
411+
udp4_connections_handled,
201412
udp4_announces_handled,
202-
udp4_scrapes_handled: udp_server_stats.udp4_scrapes_handled,
203-
udp4_responses: udp_server_stats.udp4_responses,
204-
udp4_errors_handled: udp_server_stats.udp4_errors_handled,
413+
udp4_scrapes_handled,
414+
udp4_responses,
415+
udp4_errors_handled,
205416
// UDPv6
206-
udp6_requests: udp_server_stats.udp6_requests,
207-
udp6_connections_handled: udp_server_stats.udp6_connections_handled,
208-
udp6_announces_handled: udp_server_stats.udp6_announces_handled,
209-
udp6_scrapes_handled: udp_server_stats.udp6_scrapes_handled,
210-
udp6_responses: udp_server_stats.udp6_responses,
211-
udp6_errors_handled: udp_server_stats.udp6_errors_handled,
417+
udp6_requests,
418+
udp6_connections_handled,
419+
udp6_announces_handled,
420+
udp6_scrapes_handled,
421+
udp6_responses,
422+
udp6_errors_handled,
212423
}
213424
}
214425

packages/udp-tracker-server/src/statistics/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ use torrust_tracker_metrics::metric::description::MetricDescription;
88
use torrust_tracker_metrics::metric_name;
99
use torrust_tracker_metrics::unit::Unit;
1010

11-
const UDP_TRACKER_SERVER_REQUESTS_ABORTED_TOTAL: &str = "udp_tracker_server_requests_aborted_total";
12-
const UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL: &str = "udp_tracker_server_requests_banned_total";
13-
pub(crate) const UDP_TRACKER_SERVER_IPS_BANNED_TOTAL: &str = "udp_tracker_server_ips_banned_total";
14-
const UDP_TRACKER_SERVER_CONNECTION_ID_ERRORS_TOTAL: &str = "udp_tracker_server_connection_id_errors_total";
15-
const UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL: &str = "udp_tracker_server_requests_received_total";
11+
pub const UDP_TRACKER_SERVER_REQUESTS_ABORTED_TOTAL: &str = "udp_tracker_server_requests_aborted_total";
12+
pub const UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL: &str = "udp_tracker_server_requests_banned_total";
13+
pub const UDP_TRACKER_SERVER_IPS_BANNED_TOTAL: &str = "udp_tracker_server_ips_banned_total";
14+
pub const UDP_TRACKER_SERVER_CONNECTION_ID_ERRORS_TOTAL: &str = "udp_tracker_server_connection_id_errors_total";
15+
pub const UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL: &str = "udp_tracker_server_requests_received_total";
1616
pub const UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL: &str = "udp_tracker_server_requests_accepted_total";
17-
const UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL: &str = "udp_tracker_server_responses_sent_total";
18-
const UDP_TRACKER_SERVER_ERRORS_TOTAL: &str = "udp_tracker_server_errors_total";
19-
const UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS: &str = "udp_tracker_server_performance_avg_processing_time_ns";
17+
pub const UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL: &str = "udp_tracker_server_responses_sent_total";
18+
pub const UDP_TRACKER_SERVER_ERRORS_TOTAL: &str = "udp_tracker_server_errors_total";
19+
pub const UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS: &str = "udp_tracker_server_performance_avg_processing_time_ns";
2020

2121
#[must_use]
2222
pub fn describe_metrics() -> Metrics {

0 commit comments

Comments
 (0)