Skip to content

Commit fbefc88

Browse files
committed
Merge #1528: Overhaul stats: add event metrics to torrent-repository package
b11af88 feat: [#1522] add events metrics in torrent-repository (Jose Celano) Pull request description: These new metrics just count the number of times events have occurred. ACKs for top commit: josecelano: ACK b11af88 Tree-SHA512: 554fe51bd6be9d39f3bf9603d01e394136f81f159375ab7264fac2bed337e10084c54bbc9fa61f7b928ea7def1d4dd88f0f80aca7a261a425f8db4a8547688d5
2 parents a194f1e + b11af88 commit fbefc88

File tree

2 files changed

+229
-18
lines changed

2 files changed

+229
-18
lines changed

packages/torrent-repository/src/statistics/event/handler.rs

Lines changed: 191 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use torrust_tracker_primitives::DurationSinceUnixEpoch;
88
use crate::event::Event;
99
use crate::statistics::repository::Repository;
1010
use crate::statistics::{
11-
TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL, TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL, TORRENT_REPOSITORY_TORRENTS_TOTAL,
11+
TORRENT_REPOSITORY_PEERS_ADDED_TOTAL, TORRENT_REPOSITORY_PEERS_REMOVED_TOTAL, TORRENT_REPOSITORY_PEERS_UPDATED_TOTAL,
12+
TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL, TORRENT_REPOSITORY_TORRENTS_ADDED_TOTAL,
13+
TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL, TORRENT_REPOSITORY_TORRENTS_REMOVED_TOTAL, TORRENT_REPOSITORY_TORRENTS_TOTAL,
1214
};
1315

1416
pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now: DurationSinceUnixEpoch) {
@@ -20,36 +22,56 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
2022
let _unused = stats_repository
2123
.increment_gauge(&metric_name!(TORRENT_REPOSITORY_TORRENTS_TOTAL), &LabelSet::default(), now)
2224
.await;
25+
26+
let _unused = stats_repository
27+
.increment_counter(
28+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_ADDED_TOTAL),
29+
&LabelSet::default(),
30+
now,
31+
)
32+
.await;
2333
}
2434
Event::TorrentRemoved { info_hash } => {
2535
tracing::debug!(info_hash = ?info_hash, "Torrent removed",);
2636

2737
let _unused = stats_repository
2838
.decrement_gauge(&metric_name!(TORRENT_REPOSITORY_TORRENTS_TOTAL), &LabelSet::default(), now)
2939
.await;
40+
41+
let _unused = stats_repository
42+
.increment_counter(
43+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_REMOVED_TOTAL),
44+
&LabelSet::default(),
45+
now,
46+
)
47+
.await;
3048
}
3149

3250
// Peer events
3351
Event::PeerAdded { info_hash, peer } => {
3452
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer added", );
3553

54+
let label_set = label_set_for_peer(&peer);
55+
3656
let _unused = stats_repository
37-
.increment_gauge(
38-
&metric_name!(TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL),
39-
&label_set_for_peer(&peer),
40-
now,
41-
)
57+
.increment_gauge(&metric_name!(TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL), &label_set, now)
58+
.await;
59+
60+
let _unused = stats_repository
61+
.increment_counter(&metric_name!(TORRENT_REPOSITORY_PEERS_ADDED_TOTAL), &label_set, now)
4262
.await;
4363
}
4464
Event::PeerRemoved { info_hash, peer } => {
4565
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer removed", );
4666

67+
let label_set = label_set_for_peer(&peer);
68+
4769
let _unused = stats_repository
48-
.decrement_gauge(
49-
&metric_name!(TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL),
50-
&label_set_for_peer(&peer),
51-
now,
52-
)
70+
.decrement_gauge(&metric_name!(TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL), &label_set, now)
71+
.await;
72+
73+
let _unused = stats_repository
74+
.increment_counter(&metric_name!(TORRENT_REPOSITORY_PEERS_REMOVED_TOTAL), &label_set, now)
5375
.await;
5476
}
5577
Event::PeerUpdated {
@@ -76,6 +98,12 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
7698
)
7799
.await;
78100
}
101+
102+
let label_set = label_set_for_peer(&new_peer);
103+
104+
let _unused = stats_repository
105+
.increment_counter(&metric_name!(TORRENT_REPOSITORY_PEERS_UPDATED_TOTAL), &label_set, now)
106+
.await;
79107
}
80108
Event::PeerDownloadCompleted { info_hash, peer } => {
81109
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer download completed", );
@@ -92,7 +120,7 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
92120
}
93121

