Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions packages/axum-http-tracker-server/tests/server/v1/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ mod for_all_config_modes {

let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;

assert_eq!(stats.tcp4_announces_handled, 1);
assert_eq!(stats.tcp4_announces_handled(), 1);

drop(stats);

Expand All @@ -730,7 +730,7 @@ mod for_all_config_modes {

let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;

assert_eq!(stats.tcp6_announces_handled, 1);
assert_eq!(stats.tcp6_announces_handled(), 1);

drop(stats);

Expand All @@ -755,7 +755,7 @@ mod for_all_config_modes {

let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;

assert_eq!(stats.tcp6_announces_handled, 0);
assert_eq!(stats.tcp6_announces_handled(), 0);

drop(stats);

Expand Down Expand Up @@ -1149,7 +1149,7 @@ mod for_all_config_modes {

let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;

assert_eq!(stats.tcp4_scrapes_handled, 1);
assert_eq!(stats.tcp4_scrapes_handled(), 1);

drop(stats);

Expand Down Expand Up @@ -1181,7 +1181,7 @@ mod for_all_config_modes {

let stats = env.container.http_tracker_core_container.stats_repository.get_stats().await;

assert_eq!(stats.tcp6_scrapes_handled, 1);
assert_eq!(stats.tcp6_scrapes_handled(), 1);

drop(stats);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,13 @@ pub struct QueryParams {
pub async fn get_stats_handler(
State(state): State<(
Arc<InMemoryTorrentRepository>,
Arc<RwLock<BanService>>,
Arc<bittorrent_tracker_core::statistics::repository::Repository>,
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
)>,
params: Query<QueryParams>,
) -> Response {
let metrics = get_metrics(
state.0.clone(),
state.1.clone(),
state.2.clone(),
state.3.clone(),
state.4.clone(),
)
.await;
let metrics = get_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;

match params.0.format {
Some(format) => match format {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
&format!("{prefix}/stats"),
get(get_stats_handler).with_state((
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
http_api_container.ban_service.clone(),
http_api_container.tracker_core_container.stats_repository.clone(),
http_api_container.http_stats_repository.clone(),
http_api_container.udp_server_stats_repository.clone(),
Expand Down
39 changes: 6 additions & 33 deletions packages/http-tracker-core/src/statistics/event/handler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::net::IpAddr;
use std::sync::Arc;

use torrust_tracker_metrics::label::{LabelSet, LabelValue};
Expand All @@ -12,19 +11,6 @@ use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now: DurationSinceUnixEpoch) {
match event {
Event::TcpAnnounce { connection, .. } => {
// Global fixed metrics

match connection.client_ip_addr() {
IpAddr::V4(_) => {
stats_repository.increase_tcp4_announces().await;
}
IpAddr::V6(_) => {
stats_repository.increase_tcp6_announces().await;
}
}

// Extendable metrics

let mut label_set = LabelSet::from(connection);
label_set.upsert(label_name!("request_kind"), LabelValue::new("announce"));

Expand All @@ -42,19 +28,6 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
};
}
Event::TcpScrape { connection } => {
// Global fixed metrics

match connection.client_ip_addr() {
IpAddr::V4(_) => {
stats_repository.increase_tcp4_scrapes().await;
}
IpAddr::V6(_) => {
stats_repository.increase_tcp6_scrapes().await;
}
}

// Extendable metrics

let mut label_set = LabelSet::from(connection);
label_set.upsert(label_name!("request_kind"), LabelValue::new("scrape"));

Expand Down Expand Up @@ -113,7 +86,7 @@ mod tests {

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp4_announces_handled, 1);
assert_eq!(stats.tcp4_announces_handled(), 1);
}

#[tokio::test]
Expand All @@ -137,7 +110,7 @@ mod tests {

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp4_scrapes_handled, 1);
assert_eq!(stats.tcp4_scrapes_handled(), 1);
}

#[tokio::test]
Expand All @@ -150,7 +123,7 @@ mod tests {
Event::TcpAnnounce {
connection: ConnectionContext::new(
RemoteClientAddr::new(ResolvedIp::FromSocketAddr(remote_client_ip), Some(8080)),
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 7070)).unwrap(),
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 7070)).unwrap(),
),
info_hash: sample_info_hash(),
announcement: peer,
Expand All @@ -162,7 +135,7 @@ mod tests {

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp6_announces_handled, 1);
assert_eq!(stats.tcp6_announces_handled(), 1);
}

#[tokio::test]
Expand All @@ -178,7 +151,7 @@ mod tests {
))),
Some(8080),
),
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V4(Ipv4Addr::LOCALHOST), 7070)).unwrap(),
ServiceBinding::new(Protocol::HTTP, SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 7070)).unwrap(),
),
},
&stats_repository,
Expand All @@ -188,6 +161,6 @@ mod tests {

let stats = stats_repository.get_stats().await;

assert_eq!(stats.tcp6_scrapes_handled, 1);
assert_eq!(stats.tcp6_scrapes_handled(), 1);
}
}
74 changes: 62 additions & 12 deletions packages/http-tracker-core/src/statistics/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
use serde::Serialize;
use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric::MetricName;
use torrust_tracker_metrics::metric_collection::aggregate::Sum;
use torrust_tracker_metrics::metric_collection::{Error, MetricCollection};
use torrust_tracker_metrics::metric_name;
use torrust_tracker_primitives::DurationSinceUnixEpoch;

use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;

/// Metrics collected by the tracker.
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
pub struct Metrics {
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
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) `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,

/// A collection of metrics.
pub metric_collection: MetricCollection,
}
Expand Down Expand Up @@ -49,3 +41,61 @@ impl Metrics {
self.metric_collection.set_gauge(metric_name, labels, value, now)
}
}

impl Metrics {
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp4_announces_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet"), ("request_kind", "announce")].into(),
)
.unwrap_or_default()
.value() as u64
}

/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp4_scrapes_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet"), ("request_kind", "scrape")].into(),
)
.unwrap_or_default()
.value() as u64
}

/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp6_announces_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "announce")].into(),
)
.unwrap_or_default()
.value() as u64
}

/// Total number of TCP (HTTP tracker) `scrape` requests from IPv6 peers.
#[must_use]
#[allow(clippy::cast_sign_loss)]
#[allow(clippy::cast_possible_truncation)]
pub fn tcp6_scrapes_handled(&self) -> u64 {
self.metric_collection
.sum(
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
&[("server_binding_address_ip_family", "inet6"), ("request_kind", "scrape")].into(),
)
.unwrap_or_default()
.value() as u64
}
}
1 change: 0 additions & 1 deletion packages/http-tracker-core/src/statistics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pub mod event;
pub mod metrics;
pub mod repository;
pub mod services;

use metrics::Metrics;
use torrust_tracker_metrics::metric::description::MetricDescription;
Expand Down
24 changes: 0 additions & 24 deletions packages/http-tracker-core/src/statistics/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,6 @@ impl Repository {
self.stats.read().await
}

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

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

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

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

/// # Errors
///
/// This function will return an error if the metric collection fails to
Expand Down
Loading