Skip to content

Commit a552482

Browse files
committed
refactor: [#1581] finished. Global metrics in API loaded from labeled metrics
1 parent 6183eba commit a552482

File tree

5 files changed

+4
-90
lines changed

5 files changed

+4
-90
lines changed

Cargo.lock

Lines changed: 0 additions & 1 deletion
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: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,13 @@ pub struct QueryParams {
4141
pub async fn get_stats_handler(
4242
State(state): State<(
4343
Arc<InMemoryTorrentRepository>,
44-
Arc<RwLock<BanService>>,
4544
Arc<bittorrent_tracker_core::statistics::repository::Repository>,
4645
Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
4746
Arc<torrust_udp_tracker_server::statistics::repository::Repository>,
4847
)>,
4948
params: Query<QueryParams>,
5049
) -> Response {
51-
let metrics = get_metrics(
52-
state.0.clone(),
53-
state.1.clone(),
54-
state.2.clone(),
55-
state.3.clone(),
56-
state.4.clone(),
57-
)
58-
.await;
50+
let metrics = get_metrics(state.0.clone(), state.1.clone(), state.2.clone(), state.3.clone()).await;
5951

6052
match params.0.format {
6153
Some(format) => match format {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ pub fn add(prefix: &str, router: Router, http_api_container: &Arc<TrackerHttpApi
1818
&format!("{prefix}/stats"),
1919
get(get_stats_handler).with_state((
2020
http_api_container.tracker_core_container.in_memory_torrent_repository.clone(),
21-
http_api_container.ban_service.clone(),
2221
http_api_container.tracker_core_container.stats_repository.clone(),
2322
http_api_container.http_stats_repository.clone(),
2423
http_api_container.udp_server_stats_repository.clone(),

packages/rest-tracker-api-core/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ torrust-tracker-metrics = { version = "3.0.0-develop", path = "../metrics" }
2323
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
2424
torrust-tracker-swarm-coordination-registry = { version = "3.0.0-develop", path = "../swarm-coordination-registry" }
2525
torrust-udp-tracker-server = { version = "3.0.0-develop", path = "../udp-tracker-server" }
26-
tracing = "0"
2726

2827
[dev-dependencies]
2928
torrust-tracker-events = { version = "3.0.0-develop", path = "../events" }

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

Lines changed: 3 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -27,39 +27,13 @@ pub struct TrackerMetrics {
2727
/// It returns all the [`TrackerMetrics`]
2828
pub async fn get_metrics(
2929
in_memory_torrent_repository: Arc<InMemoryTorrentRepository>,
30-
ban_service: Arc<RwLock<BanService>>,
3130
tracker_core_stats_repository: Arc<bittorrent_tracker_core::statistics::repository::Repository>,
3231
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
3332
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
3433
) -> TrackerMetrics {
35-
let protocol_metrics_from_global_metrics = get_protocol_metrics(
36-
ban_service.clone(),
37-
http_stats_repository.clone(),
38-
udp_server_stats_repository.clone(),
39-
)
40-
.await;
41-
42-
let protocol_metrics_from_labeled_metrics =
43-
get_protocol_metrics_from_labeled_metrics(http_stats_repository.clone(), udp_server_stats_repository.clone()).await;
44-
45-
// todo:
46-
// We keep both metrics until we deploy to production and we can
47-
// ensure that the protocol metrics from labeled metrics are correct.
48-
// After that we can remove the `get_protocol_metrics` function and
49-
// use only the `get_protocol_metrics_from_labeled_metrics` function.
50-
// And also remove the code in repositories to generate the global metrics.
51-
let protocol_metrics = if protocol_metrics_from_global_metrics == protocol_metrics_from_labeled_metrics {
52-
protocol_metrics_from_labeled_metrics
53-
} else {
54-
tracing::warn!("The protocol metrics from global metrics and labeled metrics are different");
55-
tracing::warn!("Global metrics: {:?}", protocol_metrics_from_global_metrics);
56-
tracing::warn!("Labeled metrics: {:?}", protocol_metrics_from_labeled_metrics);
57-
protocol_metrics_from_global_metrics
58-
};
59-
6034
TrackerMetrics {
6135
torrents_metrics: get_torrents_metrics(in_memory_torrent_repository, tracker_core_stats_repository).await,
62-
protocol_metrics,
36+
protocol_metrics: get_protocol_metrics(http_stats_repository.clone(), udp_server_stats_repository.clone()).await,
6337
}
6438
}
6539

@@ -76,57 +50,9 @@ async fn get_torrents_metrics(
7650
torrents_metrics
7751
}
7852

79-
#[allow(deprecated)]
80-
async fn get_protocol_metrics(
81-
ban_service: Arc<RwLock<BanService>>,
82-
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
83-
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
84-
) -> ProtocolMetrics {
85-
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
86-
let http_stats = http_stats_repository.get_stats().await;
87-
let udp_server_stats = udp_server_stats_repository.get_stats().await;
88-
89-
// For backward compatibility we keep the `tcp4_connections_handled` and
90-
// `tcp6_connections_handled` metrics. They don't make sense for the HTTP
91-
// tracker, but we keep them for now. In new major versions we should remove
92-
// them.
93-
94-
ProtocolMetrics {
95-
// TCPv4
96-
tcp4_connections_handled: http_stats.tcp4_announces_handled() + http_stats.tcp4_scrapes_handled(),
97-
tcp4_announces_handled: http_stats.tcp4_announces_handled(),
98-
tcp4_scrapes_handled: http_stats.tcp4_scrapes_handled(),
99-
// TCPv6
100-
tcp6_connections_handled: http_stats.tcp6_announces_handled() + http_stats.tcp6_scrapes_handled(),
101-
tcp6_announces_handled: http_stats.tcp6_announces_handled(),
102-
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled(),
103-
// UDP
104-
udp_requests_aborted: udp_server_stats.udp_requests_aborted(),
105-
udp_requests_banned: udp_server_stats.udp_requests_banned(),
106-
udp_banned_ips_total: udp_banned_ips_total as u64,
107-
udp_avg_connect_processing_time_ns: udp_server_stats.udp_avg_connect_processing_time_ns(),
108-
udp_avg_announce_processing_time_ns: udp_server_stats.udp_avg_announce_processing_time_ns(),
109-
udp_avg_scrape_processing_time_ns: udp_server_stats.udp_avg_scrape_processing_time_ns(),
110-
// UDPv4
111-
udp4_requests: udp_server_stats.udp4_requests(),
112-
udp4_connections_handled: udp_server_stats.udp4_connections_handled(),
113-
udp4_announces_handled: udp_server_stats.udp4_announces_handled(),
114-
udp4_scrapes_handled: udp_server_stats.udp4_scrapes_handled(),
115-
udp4_responses: udp_server_stats.udp4_responses(),
116-
udp4_errors_handled: udp_server_stats.udp4_errors_handled(),
117-
// UDPv6
118-
udp6_requests: udp_server_stats.udp6_requests(),
119-
udp6_connections_handled: udp_server_stats.udp6_connections_handled(),
120-
udp6_announces_handled: udp_server_stats.udp6_announces_handled(),
121-
udp6_scrapes_handled: udp_server_stats.udp6_scrapes_handled(),
122-
udp6_responses: udp_server_stats.udp6_responses(),
123-
udp6_errors_handled: udp_server_stats.udp6_errors_handled(),
124-
}
125-
}
126-
12753
#[allow(deprecated)]
12854
#[allow(clippy::too_many_lines)]
129-
async fn get_protocol_metrics_from_labeled_metrics(
55+
async fn get_protocol_metrics(
13056
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
13157
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
13258
) -> ProtocolMetrics {
@@ -307,7 +233,7 @@ mod tests {
307233
let tracker_core_container =
308234
TrackerCoreContainer::initialize_from(&core_config, &swarm_coordination_registry_container.clone());
309235

310-
let ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
236+
let _ban_service = Arc::new(RwLock::new(BanService::new(MAX_CONNECTION_ID_ERRORS_PER_IP)));
311237

312238
// HTTP core stats
313239
let http_core_broadcaster = Broadcaster::default();
@@ -326,7 +252,6 @@ mod tests {
326252

327253
let tracker_metrics = get_metrics(
328254
tracker_core_container.in_memory_torrent_repository.clone(),
329-
ban_service.clone(),
330255
tracker_core_container.stats_repository.clone(),
331256
http_stats_repository.clone(),
332257
udp_server_stats_repository.clone(),

0 commit comments

Comments
 (0)