Skip to content
Closed
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
124 changes: 124 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions cSpell.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"Eray",
"filesd",
"flamegraph",
"formatjson",
"Freebox",
"Frostegård",
"gecos",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepo
use bittorrent_udp_tracker_core::services::banning::BanService;
use serde::Deserialize;
use tokio::sync::RwLock;
use torrust_rest_tracker_api_core::statistics::services::get_metrics;
use torrust_rest_tracker_api_core::statistics::services::{get_labeled_metrics, get_metrics};

use super::responses::{metrics_response, stats_response};
use super::responses::{labeled_stats_response, metrics_response, stats_response};

#[derive(Deserialize, Debug, Default)]
#[serde(rename_all = "lowercase")]
Expand Down Expand Up @@ -57,3 +57,24 @@ pub async fn get_stats_handler(
None => stats_response(metrics),
}
}

#[allow(clippy::type_complexity)]
pub async fn get_metrics_handler(
State(state): State<(
Arc<InMemoryTorrentRepository>,
Arc<RwLock<BanService>>,
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
)>,
params: Query<QueryParams>,
) -> Response {
let metrics = get_labeled_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;

match params.0.format {
Some(format) => match format {
Format::Json => labeled_stats_response(metrics),
Format::Prometheus => todo!(),
},
None => labeled_stats_response(metrics),
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! API resources for the [`stats`](crate::v1::context::stats)
//! API context.
use bittorrent_http_tracker_core::statistics::metrics::LabeledMetric;
use serde::{Deserialize, Serialize};
use torrust_rest_tracker_api_core::statistics::services::TrackerMetrics;
use torrust_rest_tracker_api_core::statistics::services::{TrackerLabeledMetrics, TrackerMetrics};

/// It contains all the statistics generated by the tracker.
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -116,6 +117,22 @@ impl From<TrackerMetrics> for Stats {
}
}

/// It contains all the statistics generated by the tracker.
#[derive(Serialize, Debug, PartialEq, Eq)]
pub struct LabeledStats {
// Extendable metrics
labeled_metrics: Vec<LabeledMetric>,
}

impl From<TrackerLabeledMetrics> for LabeledStats {
#[allow(deprecated)]
fn from(metrics: TrackerLabeledMetrics) -> Self {
Self {
labeled_metrics: metrics.labeled_metrics,
}
}
}

#[cfg(test)]
mod tests {
use torrust_rest_tracker_api_core::statistics::metrics::Metrics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
//! API responses for the [`stats`](crate::v1::context::stats)
//! API context.
use axum::response::{IntoResponse, Json, Response};
use torrust_rest_tracker_api_core::statistics::services::TrackerMetrics;
use torrust_rest_tracker_api_core::statistics::services::{TrackerLabeledMetrics, TrackerMetrics};

use super::resources::Stats;
use super::resources::{LabeledStats, Stats};

/// `200` response that contains the [`Stats`] resource as json.
#[must_use]
pub fn stats_response(tracker_metrics: TrackerMetrics) -> Response {
Json(Stats::from(tracker_metrics)).into_response()
}

/// `200` response that contains the [`LabeledStats`] resource as json.
#[must_use]
pub fn labeled_stats_response(tracker_metrics: TrackerLabeledMetrics) -> Response {
Json(LabeledStats::from(tracker_metrics)).into_response()
}

/// `200` response that contains the [`Stats`] resource in Prometheus Text Exposition Format .
#[allow(deprecated)]
#[must_use]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,27 @@ use axum::routing::get;
use axum::Router;
use torrust_rest_tracker_api_core::container::TrackerHttpApiCoreContainer;

use super::handlers::get_stats_handler;
use super::handlers::{get_metrics_handler, get_stats_handler};

/// It adds the routes to the router for the [`stats`](crate::v1::context::stats) API context.
pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApiCoreContainer>) -> Router {
router.route(
&format!("{prefix}/stats"),
get(get_stats_handler).with_state((
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
http_api_container.ban_service.clone(),
http_api_container.http_stats_repository.clone(),
http_api_container.udp_server_stats_repository.clone(),
)),
)
router
.route(
&format!("{prefix}/stats"),
get(get_stats_handler).with_state((
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
http_api_container.ban_service.clone(),
http_api_container.http_stats_repository.clone(),
http_api_container.udp_server_stats_repository.clone(),
)),
)
.route(
&format!("{prefix}/metrics"),
get(get_metrics_handler).with_state((
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
http_api_container.ban_service.clone(),
http_api_container.http_stats_repository.clone(),
http_api_container.udp_server_stats_repository.clone(),
)),
)
}
3 changes: 3 additions & 0 deletions packages/http-tracker-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ bittorrent-primitives = "0.1.0"
bittorrent-tracker-core = { version = "3.0.0-develop", path = "../tracker-core" }
criterion = { version = "0.5.1", features = ["async_tokio"] }
futures = "0"
serde = "1.0.219"
thiserror = "2"
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync"] }
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
tracing = "0"

[dev-dependencies]
formatjson = "0.3.1"
mockall = "0"
serde_json = "1.0.140"
torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-helpers" }

[[bench]]
Expand Down
Loading
Loading