Skip to content

Commit 476ece4

Browse files
committed
refactor: [#1446] WIP. Calculate global metrics from labeled metrics
We need to add a new label to make it easier to fileter by the server IP family: IPV4 or IPv6.
1 parent 0d13439 commit 476ece4

File tree

8 files changed

+135
-8
lines changed

8 files changed

+135
-8
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
3232
.increase_counter(&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL), &label_set, now)
3333
.await
3434
{
35-
Ok(()) => {}
35+
Ok(()) => {
36+
tracing::debug!(
37+
"Successfully increased the counter for HTTP announce requests received: {}",
38+
label_set
39+
);
40+
}
3641
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
3742
};
3843
}
@@ -57,7 +62,12 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
5762
.increase_counter(&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL), &label_set, now)
5863
.await
5964
{
60-
Ok(()) => {}
65+
Ok(()) => {
66+
tracing::debug!(
67+
"Successfully increased the counter for HTTP scrape requests received: {}",
68+
label_set
69+
);
70+
}
6171
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
6272
};
6373
}

packages/http-tracker-core/src/statistics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use torrust_tracker_metrics::metric::description::MetricDescription;
88
use torrust_tracker_metrics::metric_name;
99
use torrust_tracker_metrics::unit::Unit;
1010

11-
const HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL: &str = "http_tracker_core_requests_received_total";
11+
pub const HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL: &str = "http_tracker_core_requests_received_total";
1212

