Skip to content

Commit

Permalink
feat: [#704] add latency to UDP tracker logs
Browse files Browse the repository at this point in the history
Example:

```
2024-02-23T08:24:50.137064143+00:00 [UDP TRACKER][INFO] request; server_socket_addr=0.0.0.0:6969 action=ANNOUNCE transaction_id=-888840697 request_id=c38ab102-3ad1-48d3-8f2e-e03190d6e592 connection_id=4792797915217963415 info_hash=9c38422213e30bff212b30c360d26f9a02136422
2024-02-23T08:24:50.137075433+00:00 [UDP TRACKER][INFO] response; server_socket_addr=0.0.0.0:6969 transaction_id=-888840697 request_id=c38ab102-3ad1-48d3-8f2e-e03190d6e592 latency_ms=0
```
  • Loading branch information
josecelano committed Feb 23, 2024
1 parent 5e5a9aa commit 5a6c968
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
7 changes: 6 additions & 1 deletion src/servers/udp/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::panic::Location;
use std::sync::Arc;
use std::time::Instant;

use aquatic_udp_protocol::{
AnnounceInterval, AnnounceRequest, AnnounceResponse, ConnectRequest, ConnectResponse, ErrorResponse, NumberOfDownloads,
Expand Down Expand Up @@ -35,6 +36,8 @@ use crate::shared::bit_torrent::info_hash::InfoHash;
pub(crate) async fn handle_packet(udp_request: UdpRequest, tracker: &Arc<Tracker>, socket: Arc<UdpSocket>) -> Response {
debug!("Handling Packets: {udp_request:?}");

let start_time = Instant::now();

let request_id = RequestId::make(&udp_request);
let server_socket_addr = socket.local_addr().expect("Could not get local_addr for socket.");

Expand All @@ -58,7 +61,9 @@ pub(crate) async fn handle_packet(udp_request: UdpRequest, tracker: &Arc<Tracker
Err(e) => handle_error(&e, transaction_id),
};

log_response(&response, &transaction_id, &request_id, &server_socket_addr);
let latency = start_time.elapsed();

log_response(&response, &transaction_id, &request_id, &server_socket_addr, latency);

response
}
Expand Down
17 changes: 15 additions & 2 deletions src/servers/udp/logging.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Logging for UDP Tracker requests and responses.

use std::net::SocketAddr;
use std::time::Duration;

use aquatic_udp_protocol::{Request, Response, TransactionId};

Expand Down Expand Up @@ -36,7 +37,13 @@ pub fn log_request(request: &Request, request_id: &RequestId, server_socket_addr

tracing::span!(
target: "UDP TRACKER",
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);
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);
}
};
}
Expand All @@ -54,10 +61,16 @@ pub fn log_response(
transaction_id: &TransactionId,
request_id: &RequestId,
server_socket_addr: &SocketAddr,
latency: Duration,
) {
tracing::span!(
target: "UDP TRACKER",
tracing::Level::INFO, "response", server_socket_addr = %server_socket_addr, transaction_id = %transaction_id.0.to_string(), request_id = %request_id);
tracing::Level::INFO,
"response",
server_socket_addr = %server_socket_addr,
transaction_id = %transaction_id.0.to_string(),
request_id = %request_id,
latency_ms = %latency.as_millis());
}

pub fn log_bad_request(request_id: &RequestId) {
Expand Down

0 comments on commit 5a6c968

Please sign in to comment.