Skip to content

Commit 3fce84b

Browse files
committed
Merge #1536: Overhaul stats: Scaffolding for metrics in the tracker-core package
2175270 feat: [#1535] scaffolding for tracker-core metrics (Jose Celano) Pull request description: Relates to: #1502 It adds a new metric. The new metric in Prometheus format: ``` tracker_core_persistent_torrents_downloads_total{} 1 ``` In new PRs, the new metric will be persisted. ACKs for top commit: josecelano: ACK 2175270 Tree-SHA512: 99c08e6326dcfc434bc12edff998dcd9f273492a114b8c39196c1c97ac64685e52f842e1b34a2d7b9d4659964a365ef0409f4c85a5fbb96456921ce00f08adfc
2 parents f21957d + 2175270 commit 3fce84b

File tree

15 files changed

+276
-14
lines changed

15 files changed

+276
-14
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub async fn get_metrics_handler(
7070
Arc<InMemoryTorrentRepository>,
7171
Arc<RwLock<BanService>>,
7272
Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
73+
Arc<bittorrent_tracker_core::statistics::repository::Repository>,
7374
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
7475
Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
7576
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
@@ -83,6 +84,7 @@ pub async fn get_metrics_handler(
8384
state.3.clone(),
8485
state.4.clone(),
8586
state.5.clone(),
87+
state.6.clone(),
8688
)
8789
.await;
8890

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
2828
get(get_metrics_handler).with_state((
2929
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
3030
http_api_container.ban_service.clone(),
31+
// Stats
3132
http_api_container.torrent_repository_container.stats_repository.clone(),
33+
http_api_container.tracker_core_container.stats_repository.clone(),
3234
http_api_container.http_stats_repository.clone(),
3335
http_api_container.udp_core_stats_repository.clone(),
3436
http_api_container.udp_server_stats_repository.clone(),

packages/rest-tracker-api-core/src/statistics/services.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ pub async fn get_labeled_metrics(
9494
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
9595
ban_service: Arc<RwLock<BanService>>,
9696
swarms_stats_repository: Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
97+
tracker_core_stats_repository: Arc<bittorrent_tracker_core::statistics::repository::Repository>,
9798
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
9899
udp_stats_repository: Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
99100
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
@@ -102,6 +103,7 @@ pub async fn get_labeled_metrics(
102103
let _udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
103104

104105
let swarms_stats = swarms_stats_repository.get_metrics().await;
106+
let tracker_core_stats = tracker_core_stats_repository.get_metrics().await;
105107
let http_stats = http_stats_repository.get_stats().await;
106108
let udp_stats_repository = udp_stats_repository.get_stats().await;
107109
let udp_server_stats = udp_server_stats_repository.get_stats().await;
@@ -112,6 +114,9 @@ pub async fn get_labeled_metrics(
112114
metrics
113115
.merge(&swarms_stats.metric_collection)
114116
.expect("msg: failed to merge torrent repository metrics");
117+
metrics
118+
.merge(&tracker_core_stats.metric_collection)
119+
.expect("msg: failed to merge tracker core metrics");
115120
metrics
116121
.merge(&http_stats.metric_collection)
117122
.expect("msg: failed to merge HTTP core metrics");

packages/tracker-client/src/http/client/requests/announce.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ pub type BaseTenASCII = u64;
5353
pub type PortNumber = u16;
5454

5555
pub enum Event {
56-
//Started,
57-
//Stopped,
56+
Started,
57+
Stopped,
5858
Completed,
5959
}
6060

6161
impl fmt::Display for Event {
6262
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6363
match self {
64-
//Event::Started => write!(f, "started"),
65-
//Event::Stopped => write!(f, "stopped"),
64+
Event::Started => write!(f, "started"),
65+
Event::Stopped => write!(f, "stopped"),
6666
Event::Completed => write!(f, "completed"),
6767
}
6868
}

packages/tracker-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
3131
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
3232
torrust-tracker-events = { version = "3.0.0-develop", path = "../events" }
3333
torrust-tracker-located-error = { version = "3.0.0-develop", path = "../located-error" }
34+
torrust-tracker-metrics = { version = "3.0.0-develop", path = "../metrics" }
3435
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
3536
torrust-tracker-torrent-repository = { version = "3.0.0-develop", path = "../torrent-repository" }
3637
tracing = "0"

packages/tracker-core/src/container.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use crate::scrape_handler::ScrapeHandler;
1414
use crate::torrent::manager::TorrentsManager;
1515
use crate::torrent::repository::in_memory::InMemoryTorrentRepository;
1616
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
17-
use crate::whitelist;
1817
use crate::whitelist::authorization::WhitelistAuthorization;
1918
use crate::whitelist::manager::WhitelistManager;
2019
use crate::whitelist::repository::in_memory::InMemoryWhitelist;
2120
use crate::whitelist::setup::initialize_whitelist_manager;
21+
use crate::{statistics, whitelist};
2222

2323
pub struct TrackerCoreContainer {
2424
pub core_config: Arc<Core>,
@@ -33,6 +33,7 @@ pub struct TrackerCoreContainer {
3333
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
3434
pub db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
3535
pub torrents_manager: Arc<TorrentsManager>,
36+
pub stats_repository: Arc<statistics::repository::Repository>,
3637
}
3738

3839
impl TrackerCoreContainer {
@@ -58,6 +59,8 @@ impl TrackerCoreContainer {
5859
&db_torrent_repository,
5960
));
6061

62+
let stats_repository = Arc::new(statistics::repository::Repository::new());
63+
6164
let announce_handler = Arc::new(AnnounceHandler::new(
6265
core_config,
6366
&whitelist_authorization,
@@ -80,6 +83,7 @@ impl TrackerCoreContainer {
8083
in_memory_torrent_repository,
8184
db_torrent_repository,
8285
torrents_manager,
86+
stats_repository,
8387
}
8488
}
8589
}

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
use std::sync::Arc;
22

3+
use torrust_tracker_metrics::label::LabelSet;
4+
use torrust_tracker_metrics::metric_name;
35
use torrust_tracker_primitives::DurationSinceUnixEpoch;
46
use torrust_tracker_torrent_repository::event::Event;
57

8+
use crate::statistics::repository::Repository;
9+
use crate::statistics::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL;
610
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
711

812
pub async fn handle_event(
913
event: Event,
14+
stats_repository: &Arc<Repository>,
1015
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
11-
_now: DurationSinceUnixEpoch,
16+
now: DurationSinceUnixEpoch,
1217
) {
1318
match event {
1419
// Torrent events
@@ -36,6 +41,7 @@ pub async fn handle_event(
3641
Event::PeerDownloadCompleted { info_hash, peer } => {
3742
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer download completed", );
3843

44+
// Increment the number of downloads for the torrent
3945
match db_torrent_repository.increase_number_of_downloads(&info_hash) {
4046
Ok(()) => {
4147
tracing::debug!(info_hash = ?info_hash, "Number of downloads increased");
@@ -44,6 +50,19 @@ pub async fn handle_event(
4450
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads");
4551
}
4652
}
53+
54+
// Increment the number of downloads for all the torrents
55+
let _unused = stats_repository
56+
.increment_counter(
57+
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
58+
&LabelSet::default(),
59+
now,
60+
)
61+
.await;
62+
63+
// todo:
64+
// - Persist the metric into the database.
65+
// - Load the metric from the database.
4766
}
4867
}
4968
}

packages/tracker-core/src/statistics/event/listener.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,33 @@ use torrust_tracker_events::receiver::RecvError;
66
use torrust_tracker_torrent_repository::event::receiver::Receiver;
77

88
use super::handler::handle_event;
9+
use crate::statistics::repository::Repository;
910
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
1011
use crate::{CurrentClock, TRACKER_CORE_LOG_TARGET};
1112

1213
#[must_use]
1314
pub fn run_event_listener(
1415
receiver: Receiver,
16+
repository: &Arc<Repository>,
1517
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
1618
) -> JoinHandle<()> {
19+
let stats_repository = repository.clone();
1720
let db_torrent_repository: Arc<DatabasePersistentTorrentRepository> = db_torrent_repository.clone();
1821

1922
tracing::info!(target: TRACKER_CORE_LOG_TARGET, "Starting torrent repository event listener");
2023

2124
tokio::spawn(async move {
22-
dispatch_events(receiver, db_torrent_repository).await;
25+
dispatch_events(receiver, stats_repository, db_torrent_repository).await;
2326

2427
tracing::info!(target: TRACKER_CORE_LOG_TARGET, "Torrent repository listener finished");
2528
})
2629
}
2730

28-
async fn dispatch_events(mut receiver: Receiver, db_torrent_repository: Arc<DatabasePersistentTorrentRepository>) {
31+
async fn dispatch_events(
32+
mut receiver: Receiver,
33+
stats_repository: Arc<Repository>,
34+
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
35+
) {
2936
let shutdown_signal = tokio::signal::ctrl_c();
3037

3138
tokio::pin!(shutdown_signal);
@@ -41,7 +48,7 @@ async fn dispatch_events(mut receiver: Receiver, db_torrent_repository: Arc<Data
4148

4249
result = receiver.recv() => {
4350
match result {
44-
Ok(event) => handle_event(event, &db_torrent_repository, CurrentClock::now()).await,
51+
Ok(event) => handle_event(event, &stats_repository, &db_torrent_repository, CurrentClock::now()).await,
4552
Err(e) => {
4653
match e {
4754
RecvError::Closed => {
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
use serde::Serialize;
2+
use torrust_tracker_metrics::label::LabelSet;
3+
use torrust_tracker_metrics::metric::MetricName;
4+
use torrust_tracker_metrics::metric_collection::{Error, MetricCollection};
5+
use torrust_tracker_primitives::DurationSinceUnixEpoch;
6+
7+
/// Metrics collected by the torrent repository.
8+
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
9+
pub struct Metrics {
10+
/// A collection of metrics.
11+
pub metric_collection: MetricCollection,
12+
}
13+
14+
impl Metrics {
15+
/// # Errors
16+
///
17+
/// Returns an error if the metric does not exist and it cannot be created.
18+
pub fn increment_counter(
19+
&mut self,
20+
metric_name: &MetricName,
21+
labels: &LabelSet,
22+
now: DurationSinceUnixEpoch,
23+
) -> Result<(), Error> {
24+
self.metric_collection.increase_counter(metric_name, labels, now)
25+
}
26+
27+
/// # Errors
28+
///
29+
/// Returns an error if the metric does not exist and it cannot be created.
30+
pub fn set_gauge(
31+
&mut self,
32+
metric_name: &MetricName,
33+
labels: &LabelSet,
34+
value: f64,
35+
now: DurationSinceUnixEpoch,
36+
) -> Result<(), Error> {
37+
self.metric_collection.set_gauge(metric_name, labels, value, now)
38+
}
39+
40+
/// # Errors
41+
///
42+
/// Returns an error if the metric does not exist and it cannot be created.
43+
pub fn increment_gauge(
44+
&mut self,
45+
metric_name: &MetricName,
46+
labels: &LabelSet,
47+
now: DurationSinceUnixEpoch,
48+
) -> Result<(), Error> {
49+
self.metric_collection.increment_gauge(metric_name, labels, now)
50+
}
51+
52+
/// # Errors
53+
///
54+
/// Returns an error if the metric does not exist and it cannot be created.
55+
pub fn decrement_gauge(
56+
&mut self,
57+
metric_name: &MetricName,
58+
labels: &LabelSet,
59+
now: DurationSinceUnixEpoch,
60+
) -> Result<(), Error> {
61+
self.metric_collection.decrement_gauge(metric_name, labels, now)
62+
}
63+
}

0 commit comments

Comments
 (0)