1313
#[must_use]
1414
pub fn describe_metrics() -> Metrics {

packages/metrics/src/aggregate.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use derive_more::Display;
22

3-
#[derive(Debug, Display, Clone, Copy, PartialEq)]
3+
#[derive(Debug, Display, Clone, Copy, PartialEq, Default)]
44
pub struct AggregateValue(f64);
55

66
impl AggregateValue {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ 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"
2627

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

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

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
use std::sync::Arc;
22

3+
use bittorrent_http_tracker_core::statistics::HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL;
34
use bittorrent_tracker_core::torrent::repository::in_memory::InMemoryTorrentRepository;
45
use bittorrent_udp_tracker_core::services::banning::BanService;
56
use bittorrent_udp_tracker_core::{self};
67
use tokio::sync::RwLock;
8+
use torrust_tracker_metrics::metric_collection::aggregate::Sum;
79
use torrust_tracker_metrics::metric_collection::MetricCollection;
8-
use torrust_udp_tracker_server::statistics as udp_server_statistics;
10+
use torrust_tracker_metrics::metric_name;
11+
use torrust_udp_tracker_server::statistics::{self as udp_server_statistics, UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL};
912

1013
use super::metrics::TorrentsMetrics;
1114
use crate::statistics::metrics::ProtocolMetrics;
@@ -32,9 +35,38 @@ pub async fn get_metrics(
3235
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
3336
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
3437
) -> TrackerMetrics {
38+
let protocol_metrics_from_global_metrics = get_protocol_metrics(
39+
ban_service.clone(),
40+
http_stats_repository.clone(),
41+
udp_server_stats_repository.clone(),
42+
)
43+
.await;
44+
45+
let protocol_metrics_from_labeled_metrics = get_protocol_metrics_from_labeled_metrics(
46+
ban_service.clone(),
47+
http_stats_repository.clone(),
48+
udp_server_stats_repository.clone(),
49+
)
50+
.await;
51+
52+
// todo:
53+
// We keep both metrics until we deploy to production and we can
54+
// ensure that the protocol metrics from labeled metrics are correct.
55+
// After that we can remove the `get_protocol_metrics` function and
56+
// use only the `get_protocol_metrics_from_labeled_metrics` function.
57+
// And also remove the code in repositories to generate the global metrics.
58+
let protocol_metrics = if protocol_metrics_from_global_metrics == protocol_metrics_from_labeled_metrics {
59+
protocol_metrics_from_labeled_metrics
60+
} else {
61+
// tracing::warn!("The protocol metrics from global metrics and labeled metrics are different");
62+
// tracing::warn!("Global metrics: {:?}", protocol_metrics_from_global_metrics);
63+
// tracing::warn!("Labeled metrics: {:?}", protocol_metrics_from_labeled_metrics);
64+
protocol_metrics_from_global_metrics
65+
};
66+
3567
TrackerMetrics {
3668
torrents_metrics: get_torrents_metrics(in_memory_torrent_repository, tracker_core_stats_repository).await,
37-
protocol_metrics: get_protocol_metrics(ban_service, http_stats_repository, udp_server_stats_repository).await,
69+
protocol_metrics,
3870
}
3971
}
4072

@@ -99,6 +131,87 @@ async fn get_protocol_metrics(
99131
}
100132
}
101133

134+
#[allow(deprecated)]
135+
async fn get_protocol_metrics_from_labeled_metrics(
136+
ban_service: Arc<RwLock<BanService>>,
137+
http_stats_repository: Arc<bittorrent_http_tracker_core::statistics::repository::Repository>,
138+
udp_server_stats_repository: Arc<udp_server_statistics::repository::Repository>,
139+
) -> ProtocolMetrics {
140+
let udp_banned_ips_total = ban_service.read().await.get_banned_ips_total();
141+
let http_stats = http_stats_repository.get_stats().await;
142+
let udp_server_stats = udp_server_stats_repository.get_stats().await;
143+
144+
#[allow(clippy::cast_sign_loss)]
145+
#[allow(clippy::cast_possible_truncation)]
146+
let tcp4_announces_handled = http_stats
147+
.metric_collection
148+
.sum(
149+
&metric_name!(HTTP_TRACKER_CORE_REQUESTS_RECEIVED_TOTAL),
150+
&[("request_kind", "announce")].into(), // todo: add label for `server_binding_ip_family` with value `inet` (inet/inet6)
151+
)
152+
.unwrap_or_default()
153+
.value() as u64;
154+
155+
#[allow(clippy::cast_sign_loss)]
156+
#[allow(clippy::cast_possible_truncation)]
157+
let udp4_announces_handled = udp_server_stats
158+
.metric_collection
159+
.sum(
160+
&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL),
161+
&[("request_kind", "announce")].into(), // todo: add label for `server_binding_ip_family` with value `inet` (inet/inet6)
162+
)
163+
.unwrap_or_default()
164+
.value() as u64;
165+
166+
/*
167+
168+
todo:
169+
170+
- Add a label for `server_binding_ip_family` with value `inet` (inet/inet6)
171+
to all metrics containing an IP address. This will allow us to distinguish
172+
between IPv4 and IPv6 metrics.
173+
- Continue replacing the other metrics with the labeled metrics.
174+
175+
*/
176+
177+
// For backward compatibility we keep the `tcp4_connections_handled` and
178+
// `tcp6_connections_handled` metrics. They don't make sense for the HTTP
179+
// tracker, but we keep them for now. In new major versions we should remove
180+
// them.
181+
182+
ProtocolMetrics {
183+
// TCPv4
184+
tcp4_connections_handled: tcp4_announces_handled + http_stats.tcp4_scrapes_handled,
185+
tcp4_announces_handled,
186+
tcp4_scrapes_handled: http_stats.tcp4_scrapes_handled,
187+
// TCPv6
188+
tcp6_connections_handled: http_stats.tcp6_announces_handled + http_stats.tcp6_scrapes_handled,
189+
tcp6_announces_handled: http_stats.tcp6_announces_handled,
190+
tcp6_scrapes_handled: http_stats.tcp6_scrapes_handled,
191+
// UDP
192+
udp_requests_aborted: udp_server_stats.udp_requests_aborted,
193+
udp_requests_banned: udp_server_stats.udp_requests_banned,
194+
udp_banned_ips_total: udp_banned_ips_total as u64,
195+
udp_avg_connect_processing_time_ns: udp_server_stats.udp_avg_connect_processing_time_ns,
196+
udp_avg_announce_processing_time_ns: udp_server_stats.udp_avg_announce_processing_time_ns,
197+
udp_avg_scrape_processing_time_ns: udp_server_stats.udp_avg_scrape_processing_time_ns,
198+
// UDPv4
199+
udp4_requests: udp_server_stats.udp4_requests,
200+
udp4_connections_handled: udp_server_stats.udp4_connections_handled,
201+
udp4_announces_handled,
202+
udp4_scrapes_handled: udp_server_stats.udp4_scrapes_handled,
203+
udp4_responses: udp_server_stats.udp4_responses,
204+
udp4_errors_handled: udp_server_stats.udp4_errors_handled,
205+
// UDPv6
206+
udp6_requests: udp_server_stats.udp6_requests,
207+
udp6_connections_handled: udp_server_stats.udp6_connections_handled,
208+
udp6_announces_handled: udp_server_stats.udp6_announces_handled,
209+
udp6_scrapes_handled: udp_server_stats.udp6_scrapes_handled,
210+
udp6_responses: udp_server_stats.udp6_responses,
211+
udp6_errors_handled: udp_server_stats.udp6_errors_handled,
212+
}
213+
}
214+
102215
#[derive(Debug, PartialEq)]
103216
pub struct TrackerLabeledMetrics {
104217
pub metrics: MetricCollection,

packages/udp-tracker-server/src/statistics/event/handler/request_accepted.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ pub async fn handle_event(
4747
.increase_counter(&metric_name!(UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL), &label_set, now)
4848
.await
4949
{
50-
Ok(()) => {}
50+
Ok(()) => {
51+
tracing::debug!("Successfully increased the counter for UDP requests accepted: {}", label_set);
52+
}
5153
Err(err) => tracing::error!("Failed to increase the counter: {}", err),
5254
};
5355
}

packages/udp-tracker-server/src/statistics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const UDP_TRACKER_SERVER_REQUESTS_BANNED_TOTAL: &str = "udp_tracker_server_reque
1313
pub(crate) const UDP_TRACKER_SERVER_IPS_BANNED_TOTAL: &str = "udp_tracker_server_ips_banned_total";
1414
const UDP_TRACKER_SERVER_CONNECTION_ID_ERRORS_TOTAL: &str = "udp_tracker_server_connection_id_errors_total";
1515
const UDP_TRACKER_SERVER_REQUESTS_RECEIVED_TOTAL: &str = "udp_tracker_server_requests_received_total";
16-
const UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL: &str = "udp_tracker_server_requests_accepted_total";
16+
pub const UDP_TRACKER_SERVER_REQUESTS_ACCEPTED_TOTAL: &str = "udp_tracker_server_requests_accepted_total";
1717
const UDP_TRACKER_SERVER_RESPONSES_SENT_TOTAL: &str = "udp_tracker_server_responses_sent_total";
1818
const UDP_TRACKER_SERVER_ERRORS_TOTAL: &str = "udp_tracker_server_errors_total";
1919
const UDP_TRACKER_SERVER_PERFORMANCE_AVG_PROCESSING_TIME_NS: &str = "udp_tracker_server_performance_avg_processing_time_ns";

0 commit comments

Comments
 (0)