1+ use torrust_tracker_metrics:: label:: LabelSet ;
2+ use torrust_tracker_metrics:: metric:: MetricName ;
3+ use torrust_tracker_primitives:: DurationSinceUnixEpoch ;
4+
15use crate :: event:: Event ;
26use crate :: statistics:: repository:: Repository ;
37
48/// # Panics
59///
610/// This function panics if the IP version does not match the event type.
7- pub async fn handle_event ( event : Event , stats_repository : & Repository ) {
11+ pub async fn handle_event ( event : Event , stats_repository : & Repository , now : DurationSinceUnixEpoch ) {
812 match event {
9- Event :: UdpConnect { context } => match context. client_socket_addr . ip ( ) {
10- std:: net:: IpAddr :: V4 ( _) => {
11- stats_repository. increase_udp4_connections ( ) . await ;
12- }
13- std:: net:: IpAddr :: V6 ( _) => {
14- stats_repository. increase_udp6_connections ( ) . await ;
15- }
16- } ,
17- Event :: UdpAnnounce { context } => match context. client_socket_addr . ip ( ) {
18- std:: net:: IpAddr :: V4 ( _) => {
19- stats_repository. increase_udp4_announces ( ) . await ;
20- }
21- std:: net:: IpAddr :: V6 ( _) => {
22- stats_repository. increase_udp6_announces ( ) . await ;
13+ Event :: UdpConnect { context } => {
14+ // Global fixed metrics
15+
16+ match context. client_socket_addr . ip ( ) {
17+ std:: net:: IpAddr :: V4 ( _) => {
18+ stats_repository. increase_udp4_connections ( ) . await ;
19+ }
20+ std:: net:: IpAddr :: V6 ( _) => {
21+ stats_repository. increase_udp6_connections ( ) . await ;
22+ }
2323 }
24- } ,
25- Event :: UdpScrape { context } => match context. client_socket_addr . ip ( ) {
26- std:: net:: IpAddr :: V4 ( _) => {
27- stats_repository. increase_udp4_scrapes ( ) . await ;
24+
25+ // Extendable metrics
26+
27+ stats_repository
28+ . increase_counter (
29+ & MetricName :: new ( "udp_tracker_core_connect_requests_received_total" ) ,
30+ & LabelSet :: from ( context) ,
31+ now,
32+ )
33+ . await ;
34+ }
35+ Event :: UdpAnnounce { context } => {
36+ // Global fixed metrics
37+
38+ match context. client_socket_addr . ip ( ) {
39+ std:: net:: IpAddr :: V4 ( _) => {
40+ stats_repository. increase_udp4_announces ( ) . await ;
41+ }
42+ std:: net:: IpAddr :: V6 ( _) => {
43+ stats_repository. increase_udp6_announces ( ) . await ;
44+ }
2845 }
29- std:: net:: IpAddr :: V6 ( _) => {
30- stats_repository. increase_udp6_scrapes ( ) . await ;
46+
47+ // Extendable metrics
48+
49+ stats_repository
50+ . increase_counter (
51+ & MetricName :: new ( "udp_tracker_core_announce_requests_received_total" ) ,
52+ & LabelSet :: from ( context) ,
53+ now,
54+ )
55+ . await ;
56+ }
57+ Event :: UdpScrape { context } => {
58+ // Global fixed metrics
59+
60+ match context. client_socket_addr . ip ( ) {
61+ std:: net:: IpAddr :: V4 ( _) => {
62+ stats_repository. increase_udp4_scrapes ( ) . await ;
63+ }
64+ std:: net:: IpAddr :: V6 ( _) => {
65+ stats_repository. increase_udp6_scrapes ( ) . await ;
66+ }
3167 }
32- } ,
68+
69+ // Extendable metrics
70+
71+ stats_repository
72+ . increase_counter (
73+ & MetricName :: new ( "udp_tracker_core_scrape_requests_received_total" ) ,
74+ & LabelSet :: from ( context) ,
75+ now,
76+ )
77+ . await ;
78+ }
3379 }
3480
3581 tracing:: debug!( "stats: {:?}" , stats_repository. get_stats( ) . await ) ;
@@ -39,11 +85,13 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
3985mod tests {
4086 use std:: net:: { IpAddr , Ipv4Addr , Ipv6Addr , SocketAddr } ;
4187
88+ use torrust_tracker_clock:: clock:: Time ;
4289 use torrust_tracker_primitives:: service_binding:: { Protocol , ServiceBinding } ;
4390
4491 use crate :: event:: { ConnectionContext , Event } ;
4592 use crate :: statistics:: event:: handler:: handle_event;
4693 use crate :: statistics:: repository:: Repository ;
94+ use crate :: CurrentClock ;
4795
4896 #[ tokio:: test]
4997 async fn should_increase_the_udp4_connections_counter_when_it_receives_a_udp4_connect_event ( ) {
@@ -61,6 +109,7 @@ mod tests {
61109 ) ,
62110 } ,
63111 & stats_repository,
112+ CurrentClock :: now ( ) ,
64113 )
65114 . await ;
66115
@@ -85,6 +134,7 @@ mod tests {
85134 ) ,
86135 } ,
87136 & stats_repository,
137+ CurrentClock :: now ( ) ,
88138 )
89139 . await ;
90140
@@ -109,6 +159,7 @@ mod tests {
109159 ) ,
110160 } ,
111161 & stats_repository,
162+ CurrentClock :: now ( ) ,
112163 )
113164 . await ;
114165
@@ -133,6 +184,7 @@ mod tests {
133184 ) ,
134185 } ,
135186 & stats_repository,
187+ CurrentClock :: now ( ) ,
136188 )
137189 . await ;
138190
@@ -157,6 +209,7 @@ mod tests {
157209 ) ,
158210 } ,
159211 & stats_repository,
212+ CurrentClock :: now ( ) ,
160213 )
161214 . await ;
162215
@@ -181,6 +234,7 @@ mod tests {
181234 ) ,
182235 } ,
183236 & stats_repository,
237+ CurrentClock :: now ( ) ,
184238 )
185239 . await ;
186240
0 commit comments