Skip to content

Commit

Permalink
Merge #1130: Add more metrics to the UDP tracker stats
Browse files Browse the repository at this point in the history
6ca82e9 feat: [#1128] add new metric UDP total requests aborted (Jose Celano)
9499fd8 feat: [#1128] add new metric UDP total responses (Jose Celano)
286fe02 feat: [#1128] add new metric UDP total requests (Jose Celano)

Pull request description:

  Add more metrics to the UDP tracker stats. The new values are:

  - `udp4_requests`: total number of requests received from IPv4 clients.
  - `udp6_requests`: total number of requests received from IPv6 clients.
  - `udp4_responses`: total number of responses sent to IPv4 clients.
  - `udp6_responses`: total number of responses sent to IPv6 clients.
  - `udp_requests_aborted`: total number of requests aborted to make room in the active requests buffer.

  ### Notes

  - Responses sent might differ from requests received because of aborted requests.
  - When we [merge the IP ban service](#1124), we can add a new metric for the total number of IPs banned.
  - I want to add these new metrics to the [live demo Grafana dashboard](torrust/torrust-demo#20).

  ### Subtasks

  - [x] `udp4_requests`
  - [x] `udp6_requests`
  - [x] `udp4_responses`
  - [x] `udp6_responses`
  - [x] `udp_requests_aborted`
  - [x] Benchmarking to check how it affects performance before merging it.

ACKs for top commit:
  josecelano:
    ACK 6ca82e9

Tree-SHA512: 7fbf75b264b191f5c58fcecde8d5e783bbe54ee1c1799acdddc04a9ef64b7196d8b95d1bcad420b1df269bc7929e44417a1d164c6953b00804b0d1e5f0b36e7d
  • Loading branch information
josecelano committed Dec 16, 2024
2 parents 20639e8 + 6ca82e9 commit a7e20df
Show file tree
Hide file tree
Showing 8 changed files with 177 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/core/services/statistics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,26 @@ pub async fn get_metrics(tracker: Arc<Tracker>) -> TrackerMetrics {
TrackerMetrics {
torrents_metrics,
protocol_metrics: Metrics {
// TCP
tcp4_connections_handled: stats.tcp4_connections_handled,
tcp4_announces_handled: stats.tcp4_announces_handled,
tcp4_scrapes_handled: stats.tcp4_scrapes_handled,
tcp6_connections_handled: stats.tcp6_connections_handled,
tcp6_announces_handled: stats.tcp6_announces_handled,
tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
// UDP
udp_requests_aborted: stats.udp_requests_aborted,
udp4_requests: stats.udp4_requests,
udp4_connections_handled: stats.udp4_connections_handled,
udp4_announces_handled: stats.udp4_announces_handled,
udp4_scrapes_handled: stats.udp4_scrapes_handled,
udp4_responses: stats.udp4_responses,
udp4_errors_handled: stats.udp4_errors_handled,
udp6_requests: stats.udp6_requests,
udp6_connections_handled: stats.udp6_connections_handled,
udp6_announces_handled: stats.udp6_announces_handled,
udp6_scrapes_handled: stats.udp6_scrapes_handled,
udp6_responses: stats.udp6_responses,
udp6_errors_handled: stats.udp6_errors_handled,
},
}
Expand Down
66 changes: 66 additions & 0 deletions src/core/statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ pub enum Event {
Tcp4Scrape,
Tcp6Announce,
Tcp6Scrape,
Udp4RequestAborted,
Udp4Request,
Udp4Connect,
Udp4Announce,
Udp4Scrape,
Udp4Response,
Udp4Error,
Udp6Request,
Udp6Connect,
Udp6Announce,
Udp6Scrape,
Udp6Response,
Udp6Error,
}

Expand All @@ -72,26 +77,40 @@ pub struct Metrics {
pub tcp4_announces_handled: u64,
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
pub tcp4_scrapes_handled: u64,

/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
pub tcp6_connections_handled: u64,
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
pub tcp6_announces_handled: u64,
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
pub tcp6_scrapes_handled: u64,

/// Total number of UDP (UDP tracker) requests aborted.
pub udp_requests_aborted: u64,

/// Total number of UDP (UDP tracker) requests from IPv4 peers.
pub udp4_requests: u64,
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
pub udp4_connections_handled: u64,
/// Total number of UDP (UDP tracker) `announce` requests from IPv4 peers.
pub udp4_announces_handled: u64,
/// Total number of UDP (UDP tracker) `scrape` requests from IPv4 peers.
pub udp4_scrapes_handled: u64,
/// Total number of UDP (UDP tracker) responses from IPv4 peers.
pub udp4_responses: u64,
/// Total number of UDP (UDP tracker) `error` requests from IPv4 peers.
pub udp4_errors_handled: u64,

/// Total number of UDP (UDP tracker) requests from IPv6 peers.
pub udp6_requests: u64,
/// Total number of UDP (UDP tracker) `connection` requests from IPv6 peers.
pub udp6_connections_handled: u64,
/// Total number of UDP (UDP tracker) `announce` requests from IPv6 peers.
pub udp6_announces_handled: u64,
/// Total number of UDP (UDP tracker) `scrape` requests from IPv6 peers.
pub udp6_scrapes_handled: u64,
/// Total number of UDP (UDP tracker) responses from IPv6 peers.
pub udp6_responses: u64,
/// Total number of UDP (UDP tracker) `error` requests from IPv6 peers.
pub udp6_errors_handled: u64,
}
Expand Down Expand Up @@ -164,7 +183,15 @@ async fn event_handler(event: Event, stats_repository: &Repo) {
stats_repository.increase_tcp6_connections().await;
}

// UDP
Event::Udp4RequestAborted => {
stats_repository.increase_udp_requests_aborted().await;
}

// UDP4
Event::Udp4Request => {
stats_repository.increase_udp4_requests().await;
}
Event::Udp4Connect => {
stats_repository.increase_udp4_connections().await;
}
Expand All @@ -174,11 +201,17 @@ async fn event_handler(event: Event, stats_repository: &Repo) {
Event::Udp4Scrape => {
stats_repository.increase_udp4_scrapes().await;
}
Event::Udp4Response => {
stats_repository.increase_udp4_responses().await;
}
Event::Udp4Error => {
stats_repository.increase_udp4_errors().await;
}

// UDP6
Event::Udp6Request => {
stats_repository.increase_udp6_requests().await;
}
Event::Udp6Connect => {
stats_repository.increase_udp6_connections().await;
}
Expand All @@ -188,6 +221,9 @@ async fn event_handler(event: Event, stats_repository: &Repo) {
Event::Udp6Scrape => {
stats_repository.increase_udp6_scrapes().await;
}
Event::Udp6Response => {
stats_repository.increase_udp6_responses().await;
}
Event::Udp6Error => {
stats_repository.increase_udp6_errors().await;
}
Expand Down Expand Up @@ -276,6 +312,18 @@ impl Repo {
drop(stats_lock);
}

pub async fn increase_udp_requests_aborted(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp_requests_aborted += 1;
drop(stats_lock);
}

pub async fn increase_udp4_requests(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp4_requests += 1;
drop(stats_lock);
}

pub async fn increase_udp4_connections(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp4_connections_handled += 1;
Expand All @@ -294,12 +342,24 @@ impl Repo {
drop(stats_lock);
}

pub async fn increase_udp4_responses(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp4_responses += 1;
drop(stats_lock);
}

pub async fn increase_udp4_errors(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp4_errors_handled += 1;
drop(stats_lock);
}

pub async fn increase_udp6_requests(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp6_requests += 1;
drop(stats_lock);
}

pub async fn increase_udp6_connections(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp6_connections_handled += 1;
Expand All @@ -318,6 +378,12 @@ impl Repo {
drop(stats_lock);
}

pub async fn increase_udp6_responses(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp6_responses += 1;
drop(stats_lock);
}

pub async fn increase_udp6_errors(&self) {
let mut stats_lock = self.stats.write().await;
stats_lock.udp6_errors_handled += 1;
Expand Down
67 changes: 51 additions & 16 deletions src/servers/apis/v1/context/stats/resources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,40 @@ pub struct Stats {
pub tcp4_announces_handled: u64,
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
pub tcp4_scrapes_handled: u64,

/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
pub tcp6_connections_handled: u64,
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
pub tcp6_announces_handled: u64,
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
pub tcp6_scrapes_handled: u64,

/// Total number of UDP (UDP tracker) requests aborted.
pub udp_requests_aborted: u64,

/// Total number of UDP (UDP tracker) requests from IPv4 peers.
pub udp4_requests: u64,
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
pub udp4_connections_handled: u64,
/// Total number of UDP (UDP tracker) `announce` requests from IPv4 peers.
pub udp4_announces_handled: u64,
/// Total number of UDP (UDP tracker) `scrape` requests from IPv4 peers.
pub udp4_scrapes_handled: u64,
/// Total number of UDP (UDP tracker) responses from IPv4 peers.
pub udp4_responses: u64,
/// Total number of UDP (UDP tracker) `scrape` requests from IPv4 peers.
pub udp4_errors_handled: u64,

/// Total number of UDP (UDP tracker) requests from IPv6 peers.
pub udp6_requests: u64,
/// Total number of UDP (UDP tracker) `connection` requests from IPv6 peers.
pub udp6_connections_handled: u64,
/// Total number of UDP (UDP tracker) `announce` requests from IPv6 peers.
pub udp6_announces_handled: u64,
/// Total number of UDP (UDP tracker) `scrape` requests from IPv6 peers.
pub udp6_scrapes_handled: u64,
/// Total number of UDP (UDP tracker) responses from IPv6 peers.
pub udp6_responses: u64,
/// Total number of UDP (UDP tracker) `scrape` requests from IPv6 peers.
pub udp6_errors_handled: u64,
}
Expand All @@ -57,19 +71,26 @@ impl From<TrackerMetrics> for Stats {
seeders: metrics.torrents_metrics.complete,
completed: metrics.torrents_metrics.downloaded,
leechers: metrics.torrents_metrics.incomplete,
// TCP
tcp4_connections_handled: metrics.protocol_metrics.tcp4_connections_handled,
tcp4_announces_handled: metrics.protocol_metrics.tcp4_announces_handled,
tcp4_scrapes_handled: metrics.protocol_metrics.tcp4_scrapes_handled,
tcp6_connections_handled: metrics.protocol_metrics.tcp6_connections_handled,
tcp6_announces_handled: metrics.protocol_metrics.tcp6_announces_handled,
tcp6_scrapes_handled: metrics.protocol_metrics.tcp6_scrapes_handled,
// UDP
udp_requests_aborted: metrics.protocol_metrics.udp_requests_aborted,
udp4_requests: metrics.protocol_metrics.udp4_requests,
udp4_connections_handled: metrics.protocol_metrics.udp4_connections_handled,
udp4_announces_handled: metrics.protocol_metrics.udp4_announces_handled,
udp4_scrapes_handled: metrics.protocol_metrics.udp4_scrapes_handled,
udp4_responses: metrics.protocol_metrics.udp4_responses,
udp4_errors_handled: metrics.protocol_metrics.udp4_errors_handled,
udp6_requests: metrics.protocol_metrics.udp6_requests,
udp6_connections_handled: metrics.protocol_metrics.udp6_connections_handled,
udp6_announces_handled: metrics.protocol_metrics.udp6_announces_handled,
udp6_scrapes_handled: metrics.protocol_metrics.udp6_scrapes_handled,
udp6_responses: metrics.protocol_metrics.udp6_responses,
udp6_errors_handled: metrics.protocol_metrics.udp6_errors_handled,
}
}
Expand All @@ -94,41 +115,55 @@ mod tests {
torrents: 4
},
protocol_metrics: Metrics {
// TCP
tcp4_connections_handled: 5,
tcp4_announces_handled: 6,
tcp4_scrapes_handled: 7,
tcp6_connections_handled: 8,
tcp6_announces_handled: 9,
tcp6_scrapes_handled: 10,
udp4_connections_handled: 11,
udp4_announces_handled: 12,
udp4_scrapes_handled: 13,
udp4_errors_handled: 14,
udp6_connections_handled: 15,
udp6_announces_handled: 16,
udp6_scrapes_handled: 17,
udp6_errors_handled: 18
// UDP
udp_requests_aborted: 11,
udp4_requests: 12,
udp4_connections_handled: 13,
udp4_announces_handled: 14,
udp4_scrapes_handled: 15,
udp4_responses: 16,
udp4_errors_handled: 17,
udp6_requests: 18,
udp6_connections_handled: 19,
udp6_announces_handled: 20,
udp6_scrapes_handled: 21,
udp6_responses: 22,
udp6_errors_handled: 23
}
}),
Stats {
torrents: 4,
seeders: 1,
completed: 2,
leechers: 3,
// TCP
tcp4_connections_handled: 5,
tcp4_announces_handled: 6,
tcp4_scrapes_handled: 7,
tcp6_connections_handled: 8,
tcp6_announces_handled: 9,
tcp6_scrapes_handled: 10,
udp4_connections_handled: 11,
udp4_announces_handled: 12,
udp4_scrapes_handled: 13,
udp4_errors_handled: 14,
udp6_connections_handled: 15,
udp6_announces_handled: 16,
udp6_scrapes_handled: 17,
udp6_errors_handled: 18
// UDP
udp_requests_aborted: 11,
udp4_requests: 12,
udp4_connections_handled: 13,
udp4_announces_handled: 14,
udp4_scrapes_handled: 15,
udp4_responses: 16,
udp4_errors_handled: 17,
udp6_requests: 18,
udp6_connections_handled: 19,
udp6_announces_handled: 20,
udp6_scrapes_handled: 21,
udp6_responses: 22,
udp6_errors_handled: 23
}
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/servers/apis/v1/context/stats/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
tracker_metrics.protocol_metrics.tcp6_scrapes_handled
));

lines.push(format!(
"udp_requests_aborted {}",
tracker_metrics.protocol_metrics.udp_requests_aborted
));

lines.push(format!("udp4_requests {}", tracker_metrics.protocol_metrics.udp4_requests));
lines.push(format!(
"udp4_connections_handled {}",
tracker_metrics.protocol_metrics.udp4_connections_handled
Expand All @@ -59,11 +65,13 @@ pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
"udp4_scrapes_handled {}",
tracker_metrics.protocol_metrics.udp4_scrapes_handled
));
lines.push(format!("udp4_responses {}", tracker_metrics.protocol_metrics.udp4_responses));
lines.push(format!(
"udp4_errors_handled {}",
tracker_metrics.protocol_metrics.udp4_errors_handled
));

lines.push(format!("udp6_requests {}", tracker_metrics.protocol_metrics.udp6_requests));
lines.push(format!(
"udp6_connections_handled {}",
tracker_metrics.protocol_metrics.udp6_connections_handled
Expand All @@ -76,6 +84,7 @@ pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
"udp6_scrapes_handled {}",
tracker_metrics.protocol_metrics.udp6_scrapes_handled
));
lines.push(format!("udp6_responses {}", tracker_metrics.protocol_metrics.udp6_responses));
lines.push(format!(
"udp6_errors_handled {}",
tracker_metrics.protocol_metrics.udp6_errors_handled
Expand Down
Loading

0 comments on commit a7e20df

Please sign in to comment.