Skip to content

Commit 2f100ba

Browse files
committed
feat(udp-server): change connection ID error log level from ERROR to WARNING
Connection cookie errors (expired/from-future) are caused by bad client traffic, not application errors. Logging them at ERROR level floods the logs, making it hard to identify real issues. Now inspects the error variant: - Connection cookie errors: logged at WARN - All other UDP errors: remain at ERROR Also updated the test helper log filter from ERROR to WARN so the captured test logs include WARN messages for assertions.
1 parent 272219d commit 2f100ba

3 files changed

Lines changed: 34 additions & 9 deletions

File tree

packages/test-helpers/src/logging.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn captured_logs_buffer() -> &'static Mutex<CircularBuffer> {
1818

1919
pub fn setup() {
2020
INIT.call_once(|| {
21-
tracing_init(LevelFilter::ERROR, &TraceStyle::Default);
21+
tracing_init(LevelFilter::WARN, &TraceStyle::Default);
2222
});
2323
}
2424

packages/udp-server/src/handlers/error.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use std::net::SocketAddr;
33
use std::ops::Range;
44

55
use torrust_net_primitives::service_binding::ServiceBinding;
6+
use torrust_tracker_udp_core::services::announce::UdpAnnounceError;
7+
use torrust_tracker_udp_core::services::scrape::UdpScrapeError;
68
use torrust_tracker_udp_core::{self, UDP_TRACKER_LOG_TARGET};
79
use torrust_tracker_udp_protocol::{ErrorResponse, Response, TransactionId};
810
use tracing::{Level, instrument};
@@ -52,17 +54,40 @@ fn log_error(
5254
opt_transaction_id: Option<TransactionId>,
5355
request_id: Uuid,
5456
) {
55-
match opt_transaction_id {
56-
Some(transaction_id) => {
57-
let transaction_id = transaction_id.0.to_string();
58-
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, %transaction_id, "response error");
57+
if is_connection_cookie_error(error) {
58+
match opt_transaction_id {
59+
Some(transaction_id) => {
60+
let transaction_id = transaction_id.0.to_string();
61+
tracing::warn!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, %transaction_id, "response error");
62+
}
63+
None => {
64+
tracing::warn!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, "response error");
65+
}
5966
}
60-
None => {
61-
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, "response error");
67+
} else {
68+
match opt_transaction_id {
69+
Some(transaction_id) => {
70+
let transaction_id = transaction_id.0.to_string();
71+
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, %transaction_id, "response error");
72+
}
73+
None => {
74+
tracing::error!(target: UDP_TRACKER_LOG_TARGET, error = %error, %client_socket_addr, %server_socket_addr, %request_id, "response error");
75+
}
6276
}
6377
}
6478
}
6579

80+
fn is_connection_cookie_error(error: &Error) -> bool {
81+
matches!(
82+
error,
83+
Error::AnnounceFailed {
84+
source: UdpAnnounceError::ConnectionCookieError { .. }
85+
} | Error::ScrapeFailed {
86+
source: UdpScrapeError::ConnectionCookieError { .. }
87+
}
88+
)
89+
}
90+
6691
async fn trigger_udp_error_event(
6792
error: &Error,
6893
client_socket_addr: SocketAddr,

packages/udp-server/tests/server/contract.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,8 +275,8 @@ mod receiving_an_announce_request {
275275
let transaction_id = tx_id.0.to_string();
276276

277277
assert!(
278-
logs_contains_a_line_with(&["ERROR", "UDP TRACKER", &transaction_id]),
279-
"Expected logs to contain: ERROR ... UDP TRACKER ... transaction_id={transaction_id}"
278+
logs_contains_a_line_with(&["WARN", "UDP TRACKER", &transaction_id]),
279+
"Expected logs to contain: WARN ... UDP TRACKER ... transaction_id={transaction_id}"
280280
);
281281
}
282282

0 commit comments

Comments
 (0)