Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub async fn get_metrics_handler(
Arc<InMemoryTorrentRepository>,
Arc<RwLock<BanService>>,
Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
Arc<bittorrent_tracker_core::statistics::repository::Repository>,
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
Expand All @@ -83,6 +84,7 @@ pub async fn get_metrics_handler(
state.3.clone(),
state.4.clone(),
state.5.clone(),
state.6.clone(),
)
.await;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
get(get_metrics_handler).with_state((
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
http_api_container.ban_service.clone(),
// Stats
http_api_container.torrent_repository_container.stats_repository.clone(),
http_api_container.tracker_core_container.stats_repository.clone(),
http_api_container.http_stats_repository.clone(),
http_api_container.udp_core_stats_repository.clone(),
http_api_container.udp_server_stats_repository.clone(),
Expand Down
5 changes: 5 additions & 0 deletions packages/rest-tracker-api-core/src/statistics/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub async fn get_labeled_metrics(
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
ban_service: Arc<RwLock<BanService>>,
swarms_stats_repository: Arc<torrust_tracker_torrent_repository::statistics::repository::Repository>,
tracker_core_stats_repository: Arc<bittorrent_tracker_core::statistics::repository::Repository>,
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
udp_stats_repository: Arc<bittorrent_udp_tracker_core::statistics::repository::Repository>,
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
Expand All @@ -102,6 +103,7 @@ pub async fn get_labeled_metrics(
let _udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();

let swarms_stats = swarms_stats_repository.get_metrics().await;
let tracker_core_stats = tracker_core_stats_repository.get_metrics().await;
let http_stats = http_stats_repository.get_stats().await;
let udp_stats_repository = udp_stats_repository.get_stats().await;
let udp_server_stats = udp_server_stats_repository.get_stats().await;
Expand All @@ -112,6 +114,9 @@ pub async fn get_labeled_metrics(
metrics
.merge(&swarms_stats.metric_collection)
.expect("msg: failed to merge torrent repository metrics");
metrics
.merge(&tracker_core_stats.metric_collection)
.expect("msg: failed to merge tracker core metrics");
metrics
.merge(&http_stats.metric_collection)
.expect("msg: failed to merge HTTP core metrics");
Expand Down
8 changes: 4 additions & 4 deletions packages/tracker-client/src/http/client/requests/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ pub type BaseTenASCII = u64;
pub type PortNumber = u16;

pub enum Event {
//Started,
//Stopped,
Started,
Stopped,
Completed,
}

impl fmt::Display for Event {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
//Event::Started => write!(f, "started"),
//Event::Stopped => write!(f, "stopped"),
Event::Started => write!(f, "started"),
Event::Stopped => write!(f, "stopped"),
Event::Completed => write!(f, "completed"),
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/tracker-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
torrust-tracker-events = { version = "3.0.0-develop", path = "../events" }
torrust-tracker-located-error = { version = "3.0.0-develop", path = "../located-error" }
torrust-tracker-metrics = { version = "3.0.0-develop", path = "../metrics" }
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
torrust-tracker-torrent-repository = { version = "3.0.0-develop", path = "../torrent-repository" }
tracing = "0"
Expand Down
6 changes: 5 additions & 1 deletion packages/tracker-core/src/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ use crate::scrape_handler::ScrapeHandler;
use crate::torrent::manager::TorrentsManager;
use crate::torrent::repository::in_memory::InMemoryTorrentRepository;
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
use crate::whitelist;
use crate::whitelist::authorization::WhitelistAuthorization;
use crate::whitelist::manager::WhitelistManager;
use crate::whitelist::repository::in_memory::InMemoryWhitelist;
use crate::whitelist::setup::initialize_whitelist_manager;
use crate::{statistics, whitelist};

pub struct TrackerCoreContainer {
pub core_config: Arc<Core>,
Expand All @@ -33,6 +33,7 @@ pub struct TrackerCoreContainer {
pub in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
pub db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
pub torrents_manager: Arc<TorrentsManager>,
pub stats_repository: Arc<statistics::repository::Repository>,
}

impl TrackerCoreContainer {
Expand All @@ -58,6 +59,8 @@ impl TrackerCoreContainer {
&db_torrent_repository,
));

let stats_repository = Arc::new(statistics::repository::Repository::new());

let announce_handler = Arc::new(AnnounceHandler::new(
core_config,
&whitelist_authorization,
Expand All @@ -80,6 +83,7 @@ impl TrackerCoreContainer {
in_memory_torrent_repository,
db_torrent_repository,
torrents_manager,
stats_repository,
}
}
}
21 changes: 20 additions & 1 deletion packages/tracker-core/src/statistics/event/handler.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
use std::sync::Arc;

use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric_name;
use torrust_tracker_primitives::DurationSinceUnixEpoch;
use torrust_tracker_torrent_repository::event::Event;

use crate::statistics::repository::Repository;
use crate::statistics::TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL;
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;

pub async fn handle_event(
event: Event,
stats_repository: &Arc<Repository>,
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
_now: DurationSinceUnixEpoch,
now: DurationSinceUnixEpoch,
) {
match event {
// Torrent events
Expand Down Expand Up @@ -36,6 +41,7 @@ pub async fn handle_event(
Event::PeerDownloadCompleted { info_hash, peer } => {
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer download completed", );

// Increment the number of downloads for the torrent
match db_torrent_repository.increase_number_of_downloads(&info_hash) {
Ok(()) => {
tracing::debug!(info_hash = ?info_hash, "Number of downloads increased");
Expand All @@ -44,6 +50,19 @@ pub async fn handle_event(
tracing::error!(info_hash = ?info_hash, error = ?err, "Failed to increase number of downloads");
}
}

// Increment the number of downloads for all the torrents
let _unused = stats_repository
.increment_counter(
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
&LabelSet::default(),
now,
)
.await;

// todo:
// - Persist the metric into the database.
// - Load the metric from the database.
}
}
}
13 changes: 10 additions & 3 deletions packages/tracker-core/src/statistics/event/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,33 @@ use torrust_tracker_events::receiver::RecvError;
use torrust_tracker_torrent_repository::event::receiver::Receiver;

use super::handler::handle_event;
use crate::statistics::repository::Repository;
use crate::torrent::repository::persisted::DatabasePersistentTorrentRepository;
use crate::{CurrentClock, TRACKER_CORE_LOG_TARGET};

#[must_use]
pub fn run_event_listener(
receiver: Receiver,
repository: &Arc<Repository>,
db_torrent_repository: &Arc<DatabasePersistentTorrentRepository>,
) -> JoinHandle<()> {
let stats_repository = repository.clone();
let db_torrent_repository: Arc<DatabasePersistentTorrentRepository> = db_torrent_repository.clone();

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

tokio::spawn(async move {
dispatch_events(receiver, db_torrent_repository).await;
dispatch_events(receiver, stats_repository, db_torrent_repository).await;

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

async fn dispatch_events(mut receiver: Receiver, db_torrent_repository: Arc<DatabasePersistentTorrentRepository>) {
async fn dispatch_events(
mut receiver: Receiver,
stats_repository: Arc<Repository>,
db_torrent_repository: Arc<DatabasePersistentTorrentRepository>,
) {
let shutdown_signal = tokio::signal::ctrl_c();

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

result = receiver.recv() => {
match result {
Ok(event) => handle_event(event, &db_torrent_repository, CurrentClock::now()).await,
Ok(event) => handle_event(event, &stats_repository, &db_torrent_repository, CurrentClock::now()).await,
Err(e) => {
match e {
RecvError::Closed => {
Expand Down
63 changes: 63 additions & 0 deletions packages/tracker-core/src/statistics/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use serde::Serialize;
use torrust_tracker_metrics::label::LabelSet;
use torrust_tracker_metrics::metric::MetricName;
use torrust_tracker_metrics::metric_collection::{Error, MetricCollection};
use torrust_tracker_primitives::DurationSinceUnixEpoch;

/// Metrics collected by the torrent repository.
#[derive(Debug, Clone, PartialEq, Default, Serialize)]
pub struct Metrics {
/// A collection of metrics.
pub metric_collection: MetricCollection,
}

impl Metrics {
/// # Errors
///
/// Returns an error if the metric does not exist and it cannot be created.
pub fn increment_counter(
&mut self,
metric_name: &MetricName,
labels: &LabelSet,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
self.metric_collection.increase_counter(metric_name, labels, now)
}

/// # Errors
///
/// Returns an error if the metric does not exist and it cannot be created.
pub fn set_gauge(
&mut self,
metric_name: &MetricName,
labels: &LabelSet,
value: f64,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
self.metric_collection.set_gauge(metric_name, labels, value, now)
}

/// # Errors
///
/// Returns an error if the metric does not exist and it cannot be created.
pub fn increment_gauge(
&mut self,
metric_name: &MetricName,
labels: &LabelSet,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
self.metric_collection.increment_gauge(metric_name, labels, now)
}

/// # Errors
///
/// Returns an error if the metric does not exist and it cannot be created.
pub fn decrement_gauge(
&mut self,
metric_name: &MetricName,
labels: &LabelSet,
now: DurationSinceUnixEpoch,
) -> Result<(), Error> {
self.metric_collection.decrement_gauge(metric_name, labels, now)
}
}
26 changes: 26 additions & 0 deletions packages/tracker-core/src/statistics/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,27 @@
pub mod event;
pub mod metrics;
pub mod repository;

use metrics::Metrics;
use torrust_tracker_metrics::metric::description::MetricDescription;
use torrust_tracker_metrics::metric_name;
use torrust_tracker_metrics::unit::Unit;

// Torrent metrics

const TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL: &str = "tracker_core_persistent_torrents_downloads_total";

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

// Torrent metrics

metrics.metric_collection.describe_counter(
&metric_name!(TRACKER_CORE_PERSISTENT_TORRENTS_DOWNLOADS_TOTAL),
Some(Unit::Count),
Some(&MetricDescription::new("The total number of torrent downloads (persisted).")),
);

metrics
}
Loading
Loading