Skip to content

Commit 5f08b2e

Browse files
committed
refactor: [torrust#1228] start using the http tracker stats
Stats have been splited into HTTP and UDP stats. Parallel change, step 1: 1. [x] Start using HTTP Tracker Core Stats 2. [ ] Start using UDP Tracker Core Stats 3. [ ] Get metrics from HTTP and UDP Tracker Core Stats 4. [ ] Remove deprecate unified HTTP and UDP stats.
1 parent 39cbeda commit 5f08b2e

File tree

7 files changed

+306
-69
lines changed

7 files changed

+306
-69
lines changed

src/container.rs

+2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub struct HttpTrackerContainer {
7171
pub scrape_handler: Arc<ScrapeHandler>,
7272
pub whitelist_authorization: Arc<whitelist::authorization::WhitelistAuthorization>,
7373
pub stats_event_sender: Arc<Option<Box<dyn Sender>>>,
74+
pub http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
7475
pub authentication_service: Arc<AuthenticationService>,
7576
}
7677

@@ -84,6 +85,7 @@ impl HttpTrackerContainer {
8485
scrape_handler: app_container.scrape_handler.clone(),
8586
whitelist_authorization: app_container.whitelist_authorization.clone(),
8687
stats_event_sender: app_container.stats_event_sender.clone(),
88+
http_stats_event_sender: app_container.http_stats_event_sender.clone(),
8789
authentication_service: app_container.authentication_service.clone(),
8890
}
8991
}

src/servers/http/v1/handlers/announce.rs

+48-20
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use torrust_tracker_primitives::core::AnnounceData;
2828
use torrust_tracker_primitives::peer;
2929

