@@ -5,10 +5,16 @@ use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepo
55use bittorrent_udp_tracker_core:: services:: banning:: BanService ;
66use bittorrent_udp_tracker_core:: { self } ;
77use tokio:: sync:: RwLock ;
8+ use torrust_tracker_metrics:: label:: LabelSet ;
89use torrust_tracker_metrics:: metric_collection:: aggregate:: Sum ;
910use torrust_tracker_metrics:: metric_collection:: MetricCollection ;
1011use 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
1319use super :: metrics:: TorrentsMetrics ;
1420use 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,153 @@ async fn get_protocol_metrics(
132134}
133135
134136#[ allow( deprecated) ]
137+ #[ allow( clippy:: too_many_lines) ]
135138async 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+ /*
146+
147+ todo: We have to delete the global metrics from Metric types:
148+
149+ - bittorrent_http_tracker_core::statistics::metrics::Metrics
150+ - bittorrent_udp_tracker_core::statistics::metrics::Metrics
151+ - torrust_udp_tracker_server::statistics::metrics::Metrics
152+
153+ Internally only the labeled metrics should be used.
154+
155+ */
156+
157+ // TCPv4
158+
144159 #[ allow( clippy:: cast_sign_loss) ]
145160 #[ allow( clippy:: cast_possible_truncation) ]
146161 let tcp4_announces_handled = http_stats
147162 . metric_collection
148163 . sum (
149164 & 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)
165+ & [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "announce" ) ] . into ( ) ,
166+ )
167+ . unwrap_or_default ( )
168+ . value ( ) as u64 ;
169+
170+ #[ allow( clippy:: cast_sign_loss) ]
171+ #[ allow( clippy:: cast_possible_truncation) ]
172+ let tcp4_scrapes_handled = http_stats
173+ . metric_collection
174+ . sum (
175+ & metric_name ! ( HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL ) ,
176+ & [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "scrape" ) ] . into ( ) ,
177+ )
178+ . unwrap_or_default ( )
179+ . value ( ) as u64 ;
180+
181+ // TCPv6
182+
183+ #[ allow( clippy:: cast_sign_loss) ]
184+ #[ allow( clippy:: cast_possible_truncation) ]
185+ let tcp6_announces_handled = http_stats
186+ . metric_collection
187+ . sum (
188+ & metric_name ! ( HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL ) ,
189+ & [ ( "server_binding_address_ip_family" , "inet6" ) , ( "request_kind" , "announce" ) ] . into ( ) ,
190+ )
191+ . unwrap_or_default ( )
192+ . value ( ) as u64 ;
193+
194+ #[ allow( clippy:: cast_sign_loss) ]
195+ #[ allow( clippy:: cast_possible_truncation) ]
196+ let tcp6_scrapes_handled = http_stats
197+ . metric_collection
198+ . sum (
199+ & metric_name ! ( HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL ) ,
200+ & [ ( "server_binding_address_ip_family" , "inet6" ) , ( "request_kind" , "scrape" ) ] . into ( ) ,
201+ )
202+ . unwrap_or_default ( )
203+ . value ( ) as u64 ;
204+
205+ // UDP
206+
207+ #[ allow( clippy:: cast_sign_loss) ]
208+ #[ allow( clippy:: cast_possible_truncation) ]
209+ let udp_requests_aborted = udp_server_stats
210+ . metric_collection
211+ . sum ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ABORTED_TOTAL ) , & LabelSet :: empty ( ) )
212+ . unwrap_or_default ( )
213+ . value ( ) as u64 ;
214+
215+ #[ allow( clippy:: cast_sign_loss) ]
216+ #[ allow( clippy:: cast_possible_truncation) ]
217+ let udp_requests_banned = udp_server_stats
218+ . metric_collection
219+ . sum ( & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL ) , & LabelSet :: empty ( ) )
220+ . unwrap_or_default ( )
221+ . value ( ) as u64 ;
222+
223+ #[ allow( clippy:: cast_sign_loss) ]
224+ #[ allow( clippy:: cast_possible_truncation) ]
225+ let udp_banned_ips_total = udp_server_stats
226+ . metric_collection
227+ . sum ( & metric_name ! ( UDP_TRACKER_SERVER_IPS_BANNED_TOTAL ) , & LabelSet :: empty ( ) )
228+ . unwrap_or_default ( )
229+ . value ( ) as u64 ;
230+
231+ #[ allow( clippy:: cast_sign_loss) ]
232+ #[ allow( clippy:: cast_possible_truncation) ]
233+ let udp_avg_connect_processing_time_ns = udp_server_stats
234+ . metric_collection
235+ . sum (
236+ & metric_name ! ( UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS ) ,
237+ & [ ( "request_kind" , "connect" ) ] . into ( ) ,
238+ )
239+ . unwrap_or_default ( )
240+ . value ( ) as u64 ;
241+
242+ #[ allow( clippy:: cast_sign_loss) ]
243+ #[ allow( clippy:: cast_possible_truncation) ]
244+ let udp_avg_announce_processing_time_ns = udp_server_stats
245+ . metric_collection
246+ . sum (
247+ & metric_name ! ( UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS ) ,
248+ & [ ( "request_kind" , "announce" ) ] . into ( ) ,
249+ )
250+ . unwrap_or_default ( )
251+ . value ( ) as u64 ;
252+
253+ #[ allow( clippy:: cast_sign_loss) ]
254+ #[ allow( clippy:: cast_possible_truncation) ]
255+ let udp_avg_scrape_processing_time_ns = udp_server_stats
256+ . metric_collection
257+ . sum (
258+ & metric_name ! ( UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS ) ,
259+ & [ ( "request_kind" , "scrape" ) ] . into ( ) ,
260+ )
261+ . unwrap_or_default ( )
262+ . value ( ) as u64 ;
263+
264+ // UDPv4
265+
266+ #[ allow( clippy:: cast_sign_loss) ]
267+ #[ allow( clippy:: cast_possible_truncation) ]
268+ let udp4_requests = udp_server_stats
269+ . metric_collection
270+ . sum (
271+ & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL ) ,
272+ & [ ( "server_binding_address_ip_family" , "inet" ) ] . into ( ) ,
273+ )
274+ . unwrap_or_default ( )
275+ . value ( ) as u64 ;
276+
277+ #[ allow( clippy:: cast_sign_loss) ]
278+ #[ allow( clippy:: cast_possible_truncation) ]
279+ let udp4_connections_handled = udp_server_stats
280+ . metric_collection
281+ . sum (
282+ & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) ,
283+ & [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "connect" ) ] . into ( ) ,
151284 )
152285 . unwrap_or_default ( )
153286 . value ( ) as u64 ;
@@ -158,21 +291,111 @@ async fn get_protocol_metrics_from_labeled_metrics(
158291 . metric_collection
159292 . sum (
160293 & 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)
294+ & [ ( "server_binding_address_ip_family" , "inet" ) , ( " request_kind", "announce" ) ] . into ( ) ,
162295 )
163296 . unwrap_or_default ( )
164297 . value ( ) as u64 ;
165298
166- /*
299+ #[ allow( clippy:: cast_sign_loss) ]
300+ #[ allow( clippy:: cast_possible_truncation) ]
301+ let udp4_scrapes_handled = udp_server_stats
302+ . metric_collection
303+ . sum (
304+ & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) ,
305+ & [ ( "server_binding_address_ip_family" , "inet" ) , ( "request_kind" , "scrape" ) ] . into ( ) ,
306+ )
307+ . unwrap_or_default ( )
308+ . value ( ) as u64 ;
309+
310+ #[ allow( clippy:: cast_sign_loss) ]
311+ #[ allow( clippy:: cast_possible_truncation) ]
312+ let udp4_responses = udp_server_stats
313+ . metric_collection
314+ . sum (
315+ & metric_name ! ( UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL ) ,
316+ & [ ( "server_binding_address_ip_family" , "inet" ) ] . into ( ) ,
317+ )
318+ . unwrap_or_default ( )
319+ . value ( ) as u64 ;
320+
321+ #[ allow( clippy:: cast_sign_loss) ]
322+ #[ allow( clippy:: cast_possible_truncation) ]
323+ let udp4_errors_handled = udp_server_stats
324+ . metric_collection
325+ . sum (
326+ & metric_name ! ( UDP_TRACKER_SERVER_ERRORS_TOTAL ) ,
327+ & [ ( "server_binding_address_ip_family" , "inet" ) ] . into ( ) ,
328+ )
329+ . unwrap_or_default ( )
330+ . value ( ) as u64 ;
167331
168- todo:
332+ // UDPv6
169333
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.
334+ #[ allow( clippy:: cast_sign_loss) ]
335+ #[ allow( clippy:: cast_possible_truncation) ]
336+ let udp6_requests = udp_server_stats
337+ . metric_collection
338+ . sum (
339+ & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL ) ,
340+ & [ ( "server_binding_address_ip_family" , "inet6" ) ] . into ( ) ,
341+ )
342+ . unwrap_or_default ( )
343+ . value ( ) as u64 ;
174344
175- */
345+ #[ allow( clippy:: cast_sign_loss) ]
346+ #[ allow( clippy:: cast_possible_truncation) ]
347+ let udp6_connections_handled = udp_server_stats
348+ . metric_collection
349+ . sum (
350+ & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) ,
351+ & [ ( "server_binding_address_ip_family" , "inet6" ) , ( "request_kind" , "connect" ) ] . into ( ) ,
352+ )
353+ . unwrap_or_default ( )
354+ . value ( ) as u64 ;
355+
356+ #[ allow( clippy:: cast_sign_loss) ]
357+ #[ allow( clippy:: cast_possible_truncation) ]
358+ let udp6_announces_handled = udp_server_stats
359+ . metric_collection
360+ . sum (
361+ & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) ,
362+ & [ ( "server_binding_address_ip_family" , "inet6" ) , ( "request_kind" , "announce" ) ] . into ( ) ,
363+ )
364+ . unwrap_or_default ( )
365+ . value ( ) as u64 ;
366+
367+ #[ allow( clippy:: cast_sign_loss) ]
368+ #[ allow( clippy:: cast_possible_truncation) ]
369+ let udp6_scrapes_handled = udp_server_stats
370+ . metric_collection
371+ . sum (
372+ & metric_name ! ( UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL ) ,
373+ & [ ( "server_binding_address_ip_family" , "inet6" ) , ( "request_kind" , "scrape" ) ] . into ( ) ,
374+ )
375+ . unwrap_or_default ( )
376+ . value ( ) as u64 ;
377+
378+ #[ allow( clippy:: cast_sign_loss) ]
379+ #[ allow( clippy:: cast_possible_truncation) ]
380+ let udp6_responses = udp_server_stats
381+ . metric_collection
382+ . sum (
383+ & metric_name ! ( UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL ) ,
384+ & [ ( "server_binding_address_ip_family" , "inet6" ) ] . into ( ) ,
385+ )
386+ . unwrap_or_default ( )
387+ . value ( ) as u64 ;
388+
389+ #[ allow( clippy:: cast_sign_loss) ]
390+ #[ allow( clippy:: cast_possible_truncation) ]
391+ let udp6_errors_handled = udp_server_stats
392+ . metric_collection
393+ . sum (
394+ & metric_name ! ( UDP_TRACKER_SERVER_ERRORS_TOTAL ) ,
395+ & [ ( "server_binding_address_ip_family" , "inet6" ) ] . into ( ) ,
396+ )
397+ . unwrap_or_default ( )
398+ . value ( ) as u64 ;
176399
177400 // For backward compatibility we keep the `tcp4_connections_handled` and
178401 // `tcp6_connections_handled` metrics. They don't make sense for the HTTP
@@ -181,34 +404,34 @@ async fn get_protocol_metrics_from_labeled_metrics(
181404
182405 ProtocolMetrics {
183406 // TCPv4
184- tcp4_connections_handled : tcp4_announces_handled + http_stats . tcp4_scrapes_handled ,
407+ tcp4_connections_handled : tcp4_announces_handled + tcp4_scrapes_handled,
185408 tcp4_announces_handled,
186- tcp4_scrapes_handled : http_stats . tcp4_scrapes_handled ,
409+ tcp4_scrapes_handled,
187410 // 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 ,
411+ tcp6_connections_handled : tcp6_announces_handled + tcp6_scrapes_handled,
412+ tcp6_announces_handled,
413+ tcp6_scrapes_handled,
191414 // 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 ,
415+ udp_requests_aborted,
416+ udp_requests_banned,
417+ udp_banned_ips_total,
418+ udp_avg_connect_processing_time_ns,
419+ udp_avg_announce_processing_time_ns,
420+ udp_avg_scrape_processing_time_ns,
198421 // UDPv4
199- udp4_requests : udp_server_stats . udp4_requests ,
200- udp4_connections_handled : udp_server_stats . udp4_connections_handled ,
422+ udp4_requests,
423+ udp4_connections_handled,
201424 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 ,
425+ udp4_scrapes_handled,
426+ udp4_responses,
427+ udp4_errors_handled,
205428 // 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 ,
429+ udp6_requests,
430+ udp6_connections_handled,
431+ udp6_announces_handled,
432+ udp6_scrapes_handled,
433+ udp6_responses,
434+ udp6_errors_handled,
212435 }
213436}
214437
0 commit comments