torrust_tracker/core/services/statistics/mod.rs
1//! Statistics services.
2//!
3//! It includes:
4//!
5//! - A [`factory`](crate::core::services::statistics::setup::factory) function to build the structs needed to collect the tracker metrics.
6//! - A [`get_metrics`] service to get the [`tracker metrics`](crate::core::statistics::Metrics).
7//!
8//! Tracker metrics are collected using a Publisher-Subscribe pattern.
9//!
10//! The factory function builds two structs:
11//!
12//! - An statistics [`EventSender`](crate::core::statistics::EventSender)
13//! - An statistics [`Repo`](crate::core::statistics::Repo)
14//!
15//! ```text
16//! let (stats_event_sender, stats_repository) = factory(tracker_usage_statistics);
17//! ```
18//!
19//! The statistics repository is responsible for storing the metrics in memory.
20//! The statistics event sender allows sending events related to metrics.
21//! There is an event listener that is receiving all the events and processing them with an event handler.
22//! Then, the event handler updates the metrics depending on the received event.
23//!
24//! For example, if you send the event [`Event::Udp4Connect`](crate::core::statistics::Event::Udp4Connect):
25//!
26//! ```text
27//! let result = event_sender.send_event(Event::Udp4Connect).await;
28//! ```
29//!
30//! Eventually the counter for UDP connections from IPv4 peers will be increased.
31//!
32//! ```rust,no_run
33//! pub struct Metrics {
34//! // ...
35//! pub udp4_connections_handled: u64, // This will be incremented
36//! // ...
37//! }
38//! ```
39pub mod setup;
40
41use std::sync::Arc;
42
43use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
44
45use crate::core::statistics::Metrics;
46use crate::core::Tracker;
47
48/// All the metrics collected by the tracker.
49#[derive(Debug, PartialEq)]
50pub struct TrackerMetrics {
51 /// Domain level metrics.
52 ///
53 /// General metrics for all torrents (number of seeders, leechers, etcetera)
54 pub torrents_metrics: TorrentsMetrics,
55
56 /// Application level metrics. Usage statistics/metrics.
57 ///
58 /// Metrics about how the tracker is been used (number of udp announce requests, number of http scrape requests, etcetera)
59 pub protocol_metrics: Metrics,
60}
61
62/// It returns all the [`TrackerMetrics`]
63pub async fn get_metrics(tracker: Arc<Tracker>) -> TrackerMetrics {
64 let torrents_metrics = tracker.get_torrents_metrics();
65 let stats = tracker.get_stats().await;
66
67 TrackerMetrics {
68 torrents_metrics,
69 protocol_metrics: Metrics {
70 tcp4_connections_handled: stats.tcp4_connections_handled,
71 tcp4_announces_handled: stats.tcp4_announces_handled,
72 tcp4_scrapes_handled: stats.tcp4_scrapes_handled,
73 tcp6_connections_handled: stats.tcp6_connections_handled,
74 tcp6_announces_handled: stats.tcp6_announces_handled,
75 tcp6_scrapes_handled: stats.tcp6_scrapes_handled,
76 udp4_connections_handled: stats.udp4_connections_handled,
77 udp4_announces_handled: stats.udp4_announces_handled,
78 udp4_scrapes_handled: stats.udp4_scrapes_handled,
79 udp6_connections_handled: stats.udp6_connections_handled,
80 udp6_announces_handled: stats.udp6_announces_handled,
81 udp6_scrapes_handled: stats.udp6_scrapes_handled,
82 },
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use std::sync::Arc;
89
90 use torrust_tracker_configuration::Configuration;
91 use torrust_tracker_primitives::torrent_metrics::TorrentsMetrics;
92 use torrust_tracker_test_helpers::configuration;
93
94 use crate::core;
95 use crate::core::services::statistics::{get_metrics, TrackerMetrics};
96 use crate::core::services::tracker_factory;
97
98 pub fn tracker_configuration() -> Configuration {
99 configuration::ephemeral()
100 }
101
102 #[tokio::test]
103 async fn the_statistics_service_should_return_the_tracker_metrics() {
104 let tracker = Arc::new(tracker_factory(&tracker_configuration()));
105
106 let tracker_metrics = get_metrics(tracker.clone()).await;
107
108 assert_eq!(
109 tracker_metrics,
110 TrackerMetrics {
111 torrents_metrics: TorrentsMetrics::default(),
112 protocol_metrics: core::statistics::Metrics::default(),
113 }
114 );
115 }
116}