Skip to content

Commit d47483f

Browse files
committed
feat: [#1358] new metric in torrent-repository: total number of torrents
1 parent 29a2dfd commit d47483f

File tree

7 files changed

+111
-19
lines changed

7 files changed

+111
-19
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ use crate::event::Event;
99
use crate::statistics::repository::Repository;
1010
use crate::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
1111

12-
/// # Panics
13-
///
14-
/// This function panics if the client IP address is not the same as the IP
15-
/// version of the event.
1612
pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now: DurationSinceUnixEpoch) {
1713
match event {
1814
Event::TcpAnnounce { connection, .. } => {

packages/metrics/src/metric_collection.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ impl MetricCollection {
140140
///
141141
/// Return an error if a metrics of a different type with the same name
142142
/// already exists.
143-
pub fn increase_gauge(&mut self, name: &MetricName, label_set: &LabelSet, time: DurationSinceUnixEpoch) -> Result<(), Error> {
143+
pub fn increment_gauge(
144+
&mut self,
145+
name: &MetricName,
146+
label_set: &LabelSet,
147+
time: DurationSinceUnixEpoch,
148+
) -> Result<(), Error> {
144149
if self.counters.metrics.contains_key(name) {
145150
return Err(Error::MetricNameCollisionAdding {
146151
metric_name: name.clone(),
@@ -156,7 +161,12 @@ impl MetricCollection {
156161
///
157162
/// Return an error if a metrics of a different type with the same name
158163
/// already exists.
159-
pub fn decrease_gauge(&mut self, name: &MetricName, label_set: &LabelSet, time: DurationSinceUnixEpoch) -> Result<(), Error> {
164+
pub fn decrement_gauge(
165+
&mut self,
166+
name: &MetricName,
167+
label_set: &LabelSet,
168+
time: DurationSinceUnixEpoch,
169+
) -> Result<(), Error> {
160170
if self.counters.metrics.contains_key(name) {
161171
return Err(Error::MetricNameCollisionAdding {
162172
metric_name: name.clone(),
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
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

57
use crate::event::Event;
68
use crate::statistics::repository::Repository;
9+
use crate::statistics::TORRENT_REPOSITORY_TORRENTS_TOTAL;
710

8-
/// # Panics
9-
///
10-
/// This function panics if the client IP address is not the same as the IP
11-
/// version of the event.
12-
pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, _now: DurationSinceUnixEpoch) {
11+
pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now: DurationSinceUnixEpoch) {
1312
match event {
1413
Event::TorrentAdded { info_hash, .. } => {
15-
// todo: update metrics
1614
tracing::debug!("Torrent added {info_hash}");
15+
16+
match stats_repository
17+
.increment_gauge(&metric_name!(TORRENT_REPOSITORY_TORRENTS_TOTAL), &LabelSet::default(), now)
18+
.await
19+
{
20+
Ok(()) => {}
21+
Err(err) => tracing::error!("Failed to increment the gauge: {}", err),
22+
};
1723
}
1824
Event::TorrentRemoved { info_hash } => {
19-
// todo: update metrics
2025
tracing::debug!("Torrent removed {info_hash}");
26+
27+
match stats_repository
28+
.decrement_gauge(&metric_name!(TORRENT_REPOSITORY_TORRENTS_TOTAL), &LabelSet::default(), now)
29+
.await
30+
{
31+
Ok(()) => {}
32+
Err(err) => tracing::error!("Failed to decrement the gauge: {}", err),
33+
};
2134
}
2235
Event::PeerAdded { announcement } => {
2336
// todo: update metrics
@@ -28,6 +41,4 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, _now
2841
tracing::debug!("Peer removed: socket address {socket_addr:?}, peer ID: {peer_id:?}");
2942
}
3043
}
31-
32-
tracing::debug!("metrics: {:?}", stats_repository.get_metrics().await);
3344
}

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

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Metrics {
1515
/// # Errors
1616
///
1717
/// Returns an error if the metric does not exist and it cannot be created.
18-
pub fn increase_counter(
18+
pub fn increment_counter(
1919
&mut self,
2020
metric_name: &MetricName,
2121
labels: &LabelSet,
@@ -36,4 +36,28 @@ impl Metrics {
3636
) -> Result<(), Error> {
3737
self.metric_collection.set_gauge(metric_name, labels, value, now)
3838
}
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+
}
3963
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,19 @@ use torrust_tracker_metrics::metric::description::MetricDescription;
77
use torrust_tracker_metrics::metric_name;
88
use torrust_tracker_metrics::unit::Unit;
99

10+
const TORRENT_REPOSITORY_TORRENTS_TOTAL: &str = "torrent_repository_torrents_total";
1011
const TORRENT_REPOSITORY_RUNTIME_TORRENTS_DOWNLOADS_TOTAL: &str = "torrent_repository_runtime_torrents_downloads_total";
1112

1213
#[must_use]
1314
pub fn describe_metrics() -> Metrics {
1415
let mut metrics = Metrics::default();
1516

17+
metrics.metric_collection.describe_gauge(
18+
&metric_name!(TORRENT_REPOSITORY_TORRENTS_TOTAL),
19+
Some(Unit::Count),
20+
Some(&MetricDescription::new("The total number of torrents.")),
21+
);
22+
1623
metrics.metric_collection.describe_counter(
1724
&metric_name!(TORRENT_REPOSITORY_RUNTIME_TORRENTS_DOWNLOADS_TOTAL),
1825
Some(Unit::Count),

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

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,54 @@ impl Repository {
3636
/// # Errors
3737
///
3838
/// This function will return an error if the metric collection fails to
39-
/// increase the counter.
40-
pub async fn increase_counter(
39+
/// increment the counter.
40+
pub async fn increment_counter(
4141
&self,
4242
metric_name: &MetricName,
4343
labels: &LabelSet,
4444
now: DurationSinceUnixEpoch,
4545
) -> Result<(), Error> {
4646
let mut stats_lock = self.stats.write().await;
4747

48-
let result = stats_lock.increase_counter(metric_name, labels, now);
48+
let result = stats_lock.increment_counter(metric_name, labels, now);
49+
50+
drop(stats_lock);
51+
52+
result
53+
}
54+
55+
/// # Errors
56+
///
57+
/// This function will return an error if the metric collection fails to
58+
/// increment the gauge.
59+
pub async fn increment_gauge(
60+
&self,
61+
metric_name: &MetricName,
62+
labels: &LabelSet,
63+
now: DurationSinceUnixEpoch,
64+
) -> Result<(), Error> {
65+
let mut stats_lock = self.stats.write().await;
66+
67+
let result = stats_lock.increment_gauge(metric_name, labels, now);
68+
69+
drop(stats_lock);
70+
71+
result
72+
}
73+
74+
/// # Errors
75+
///
76+
/// This function will return an error if the metric collection fails to
77+
/// decrement the gauge.
78+
pub async fn decrement_gauge(
79+
&self,
80+
metric_name: &MetricName,
81+
labels: &LabelSet,
82+
now: DurationSinceUnixEpoch,
83+
) -> Result<(), Error> {
84+
let mut stats_lock = self.stats.write().await;
85+
86+
let result = stats_lock.decrement_gauge(metric_name, labels, now);
4987

5088
drop(stats_lock);
5189

packages/torrent-repository/src/swarms.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,15 @@ impl Swarms {
299299
continue;
300300
}
301301

302+
let info_hash = *swarm_handle.key();
303+
302304
swarm_handle.remove();
303305

304306
peerless_torrents_removed += 1;
307+
308+
if let Some(event_sender) = self.event_sender.as_deref() {
309+
event_sender.send(Event::TorrentRemoved { info_hash }).await;
310+
}
305311
}
306312

307313
tracing::info!(peerless_torrents_removed = peerless_torrents_removed);

0 commit comments

Comments
 (0)