94122
/// Returns the label set to be included in the metrics for the given peer.
95-
fn label_set_for_peer(peer: &Peer) -> LabelSet {
123+
pub(crate) fn label_set_for_peer(peer: &Peer) -> LabelSet {
96124
if peer.is_seeder() {
97125
(label_name!("peer_role"), LabelValue::new("seeder")).into()
98126
} else {
@@ -135,7 +163,7 @@ mod tests {
135163
opposite_role_peer
136164
}
137165

138-
async fn expect_counter_metric_to_be(
166+
pub async fn expect_counter_metric_to_be(
139167
stats_repository: &Arc<Repository>,
140168
metric_name: &MetricName,
141169
label_set: &LabelSet,
@@ -186,9 +214,11 @@ mod tests {
186214

187215
use crate::event::Event;
188216
use crate::statistics::event::handler::handle_event;
189-
use crate::statistics::event::handler::tests::expect_gauge_metric_to_be;
217+
use crate::statistics::event::handler::tests::{expect_counter_metric_to_be, expect_gauge_metric_to_be};
190218
use crate::statistics::repository::Repository;
191-
use crate::statistics::TORRENT_REPOSITORY_TORRENTS_TOTAL;
219+
use crate::statistics::{
220+
TORRENT_REPOSITORY_TORRENTS_ADDED_TOTAL, TORRENT_REPOSITORY_TORRENTS_REMOVED_TOTAL, TORRENT_REPOSITORY_TORRENTS_TOTAL,
221+
};
192222
use crate::tests::{sample_info_hash, sample_peer};
193223
use crate::CurrentClock;
194224

@@ -242,9 +272,73 @@ mod tests {
242272

243273
expect_gauge_metric_to_be(&stats_repository, &metric_name, &label_set, 0.0).await;
244274
}
275+
276+
#[tokio::test]
277+
async fn it_should_increment_the_number_of_torrents_added_when_a_torrent_added_event_is_received() {
278+
clock::Stopped::local_set_to_unix_epoch();
279+
280+
let stats_repository = Arc::new(Repository::new());
281+
282+
handle_event(
283+
Event::TorrentAdded {
284+
info_hash: sample_info_hash(),
285+
announcement: sample_peer(),
286+
},
287+
&stats_repository,
288+
CurrentClock::now(),
289+
)
290+
.await;
291+
292+
expect_counter_metric_to_be(
293+
&stats_repository,
294+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_ADDED_TOTAL),
295+
&LabelSet::default(),
296+
1,
297+
)
298+
.await;
299+
}
300+
301+
#[tokio::test]
302+
async fn it_should_increment_the_number_of_torrents_removed_when_a_torrent_removed_event_is_received() {
303+
clock::Stopped::local_set_to_unix_epoch();
304+
305+
let stats_repository = Arc::new(Repository::new());
306+
307+
handle_event(
308+
Event::TorrentRemoved {
309+
info_hash: sample_info_hash(),
310+
},
311+
&stats_repository,
312+
CurrentClock::now(),
313+
)
314+
.await;
315+
316+
expect_counter_metric_to_be(
317+
&stats_repository,
318+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_REMOVED_TOTAL),
319+
&LabelSet::default(),
320+
1,
321+
)
322+
.await;
323+
}
245324
}
246325

247326
mod for_peer_metrics {
327+
use std::sync::Arc;
328+
329+
use torrust_tracker_clock::clock::stopped::Stopped;
330+
use torrust_tracker_clock::clock::{self, Time};
331+
use torrust_tracker_metrics::metric_name;
332+
333+
use crate::event::Event;
334+
use crate::statistics::event::handler::tests::expect_counter_metric_to_be;
335+
use crate::statistics::event::handler::{handle_event, label_set_for_peer};
336+
use crate::statistics::repository::Repository;
337+
use crate::statistics::{
338+
TORRENT_REPOSITORY_PEERS_ADDED_TOTAL, TORRENT_REPOSITORY_PEERS_REMOVED_TOTAL, TORRENT_REPOSITORY_PEERS_UPDATED_TOTAL,
339+
};
340+
use crate::tests::{sample_info_hash, sample_peer};
341+
use crate::CurrentClock;
248342

249343
mod peer_connections_total {
250344

@@ -383,6 +477,88 @@ mod tests {
383477
}
384478
}
385479

480+
#[tokio::test]
481+
async fn it_should_increment_the_number_of_peers_added_when_a_peer_added_event_is_received() {
482+
clock::Stopped::local_set_to_unix_epoch();
483+
484+
let stats_repository = Arc::new(Repository::new());
485+
486+
let peer = sample_peer();
487+
488+
handle_event(
489+
Event::PeerAdded {
490+
info_hash: sample_info_hash(),
491+
peer,
492+
},
493+
&stats_repository,
494+
CurrentClock::now(),
495+
)
496+
.await;
497+
498+
expect_counter_metric_to_be(
499+
&stats_repository,
500+
&metric_name!(TORRENT_REPOSITORY_PEERS_ADDED_TOTAL),
501+
&label_set_for_peer(&peer),
502+
1,
503+
)
504+
.await;
505+
}
506+
507+
#[tokio::test]
508+
async fn it_should_increment_the_number_of_peers_removed_when_a_peer_removed_event_is_received() {
509+
clock::Stopped::local_set_to_unix_epoch();
510+
511+
let stats_repository = Arc::new(Repository::new());
512+
513+
let peer = sample_peer();
514+
515+
handle_event(
516+
Event::PeerRemoved {
517+
info_hash: sample_info_hash(),
518+
peer,
519+
},
520+
&stats_repository,
521+
CurrentClock::now(),
522+
)
523+
.await;
524+
525+
expect_counter_metric_to_be(
526+
&stats_repository,
527+
&metric_name!(TORRENT_REPOSITORY_PEERS_REMOVED_TOTAL),
528+
&label_set_for_peer(&peer),
529+
1,
530+
)
531+
.await;
532+
}
533+
534+
#[tokio::test]
535+
async fn it_should_increment_the_number_of_peers_updated_when_a_peer_updated_event_is_received() {
536+
clock::Stopped::local_set_to_unix_epoch();
537+
538+
let stats_repository = Arc::new(Repository::new());
539+
540+
let new_peer = sample_peer();
541+
542+
handle_event(
543+
Event::PeerUpdated {
544+
info_hash: sample_info_hash(),
545+
old_peer: sample_peer(),
546+
new_peer,
547+
},
548+
&stats_repository,
549+
CurrentClock::now(),
550+
)
551+
.await;
552+
553+
expect_counter_metric_to_be(
554+
&stats_repository,
555+
&metric_name!(TORRENT_REPOSITORY_PEERS_UPDATED_TOTAL),
556+
&label_set_for_peer(&new_peer),
557+
1,
558+
)
559+
.await;
560+
}
561+
386562
mod torrent_downloads_total {
387563

388564
use std::sync::Arc;

packages/torrent-repository/src/statistics/mod.rs

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ use torrust_tracker_metrics::unit::Unit;
99

1010
// Torrent metrics
1111

12+
const TORRENT_REPOSITORY_TORRENTS_ADDED_TOTAL: &str = "torrent_repository_torrents_added_total";
13+
const TORRENT_REPOSITORY_TORRENTS_REMOVED_TOTAL: &str = "torrent_repository_torrents_removed_total";
14+
1215
const TORRENT_REPOSITORY_TORRENTS_TOTAL: &str = "torrent_repository_torrents_total";
1316
const TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL: &str = "torrent_repository_torrents_downloads_total";
1417

1518
// Peers metrics
1619

20+
const TORRENT_REPOSITORY_PEERS_ADDED_TOTAL: &str = "torrent_repository_peers_added_total";
21+
const TORRENT_REPOSITORY_PEERS_REMOVED_TOTAL: &str = "torrent_repository_peers_removed_total";
22+
const TORRENT_REPOSITORY_PEERS_UPDATED_TOTAL: &str = "torrent_repository_peers_updated_total";
23+
1724
const TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL: &str = "torrent_repository_peer_connections_total";
1825
const TORRENT_REPOSITORY_UNIQUE_PEERS_TOTAL: &str = "torrent_repository_unique_peers_total"; // todo: not implemented yet
1926

@@ -23,6 +30,18 @@ pub fn describe_metrics() -> Metrics {
2330

2431
// Torrent metrics
2532

33+
metrics.metric_collection.describe_counter(
34+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_ADDED_TOTAL),
35+
Some(Unit::Count),
36+
Some(&MetricDescription::new("The total number of torrents added.")),
37+
);
38+
39+
metrics.metric_collection.describe_counter(
40+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_REMOVED_TOTAL),
41+
Some(Unit::Count),
42+
Some(&MetricDescription::new("The total number of torrents removed.")),
43+
);
44+
2645
metrics.metric_collection.describe_gauge(
2746
&metric_name!(TORRENT_REPOSITORY_TORRENTS_TOTAL),
2847
Some(Unit::Count),
@@ -32,13 +51,29 @@ pub fn describe_metrics() -> Metrics {
3251
metrics.metric_collection.describe_counter(
3352
&metric_name!(TORRENT_REPOSITORY_TORRENTS_DOWNLOADS_TOTAL),
3453
Some(Unit::Count),
35-
Some(&MetricDescription::new(
36-
"The total number of torrent downloads (since the tracker process started).",
37-
)),
54+
Some(&MetricDescription::new("The total number of torrent downloads.")),
3855
);
3956

4057
// Peers metrics
4158

59+
metrics.metric_collection.describe_counter(
60+
&metric_name!(TORRENT_REPOSITORY_PEERS_ADDED_TOTAL),
61+
Some(Unit::Count),
62+
Some(&MetricDescription::new("The total number of peers added.")),
63+
);
64+
65+
metrics.metric_collection.describe_counter(
66+
&metric_name!(TORRENT_REPOSITORY_PEERS_REMOVED_TOTAL),
67+
Some(Unit::Count),
68+
Some(&MetricDescription::new("The total number of peers removed.")),
69+
);
70+
71+
metrics.metric_collection.describe_counter(
72+
&metric_name!(TORRENT_REPOSITORY_PEERS_UPDATED_TOTAL),
73+
Some(Unit::Count),
74+
Some(&MetricDescription::new("The total number of peers updated.")),
75+
);
76+
4277
metrics.metric_collection.describe_gauge(
4378
&metric_name!(TORRENT_REPOSITORY_PEER_CONNECTIONS_TOTAL),
4479
Some(Unit::Count),

0 commit comments

Comments
 (0)