torrust_tracker/servers/udp/
logging.rs

1//! Logging for UDP Tracker requests and responses.
2
3use std::net::SocketAddr;
4use std::time::Duration;
5
6use aquatic_udp_protocol::{Request, Response, TransactionId};
7use torrust_tracker_primitives::info_hash::InfoHash;
8
9use super::handlers::RequestId;
10use crate::servers::udp::UDP_TRACKER_LOG_TARGET;
11
12pub fn log_request(request: &Request, request_id: &RequestId, server_socket_addr: &SocketAddr) {
13    let action = map_action_name(request);
14
15    match &request {
16        Request::Connect(connect_request) => {
17            let transaction_id = connect_request.transaction_id;
18            let transaction_id_str = transaction_id.0.to_string();
19
20            tracing::span!(
21                target: UDP_TRACKER_LOG_TARGET,
22                tracing::Level::INFO, "request", server_socket_addr = %server_socket_addr, action = %action, transaction_id = %transaction_id_str, request_id = %request_id);
23        }
24        Request::Announce(announce_request) => {
25            let transaction_id = announce_request.transaction_id;
26            let transaction_id_str = transaction_id.0.to_string();
27            let connection_id_str = announce_request.connection_id.0.to_string();
28            let info_hash_str = InfoHash::from_bytes(&announce_request.info_hash.0).to_hex_string();
29
30            tracing::span!(
31                target: UDP_TRACKER_LOG_TARGET,
32                tracing::Level::INFO, "request", server_socket_addr = %server_socket_addr, action = %action, transaction_id = %transaction_id_str, request_id = %request_id, connection_id = %connection_id_str, info_hash = %info_hash_str);
33        }
34        Request::Scrape(scrape_request) => {
35            let transaction_id = scrape_request.transaction_id;
36            let transaction_id_str = transaction_id.0.to_string();
37            let connection_id_str = scrape_request.connection_id.0.to_string();
38
39            tracing::span!(
40                target: UDP_TRACKER_LOG_TARGET,
41                tracing::Level::INFO,
42                "request",
43                server_socket_addr = %server_socket_addr,
44                action = %action,
45                transaction_id = %transaction_id_str,
46                request_id = %request_id,
47                connection_id = %connection_id_str);
48        }
49    };
50}
51
52fn map_action_name(udp_request: &Request) -> String {
53    match udp_request {
54        Request::Connect(_connect_request) => "CONNECT".to_owned(),
55        Request::Announce(_announce_request) => "ANNOUNCE".to_owned(),
56        Request::Scrape(_scrape_request) => "SCRAPE".to_owned(),
57    }
58}
59
60pub fn log_response(
61    _response: &Response,
62    transaction_id: &TransactionId,
63    request_id: &RequestId,
64    server_socket_addr: &SocketAddr,
65    latency: Duration,
66) {
67    tracing::span!(
68        target: UDP_TRACKER_LOG_TARGET,
69        tracing::Level::INFO, 
70        "response", 
71        server_socket_addr = %server_socket_addr, 
72        transaction_id = %transaction_id.0.to_string(), 
73        request_id = %request_id,
74        latency_ms = %latency.as_millis());
75}
76
77pub fn log_bad_request(request_id: &RequestId) {
78    tracing::span!(
79        target: UDP_TRACKER_LOG_TARGET,
80        tracing::Level::INFO, "bad request", request_id = %request_id);
81}
82
83pub fn log_error_response(request_id: &RequestId) {
84    tracing::span!(
85        target: UDP_TRACKER_LOG_TARGET,
86        tracing::Level::INFO, "response", request_id = %request_id);
87}