Skip to content

Commit 35d248a

Browse files
authored
Merge branch 'torrust:develop' into develop
2 parents 3d87fc3 + 6a22b1e commit 35d248a

File tree

28 files changed

+213
-366
lines changed

28 files changed

+213
-366
lines changed

packages/axum-http-tracker-server/tests/server/v1/contract.rs

-85
Original file line numberDiff line numberDiff line change
@@ -666,91 +666,6 @@ mod for_all_config_modes {
666666
compact_announce.is_ok()
667667
}
668668

669-
#[tokio::test]
670-
async fn should_increase_the_number_of_tcp4_connections_handled_in_statistics() {
671-
logging::setup();
672-
673-
let env = Started::new(&configuration::ephemeral_public().into()).await;
674-
675-
Client::new(*env.bind_address())
676-
.announce(&QueryBuilder::default().query())
677-
.await;
678-
679-
let stats = env
680-
.container
681-
.http_tracker_core_container
682-
.http_stats_repository
683-
.get_stats()
684-
.await;
685-
686-
assert_eq!(stats.tcp4_connections_handled, 1);
687-
688-
drop(stats);
689-
690-
env.stop().await;
691-
}
692-
693-
#[tokio::test]
694-
async fn should_increase_the_number_of_tcp6_connections_handled_in_statistics() {
695-
logging::setup();
696-
697-
if TcpListener::bind(SocketAddrV6::new(Ipv6Addr::LOCALHOST, 0, 0, 0))
698-
.await
699-
.is_err()
700-
{
701-
return; // we cannot bind to a ipv6 socket, so we will skip this test
702-
}
703-
704-
let env = Started::new(&configuration::ephemeral_ipv6().into()).await;
705-
706-
Client::bind(*env.bind_address(), IpAddr::from_str("::1").unwrap())
707-
.announce(&QueryBuilder::default().query())
708-
.await;
709-
710-
let stats = env
711-
.container
712-
.http_tracker_core_container
713-
.http_stats_repository
714-
.get_stats()
715-
.await;
716-
717-
assert_eq!(stats.tcp6_connections_handled, 1);
718-
719-
drop(stats);
720-
721-
env.stop().await;
722-
}
723-
724-
#[tokio::test]
725-
async fn should_not_increase_the_number_of_tcp6_connections_handled_if_the_client_is_not_using_an_ipv6_ip() {
726-
logging::setup();
727-
728-
// The tracker ignores the peer address in the request param. It uses the client remote ip address.
729-
730-
let env = Started::new(&configuration::ephemeral_public().into()).await;
731-
732-
Client::new(*env.bind_address())
733-
.announce(
734-
&QueryBuilder::default()
735-
.with_peer_addr(&IpAddr::V6(Ipv6Addr::new(0, 0, 0, 0, 0, 0, 0, 1)))
736-
.query(),
737-
)
738-
.await;
739-
740-
let stats = env
741-
.container
742-
.http_tracker_core_container
743-
.http_stats_repository
744-
.get_stats()
745-
.await;
746-
747-
assert_eq!(stats.tcp6_connections_handled, 0);
748-
749-
drop(stats);
750-
751-
env.stop().await;
752-
}
753-
754669
#[tokio::test]
755670
async fn should_increase_the_number_of_tcp4_announce_requests_handled_in_statistics() {
756671
logging::setup();

packages/axum-rest-tracker-api-server/src/v1/context/stats/resources.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,13 @@ pub struct Stats {
7777
}
7878

7979
impl From<TrackerMetrics> for Stats {
80+
#[allow(deprecated)]
8081
fn from(metrics: TrackerMetrics) -> Self {
8182
Self {
82-
torrents: metrics.torrents_metrics.torrents,
83-
seeders: metrics.torrents_metrics.complete,
84-
completed: metrics.torrents_metrics.downloaded,
85-
leechers: metrics.torrents_metrics.incomplete,
83+
torrents: metrics.torrents_metrics.total_torrents,
84+
seeders: metrics.torrents_metrics.total_complete,
85+
completed: metrics.torrents_metrics.total_downloaded,
86+
leechers: metrics.torrents_metrics.total_incomplete,
8687
// TCP
8788
tcp4_connections_handled: metrics.protocol_metrics.tcp4_connections_handled,
8889
tcp4_announces_handled: metrics.protocol_metrics.tcp4_announces_handled,
@@ -119,19 +120,20 @@ impl From<TrackerMetrics> for Stats {
119120
mod tests {
120121
use torrust_rest_tracker_api_core::statistics::metrics::Metrics;
121122
use torrust_rest_tracker_api_core::statistics::services::TrackerMetrics;
122-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
123+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
123124

124125
use super::Stats;
125126

126127
#[test]
128+
#[allow(deprecated)]
127129
fn stats_resource_should_be_converted_from_tracker_metrics() {
128130
assert_eq!(
129131
Stats::from(TrackerMetrics {
130-
torrents_metrics: TorrentsMetrics {
131-
complete: 1,
132-
downloaded: 2,
133-
incomplete: 3,
134-
torrents: 4
132+
torrents_metrics: AggregateSwarmMetadata {
133+
total_complete: 1,
134+
total_downloaded: 2,
135+
total_incomplete: 3,
136+
total_torrents: 4
135137
},
136138
protocol_metrics: Metrics {
137139
// TCP

packages/axum-rest-tracker-api-server/src/v1/context/stats/responses.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
1212
}
1313

1414
/// `200` response that contains the [`Stats`] resource in Prometheus Text Exposition Format .
15+
#[allow(deprecated)]
1516
#[must_use]
1617
pub fn metrics_response(tracker_metrics: &TrackerMetrics) -> Response {
1718
let mut lines = vec![];
1819

19-
lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.torrents));
20-
lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.complete));
21-
lines.push(format!("completed {}", tracker_metrics.torrents_metrics.downloaded));
22-
lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.incomplete));
20+
lines.push(format!("torrents {}", tracker_metrics.torrents_metrics.total_torrents));
21+
lines.push(format!("seeders {}", tracker_metrics.torrents_metrics.total_complete));
22+
lines.push(format!("completed {}", tracker_metrics.torrents_metrics.total_downloaded));
23+
lines.push(format!("leechers {}", tracker_metrics.torrents_metrics.total_incomplete));
2324

2425
// TCP
2526

packages/http-tracker-core/src/services/announce.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,8 @@ use crate::statistics;
2828
///
2929
/// The service sends an statistics event that increments:
3030
///
31-
/// - The number of TCP connections handled by the HTTP tracker.
3231
/// - The number of TCP `announce` requests handled by the HTTP tracker.
33-
///
34-
/// > **NOTICE**: as the HTTP tracker does not requires a connection request
35-
/// > like the UDP tracker, the number of TCP connections is incremented for
36-
/// > each `announce` request.
32+
/// - The number of TCP `scrape` requests handled by the HTTP tracker.
3733
pub struct AnnounceService {
3834
core_config: Arc<Core>,
3935
announce_handler: Arc<AnnounceHandler>,

packages/http-tracker-core/src/services/scrape.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@ use crate::statistics;
2525
///
2626
/// The service sends an statistics event that increments:
2727
///
28-
/// - The number of TCP connections handled by the HTTP tracker.
28+
/// - The number of TCP `announce` requests handled by the HTTP tracker.
2929
/// - The number of TCP `scrape` requests handled by the HTTP tracker.
3030
///
31-
/// > **NOTICE**: as the HTTP tracker does not requires a connection request
32-
/// > like the UDP tracker, the number of TCP connections is incremented for
33-
/// > each `scrape` request.
34-
///
3531
/// # Errors
3632
///
3733
/// This function will return an error if:

packages/http-tracker-core/src/statistics/event/handler.rs

-48
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
66
// TCP4
77
Event::Tcp4Announce => {
88
stats_repository.increase_tcp4_announces().await;
9-
stats_repository.increase_tcp4_connections().await;
109
}
1110
Event::Tcp4Scrape => {
1211
stats_repository.increase_tcp4_scrapes().await;
13-
stats_repository.increase_tcp4_connections().await;
1412
}
1513

1614
// TCP6
1715
Event::Tcp6Announce => {
1816
stats_repository.increase_tcp6_announces().await;
19-
stats_repository.increase_tcp6_connections().await;
2017
}
2118
Event::Tcp6Scrape => {
2219
stats_repository.increase_tcp6_scrapes().await;
23-
stats_repository.increase_tcp6_connections().await;
2420
}
2521
}
2622

@@ -44,17 +40,6 @@ mod tests {
4440
assert_eq!(stats.tcp4_announces_handled, 1);
4541
}
4642

47-
#[tokio::test]
48-
async fn should_increase_the_tcp4_connections_counter_when_it_receives_a_tcp4_announce_event() {
49-
let stats_repository = Repository::new();
50-
51-
handle_event(Event::Tcp4Announce, &stats_repository).await;
52-
53-
let stats = stats_repository.get_stats().await;
54-
55-
assert_eq!(stats.tcp4_connections_handled, 1);
56-
}
57-
5843
#[tokio::test]
5944
async fn should_increase_the_tcp4_scrapes_counter_when_it_receives_a_tcp4_scrape_event() {
6045
let stats_repository = Repository::new();
@@ -66,17 +51,6 @@ mod tests {
6651
assert_eq!(stats.tcp4_scrapes_handled, 1);
6752
}
6853

69-
#[tokio::test]
70-
async fn should_increase_the_tcp4_connections_counter_when_it_receives_a_tcp4_scrape_event() {
71-
let stats_repository = Repository::new();
72-
73-
handle_event(Event::Tcp4Scrape, &stats_repository).await;
74-
75-
let stats = stats_repository.get_stats().await;
76-
77-
assert_eq!(stats.tcp4_connections_handled, 1);
78-
}
79-
8054
#[tokio::test]
8155
async fn should_increase_the_tcp6_announces_counter_when_it_receives_a_tcp6_announce_event() {
8256
let stats_repository = Repository::new();
@@ -88,17 +62,6 @@ mod tests {
8862
assert_eq!(stats.tcp6_announces_handled, 1);
8963
}
9064

91-
#[tokio::test]
92-
async fn should_increase_the_tcp6_connections_counter_when_it_receives_a_tcp6_announce_event() {
93-
let stats_repository = Repository::new();
94-
95-
handle_event(Event::Tcp6Announce, &stats_repository).await;
96-
97-
let stats = stats_repository.get_stats().await;
98-
99-
assert_eq!(stats.tcp6_connections_handled, 1);
100-
}
101-
10265
#[tokio::test]
10366
async fn should_increase_the_tcp6_scrapes_counter_when_it_receives_a_tcp6_scrape_event() {
10467
let stats_repository = Repository::new();
@@ -109,15 +72,4 @@ mod tests {
10972

11073
assert_eq!(stats.tcp6_scrapes_handled, 1);
11174
}
112-
113-
#[tokio::test]
114-
async fn should_increase_the_tcp6_connections_counter_when_it_receives_a_tcp6_scrape_event() {
115-
let stats_repository = Repository::new();
116-
117-
handle_event(Event::Tcp6Scrape, &stats_repository).await;
118-
119-
let stats = stats_repository.get_stats().await;
120-
121-
assert_eq!(stats.tcp6_connections_handled, 1);
122-
}
12375
}

packages/http-tracker-core/src/statistics/metrics.rs

-8
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,12 @@
88
/// and also for each IP version used by the peers: IPv4 and IPv6.
99
#[derive(Debug, PartialEq, Default)]
1010
pub struct Metrics {
11-
/// Total number of TCP (HTTP tracker) connections from IPv4 peers.
12-
/// Since the HTTP tracker spec does not require a handshake, this metric
13-
/// increases for every HTTP request.
14-
pub tcp4_connections_handled: u64,
15-
1611
/// Total number of TCP (HTTP tracker) `announce` requests from IPv4 peers.
1712
pub tcp4_announces_handled: u64,
1813

1914
/// Total number of TCP (HTTP tracker) `scrape` requests from IPv4 peers.
2015
pub tcp4_scrapes_handled: u64,
2116

22-
/// Total number of TCP (HTTP tracker) connections from IPv6 peers.
23-
pub tcp6_connections_handled: u64,
24-
2517
/// Total number of TCP (HTTP tracker) `announce` requests from IPv6 peers.
2618
pub tcp6_announces_handled: u64,
2719

packages/http-tracker-core/src/statistics/repository.rs

-12
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ impl Repository {
3434
drop(stats_lock);
3535
}
3636

37-
pub async fn increase_tcp4_connections(&self) {
38-
let mut stats_lock = self.stats.write().await;
39-
stats_lock.tcp4_connections_handled += 1;
40-
drop(stats_lock);
41-
}
42-
4337
pub async fn increase_tcp4_scrapes(&self) {
4438
let mut stats_lock = self.stats.write().await;
4539
stats_lock.tcp4_scrapes_handled += 1;
@@ -52,12 +46,6 @@ impl Repository {
5246
drop(stats_lock);
5347
}
5448

55-
pub async fn increase_tcp6_connections(&self) {
56-
let mut stats_lock = self.stats.write().await;
57-
stats_lock.tcp6_connections_handled += 1;
58-
drop(stats_lock);
59-
}
60-
6149
pub async fn increase_tcp6_scrapes(&self) {
6250
let mut stats_lock = self.stats.write().await;
6351
stats_lock.tcp6_scrapes_handled += 1;

packages/http-tracker-core/src/statistics/services.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
use std::sync::Arc;
2424

2525
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
26-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
26+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
2727

2828
use crate::statistics::metrics::Metrics;
2929
use crate::statistics::repository::Repository;
@@ -34,7 +34,7 @@ pub struct TrackerMetrics {
3434
/// Domain level metrics.
3535
///
3636
/// General metrics for all torrents (number of seeders, leechers, etcetera)
37-
pub torrents_metrics: TorrentsMetrics,
37+
pub torrents_metrics: AggregateSwarmMetadata,
3838

3939
/// Application level metrics. Usage statistics/metrics.
4040
///
@@ -54,11 +54,9 @@ pub async fn get_metrics(
5454
torrents_metrics,
5555
protocol_metrics: Metrics {
5656
// TCPv4
57-
tcp4_connections_handled: stats.tcp4_connections_handled,
5857
tcp4_announces_handled: stats.tcp4_announces_handled,
5958
tcp4_scrapes_handled: stats.tcp4_scrapes_handled,
6059
// TCPv6
61-
tcp6_connections_handled: stats.tcp6_connections_handled,
6260
tcp6_announces_handled: stats.tcp6_announces_handled,
6361
tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
6462
},
@@ -72,7 +70,7 @@ mod tests {
7270
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
7371
use bittorrent_tracker_core::{self};
7472
use torrust_tracker_configuration::Configuration;
75-
use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
73+
use torrust_tracker_primitives::swarm_metadata::AggregateSwarmMetadata;
7674
use torrust_tracker_test_helpers::configuration;
7775

7876
use crate::statistics;
@@ -96,7 +94,7 @@ mod tests {
9694
assert_eq!(
9795
tracker_metrics,
9896
TrackerMetrics {
99-
torrents_metrics: TorrentsMetrics::default(),
97+
torrents_metrics: AggregateSwarmMetadata::default(),
10098
protocol_metrics: statistics::metrics::Metrics::default(),
10199
}
102100
);

packages/primitives/src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ pub mod core;
88
pub mod pagination;
99
pub mod peer;
1010
pub mod swarm_metadata;
11-
pub mod torrent_metrics;
1211

1312
use std::collections::BTreeMap;
1413
use std::time::Duration;

0 commit comments

Comments
 (0)