3030
use super::common::auth::map_auth_error_to_error_response;
31+
use crate::packages::http_tracker_core;
3132
use crate::servers::http::v1::extractors::announce_request::ExtractRequest;
3233
use crate::servers::http::v1::extractors::authentication_key::Extract as ExtractKey;
3334
use crate::servers::http::v1::extractors::client_ip_sources::Extract as ExtractClientIpSources;
@@ -46,6 +47,7 @@ pub async fn handle_without_key(
4647
Arc<AuthenticationService>,
4748
Arc<whitelist::authorization::WhitelistAuthorization>,
4849
Arc<Option<Box<dyn Sender>>>,
50+
Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
4951
)>,
5052
ExtractRequest(announce_request): ExtractRequest,
5153
ExtractClientIpSources(client_ip_sources): ExtractClientIpSources,
@@ -58,6 +60,7 @@ pub async fn handle_without_key(
5860
&state.2,
5961
&state.3,
6062
&state.4,
63+
&state.5,
6164
&announce_request,
6265
&client_ip_sources,
6366
None,
@@ -76,6 +79,7 @@ pub async fn handle_with_key(
7679
Arc<AuthenticationService>,
7780
Arc<whitelist::authorization::WhitelistAuthorization>,
7881
Arc<Option<Box<dyn Sender>>>,
82+
Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
7983
)>,
8084
ExtractRequest(announce_request): ExtractRequest,
8185
ExtractClientIpSources(client_ip_sources): ExtractClientIpSources,
@@ -89,6 +93,7 @@ pub async fn handle_with_key(
8993
&state.2,
9094
&state.3,
9195
&state.4,
96+
&state.5,
9297
&announce_request,
9398
&client_ip_sources,
9499
Some(key),
@@ -107,6 +112,7 @@ async fn handle(
107112
authentication_service: &Arc<AuthenticationService>,
108113
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
109114
opt_stats_event_sender: &Arc<Option<Box<dyn Sender>>>,
115+
opt_http_stats_event_sender: &Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
110116
announce_request: &Announce,
111117
client_ip_sources: &ClientIpSources,
112118
maybe_key: Option<Key>,
@@ -117,6 +123,7 @@ async fn handle(
117123
authentication_service,
118124
whitelist_authorization,
119125
opt_stats_event_sender,
126+
opt_http_stats_event_sender,
120127
announce_request,
121128
client_ip_sources,
122129
maybe_key,
@@ -142,6 +149,7 @@ async fn handle_announce(
142149
authentication_service: &Arc<AuthenticationService>,
143150
whitelist_authorization: &Arc<whitelist::authorization::WhitelistAuthorization>,
144151
opt_stats_event_sender: &Arc<Option<Box<dyn Sender>>>,
152+
opt_http_stats_event_sender: &Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
145153
announce_request: &Announce,
146154
client_ip_sources: &ClientIpSources,
147155
maybe_key: Option<Key>,
@@ -181,6 +189,7 @@ async fn handle_announce(
181189
let announce_data = services::announce::invoke(
182190
announce_handler.clone(),
183191
opt_stats_event_sender.clone(),
192+
opt_http_stats_event_sender.clone(),
184193
announce_request.info_hash,
185194
&mut peer,
186195
&peers_wanted,
@@ -265,7 +274,7 @@ mod tests {
265274
use torrust_tracker_configuration::{Configuration, Core};
266275
use torrust_tracker_test_helpers::configuration;
267276

268-
use crate::packages;
277+
use crate::packages::{self, http_tracker_core};
269278

270279
struct CoreTrackerServices {
271280
pub core_config: Arc<Core>,
@@ -275,23 +284,27 @@ mod tests {
275284
pub authentication_service: Arc<AuthenticationService>,
276285
}
277286

278-
fn initialize_private_tracker() -> CoreTrackerServices {
287+
struct CoreHttpTrackerServices {
288+
pub http_stats_event_sender: Arc<Option<Box<dyn http_tracker_core::statistics::event::sender::Sender>>>,
289+
}
290+
291+
fn initialize_private_tracker() -> (CoreTrackerServices, CoreHttpTrackerServices) {
279292
initialize_core_tracker_services(&configuration::ephemeral_private())
280293
}
281294

282-
fn initialize_listed_tracker() -> CoreTrackerServices {
295+
fn initialize_listed_tracker() -> (CoreTrackerServices, CoreHttpTrackerServices) {
283296
initialize_core_tracker_services(&configuration::ephemeral_listed())
284297
}
285298

286-
fn initialize_tracker_on_reverse_proxy() -> CoreTrackerServices {
299+
fn initialize_tracker_on_reverse_proxy() -> (CoreTrackerServices, CoreHttpTrackerServices) {
287300
initialize_core_tracker_services(&configuration::ephemeral_with_reverse_proxy())
288301
}
289302

290-
fn initialize_tracker_not_on_reverse_proxy() -> CoreTrackerServices {
303+
fn initialize_tracker_not_on_reverse_proxy() -> (CoreTrackerServices, CoreHttpTrackerServices) {
291304
initialize_core_tracker_services(&configuration::ephemeral_without_reverse_proxy())
292305
}
293306

294-
fn initialize_core_tracker_services(config: &Configuration) -> CoreTrackerServices {
307+
fn initialize_core_tracker_services(config: &Configuration) -> (CoreTrackerServices, CoreHttpTrackerServices) {
295308
let core_config = Arc::new(config.core.clone());
296309
let database = initialize_database(config);
297310
let in_memory_whitelist = Arc::new(InMemoryWhitelist::default());
@@ -300,21 +313,31 @@ mod tests {
300313
let authentication_service = Arc::new(AuthenticationService::new(&config.core, &in_memory_key_repository));
301314
let in_memory_torrent_repository = Arc::new(InMemoryTorrentRepository::default());
302315
let db_torrent_repository = Arc::new(DatabasePersistentTorrentRepository::new(&database));
303-
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
304-
let stats_event_sender = Arc::new(stats_event_sender);
305316
let announce_handler = Arc::new(AnnounceHandler::new(
306317
&config.core,
307318
&in_memory_torrent_repository,
308319
&db_torrent_repository,
309320
));
310321

311-
CoreTrackerServices {
312-
core_config,
313-
announce_handler,
314-
stats_event_sender,
315-
whitelist_authorization,
316-
authentication_service,
317-
}
322+
let (stats_event_sender, _stats_repository) = statistics::setup::factory(config.core.tracker_usage_statistics);
323+
let stats_event_sender = Arc::new(stats_event_sender);
324+
325+
// HTTP stats
326+
let (http_stats_event_sender, http_stats_repository) =
327+
http_tracker_core::statistics::setup::factory(config.core.tracker_usage_statistics);
328+
let http_stats_event_sender = Arc::new(http_stats_event_sender);
329+
let _http_stats_repository = Arc::new(http_stats_repository);
330+
331+
(
332+
CoreTrackerServices {
333+
core_config,
334+
announce_handler,
335+
stats_event_sender,
336+
whitelist_authorization,
337+
authentication_service,
338+
},
339+
CoreHttpTrackerServices { http_stats_event_sender },
340+
)
318341
}
319342

320343
fn sample_announce_request() -> Announce {
@@ -357,7 +380,7 @@ mod tests {
357380

358381
#[tokio::test]
359382
async fn it_should_fail_when_the_authentication_key_is_missing() {
360-
let core_tracker_services = initialize_private_tracker();
383+
let (core_tracker_services, http_core_tracker_services) = initialize_private_tracker();
361384

362385
let maybe_key = None;
363386

@@ -367,6 +390,7 @@ mod tests {
367390
&core_tracker_services.authentication_service,
368391
&core_tracker_services.whitelist_authorization,
369392
&core_tracker_services.stats_event_sender,
393+
&http_core_tracker_services.http_stats_event_sender,
370394
&sample_announce_request(),
371395
&sample_client_ip_sources(),
372396
maybe_key,
@@ -382,7 +406,7 @@ mod tests {
382406

383407
#[tokio::test]
384408
async fn it_should_fail_when_the_authentication_key_is_invalid() {
385-
let core_tracker_services = initialize_private_tracker();
409+
let (core_tracker_services, http_core_tracker_services) = initialize_private_tracker();
386410

387411
let unregistered_key = authentication::Key::from_str("YZSl4lMZupRuOpSRC3krIKR5BPB14nrJ").unwrap();
388412

@@ -394,6 +418,7 @@ mod tests {
394418
&core_tracker_services.authentication_service,
395419
&core_tracker_services.whitelist_authorization,
396420
&core_tracker_services.stats_event_sender,
421+
&http_core_tracker_services.http_stats_event_sender,
397422
&sample_announce_request(),
398423
&sample_client_ip_sources(),
399424
maybe_key,
@@ -413,7 +438,7 @@ mod tests {
413438

414439
#[tokio::test]
415440
async fn it_should_fail_when_the_announced_torrent_is_not_whitelisted() {
416-
let core_tracker_services = initialize_listed_tracker();
441+
let (core_tracker_services, http_core_tracker_services) = initialize_listed_tracker();
417442

418443
let announce_request = sample_announce_request();
419444

@@ -423,6 +448,7 @@ mod tests {
423448
&core_tracker_services.authentication_service,
424449
&core_tracker_services.whitelist_authorization,
425450
&core_tracker_services.stats_event_sender,
451+
&http_core_tracker_services.http_stats_event_sender,
426452
&announce_request,
427453
&sample_client_ip_sources(),
428454
None,
@@ -450,7 +476,7 @@ mod tests {
450476

451477
#[tokio::test]
452478
async fn it_should_fail_when_the_right_most_x_forwarded_for_header_ip_is_not_available() {
453-
let core_tracker_services = initialize_tracker_on_reverse_proxy();
479+
let (core_tracker_services, http_core_tracker_services) = initialize_tracker_on_reverse_proxy();
454480

455481
let client_ip_sources = ClientIpSources {
456482
right_most_x_forwarded_for: None,
@@ -463,6 +489,7 @@ mod tests {
463489
&core_tracker_services.authentication_service,
464490
&core_tracker_services.whitelist_authorization,
465491
&core_tracker_services.stats_event_sender,
492+
&http_core_tracker_services.http_stats_event_sender,
466493
&sample_announce_request(),
467494
&client_ip_sources,
468495
None,
@@ -487,7 +514,7 @@ mod tests {
487514

488515
#[tokio::test]
489516
async fn it_should_fail_when_the_client_ip_from_the_connection_info_is_not_available() {
490-
let core_tracker_services = initialize_tracker_not_on_reverse_proxy();
517+
let (core_tracker_services, http_core_tracker_services) = initialize_tracker_not_on_reverse_proxy();
491518

492519
let client_ip_sources = ClientIpSources {
493520
right_most_x_forwarded_for: None,
@@ -500,6 +527,7 @@ mod tests {
500527
&core_tracker_services.authentication_service,
501528
&core_tracker_services.whitelist_authorization,
502529
&core_tracker_services.stats_event_sender,
530+
&http_core_tracker_services.http_stats_event_sender,
503531
&sample_announce_request(),
504532
&client_ip_sources,
505533
None,

0 commit comments

Comments
 (0)