11use std:: sync:: Arc ;
22
3+ use bittorrent_http_tracker_core:: statistics:: HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL ;
34use bittorrent_tracker_core:: torrent:: repository:: in_memory:: InMemoryTorrentRepository ;
45use bittorrent_udp_tracker_core:: services:: banning:: BanService ;
56use bittorrent_udp_tracker_core:: { self } ;
67use tokio:: sync:: RwLock ;
8+ use torrust_tracker_metrics:: metric_collection:: aggregate:: Sum ;
79use torrust_tracker_metrics:: metric_collection:: MetricCollection ;
8- use torrust_udp_tracker_server:: statistics as udp_server_statistics;
10+ use torrust_tracker_metrics:: metric_name;
11+ use torrust_udp_tracker_server:: statistics:: { self as udp_server_statistics, UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL } ;
912
1013use super :: metrics:: TorrentsMetrics ;
1114use crate :: statistics:: metrics:: ProtocolMetrics ;
@@ -32,9 +35,38 @@ pub async fn get_metrics(
3235 http_stats_repository : Arc < bittorrent_http_tracker_core:: statistics:: repository:: Repository > ,
3336 udp_server_stats_repository : Arc < udp_server_statistics:: repository:: Repository > ,
3437) -> TrackerMetrics {
38+ let protocol_metrics_from_global_metrics = get_protocol_metrics (
39+ ban_service. clone ( ) ,
40+ http_stats_repository. clone ( ) ,
41+ udp_server_stats_repository. clone ( ) ,
42+ )
43+ . await ;
44+
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+
52+ // todo:
53+ // We keep both metrics until we deploy to production and we can
54+ // ensure that the protocol metrics from labeled metrics are correct.
55+ // After that we can remove the `get_protocol_metrics` function and
56+ // use only the `get_protocol_metrics_from_labeled_metrics` function.
57+ // And also remove the code in repositories to generate the global metrics.
58+ let protocol_metrics = if protocol_metrics_from_global_metrics == protocol_metrics_from_labeled_metrics {
59+ protocol_metrics_from_labeled_metrics
60+ } 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);
64+ protocol_metrics_from_global_metrics
65+ } ;
66+
3567 TrackerMetrics {
3668 torrents_metrics : get_torrents_metrics ( in_memory_torrent_repository, tracker_core_stats_repository) . await ,
37- protocol_metrics : get_protocol_metrics ( ban_service , http_stats_repository , udp_server_stats_repository ) . await ,
69+ protocol_metrics,
3870 }
3971}
4072
@@ -99,6 +131,87 @@ async fn get_protocol_metrics(
99131 }
100132}
101133
134+ #[ allow( deprecated) ]
135+ async fn get_protocol_metrics_from_labeled_metrics (
136+ ban_service : Arc < RwLock < BanService > > ,
137+ http_stats_repository : Arc < bittorrent_http_tracker_core:: statistics:: repository:: Repository > ,
138+ udp_server_stats_repository : Arc < udp_server_statistics:: repository:: Repository > ,
139+ ) -> ProtocolMetrics {
140+ let udp_banned_ips_total = ban_service. read ( ) . await . get_banned_ips_total ( ) ;
141+ let http_stats = http_stats_repository. get_stats ( ) . await ;
142+ let udp_server_stats = udp_server_stats_repository. get_stats ( ) . await ;
143+
144+ #[ allow( clippy:: cast_sign_loss) ]
145+ #[ allow( clippy:: cast_possible_truncation) ]
146+ let tcp4_announces_handled = http_stats
147+ . metric_collection
148+ . sum (
149+ & 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)
151+ )
152+ . unwrap_or_default ( )
153+ . value ( ) as u64 ;
154+
155+ #[ allow( clippy:: cast_sign_loss) ]
156+ #[ allow( clippy:: cast_possible_truncation) ]
157+ let udp4_announces_handled = udp_server_stats
158+ . metric_collection
159+ . sum (
160+ & 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)
162+ )
163+ . unwrap_or_default ( )
164+ . value ( ) as u64 ;
165+
166+ /*
167+
168+ todo:
169+
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.
174+
175+ */
176+
177+ // For backward compatibility we keep the `tcp4_connections_handled` and
178+ // `tcp6_connections_handled` metrics. They don't make sense for the HTTP
179+ // tracker, but we keep them for now. In new major versions we should remove
180+ // them.
181+
182+ ProtocolMetrics {
183+ // TCPv4
184+ tcp4_connections_handled : tcp4_announces_handled + http_stats. tcp4_scrapes_handled ,
185+ tcp4_announces_handled,
186+ tcp4_scrapes_handled : http_stats. tcp4_scrapes_handled ,
187+ // 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 ,
191+ // 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 ,
198+ // UDPv4
199+ udp4_requests : udp_server_stats. udp4_requests ,
200+ udp4_connections_handled : udp_server_stats. udp4_connections_handled ,
201+ 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 ,
205+ // 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 ,
212+ }
213+ }
214+
102215#[ derive( Debug , PartialEq ) ]
103216pub struct TrackerLabeledMetrics {
104217 pub metrics : MetricCollection ,
0 commit comments