Skip to content

Commit 9d09337

Browse files
committed
feat: [#1403] add extendable-labeled metrics to udp-tracker-core
1 parent d717818 commit 9d09337

12 files changed

Lines changed: 187 additions & 32 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 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/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use torrust_tracker_clock::clock;
2-
31
pub mod container;
42
pub mod event;
53
pub mod services;
64
pub mod statistics;
75

6+
use torrust_tracker_clock::clock;
7+
88
/// This code needs to be copied into each crate.
99
/// Working version, for production.
1010
#[cfg(not(test))]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ pub fn describe_metrics() -> Metrics {
1717
metrics.metric_collection.describe_counter(
1818
&MetricName::new("http_tracker_core_announce_requests_received_total"),
1919
Some(Unit::Count),
20-
Some(MetricDescription::new("Total number of announce requests received")),
20+
Some(MetricDescription::new("Total number of HTTP announce requests received")),
2121
);
2222

2323
metrics.metric_collection.describe_counter(
2424
&MetricName::new("http_tracker_core_scrape_requests_received_total"),
2525
Some(Unit::Count),
26-
Some(MetricDescription::new("Total number of scrape requests received")),
26+
Some(MetricDescription::new("Total number of HTTP scrape requests received")),
2727
);
2828

2929
metrics

packages/udp-tracker-core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@ criterion = { version = "0.5.1", features = ["async_tokio"] }
2525
futures = "0"
2626
lazy_static = "1"
2727
rand = "0"
28+
serde = "1.0.219"
2829
thiserror = "2"
2930
tokio = { version = "1", features = ["macros", "net", "rt-multi-thread", "signal", "sync", "time"] }
31+
torrust-tracker-clock = { version = "3.0.0-develop", path = "../clock" }
3032
torrust-tracker-configuration = { version = "3.0.0-develop", path = "../configuration" }
33+
torrust-tracker-metrics = { version = "3.0.0-develop", path = "../metrics" }
3134
torrust-tracker-primitives = { version = "3.0.0-develop", path = "../primitives" }
3235
tracing = "0"
3336
zerocopy = "0.7"
@@ -39,4 +42,3 @@ torrust-tracker-test-helpers = { version = "3.0.0-develop", path = "../test-help
3942
[[bench]]
4043
harness = false
4144
name = "udp_tracker_core_benchmark"
42-

packages/udp-tracker-core/src/event/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::net::SocketAddr;
22

3+
use torrust_tracker_metrics::label::{LabelName, LabelSet, LabelValue};
34
use torrust_tracker_primitives::service_binding::ServiceBinding;
45

56
pub mod sender;
@@ -37,3 +38,22 @@ impl ConnectionContext {
3738
self.server_service_binding.bind_address()
3839
}
3940
}
41+
42+
impl From<ConnectionContext> for LabelSet {
43+
fn from(connection_context: ConnectionContext) -> Self {
44+
LabelSet::from([
45+
(
46+
LabelName::new("server_binding_protocol"),
47+
LabelValue::new(&connection_context.server_service_binding.protocol().to_string()),
48+
),
49+
(
50+
LabelName::new("server_binding_ip"),
51+
LabelValue::new(&connection_context.server_service_binding.bind_address().ip().to_string()),
52+
),
53+
(
54+
LabelName::new("server_binding_port"),
55+
LabelValue::new(&connection_context.server_service_binding.bind_address().port().to_string()),
56+
),
57+
])
58+
}
59+
}

packages/udp-tracker-core/src/lib.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ pub mod event;
55
pub mod services;
66
pub mod statistics;
77

8+
use torrust_tracker_clock::clock;
9+
10+
/// This code needs to be copied into each crate.
11+
/// Working version, for production.
12+
#[cfg(not(test))]
13+
#[allow(dead_code)]
14+
pub(crate) type CurrentClock = clock::Working;
15+
16+
/// Stopped version, for testing.
17+
#[cfg(test)]
18+
#[allow(dead_code)]
19+
pub(crate) type CurrentClock = clock::Stopped;
20+
821
use crypto::ephemeral_instance_keys;
922
use tracing::instrument;
1023

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

Lines changed: 76 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,81 @@
1+
use torrust_tracker_metrics::label::LabelSet;
2+
use torrust_tracker_metrics::metric::MetricName;
3+
use torrust_tracker_primitives::DurationSinceUnixEpoch;
4+
15
use crate::event::Event;
26
use crate::statistics::repository::Repository;
37

48
/// # Panics
59
///
610
/// This function panics if the IP version does not match the event type.
7-
pub async fn handle_event(event: Event, stats_repository: &Repository) {
11+
pub async fn handle_event(event: Event, stats_repository: &Repository, now: DurationSinceUnixEpoch) {
812
match event {
9-
Event::UdpConnect { context } => match context.client_socket_addr.ip() {
10-
std::net::IpAddr::V4(_) => {
11-
stats_repository.increase_udp4_connections().await;
12-
}
13-
std::net::IpAddr::V6(_) => {
14-
stats_repository.increase_udp6_connections().await;
15-
}
16-
},
17-
Event::UdpAnnounce { context } => match context.client_socket_addr.ip() {
18-
std::net::IpAddr::V4(_) => {
19-
stats_repository.increase_udp4_announces().await;
20-
}
21-
std::net::IpAddr::V6(_) => {
22-
stats_repository.increase_udp6_announces().await;
13+
Event::UdpConnect { context } => {
14+
// Global fixed metrics
15+
16+
match context.client_socket_addr.ip() {
17+
std::net::IpAddr::V4(_) => {
18+
stats_repository.increase_udp4_connections().await;
19+
}
20+
std::net::IpAddr::V6(_) => {
21+
stats_repository.increase_udp6_connections().await;
22+
}
2323
}
24-
},
25-
Event::UdpScrape { context } => match context.client_socket_addr.ip() {
26-
std::net::IpAddr::V4(_) => {
27-
stats_repository.increase_udp4_scrapes().await;
24+
25+
// Extendable metrics
26+
27+
stats_repository
28+
.increase_counter(
29+
&MetricName::new("udp_tracker_core_connect_requests_received_total"),
30+
&LabelSet::from(context),
31+
now,
32+
)
33+
.await;
34+
}
35+
Event::UdpAnnounce { context } => {
36+
// Global fixed metrics
37+
38+
match context.client_socket_addr.ip() {
39+
std::net::IpAddr::V4(_) => {
40+
stats_repository.increase_udp4_announces().await;
41+
}
42+
std::net::IpAddr::V6(_) => {
43+
stats_repository.increase_udp6_announces().await;
44+
}
2845
}
29-
std::net::IpAddr::V6(_) => {
30-
stats_repository.increase_udp6_scrapes().await;
46+
47+
// Extendable metrics
48+
49+
stats_repository
50+
.increase_counter(
51+
&MetricName::new("udp_tracker_core_announce_requests_received_total"),
52+
&LabelSet::from(context),
53+
now,
54+
)
55+
.await;
56+
}
57+
Event::UdpScrape { context } => {
58+
// Global fixed metrics
59+
60+
match context.client_socket_addr.ip() {
61+
std::net::IpAddr::V4(_) => {
62+
stats_repository.increase_udp4_scrapes().await;
63+
}
64+
std::net::IpAddr::V6(_) => {
65+
stats_repository.increase_udp6_scrapes().await;
66+
}
3167
}
32-
},
68+
69+
// Extendable metrics
70+
71+
stats_repository
72+
.increase_counter(
73+
&MetricName::new("udp_tracker_core_scrape_requests_received_total"),
74+
&LabelSet::from(context),
75+
now,
76+
)
77+
.await;
78+
}
3379
}
3480

3581
tracing::debug!("stats: {:?}", stats_repository.get_stats().await);
@@ -39,11 +85,13 @@ pub async fn handle_event(event: Event, stats_repository: &Repository) {
3985
mod tests {
4086
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
4187

88+
use torrust_tracker_clock::clock::Time;
4289
use torrust_tracker_primitives::service_binding::{Protocol, ServiceBinding};
4390

4491
use crate::event::{ConnectionContext, Event};
4592
use crate::statistics::event::handler::handle_event;
4693
use crate::statistics::repository::Repository;
94+
use crate::CurrentClock;
4795

4896
#[tokio::test]
4997
async fn should_increase_the_udp4_connections_counter_when_it_receives_a_udp4_connect_event() {
@@ -61,6 +109,7 @@ mod tests {
61109
),
62110
},
63111
&stats_repository,
112+
CurrentClock::now(),
64113
)
65114
.await;
66115

@@ -85,6 +134,7 @@ mod tests {
85134
),
86135
},
87136
&stats_repository,
137+
CurrentClock::now(),
88138
)
89139
.await;
90140

@@ -109,6 +159,7 @@ mod tests {
109159
),
110160
},
111161
&stats_repository,
162+
CurrentClock::now(),
112163
)
113164
.await;
114165

@@ -133,6 +184,7 @@ mod tests {
133184
),
134185
},
135186
&stats_repository,
187+
CurrentClock::now(),
136188
)
137189
.await;
138190

@@ -157,6 +209,7 @@ mod tests {
157209
),
158210
},
159211
&stats_repository,
212+
CurrentClock::now(),
160213
)
161214
.await;
162215

@@ -181,6 +234,7 @@ mod tests {
181234
),
182235
},
183236
&stats_repository,
237+
CurrentClock::now(),
184238
)
185239
.await;
186240

packages/udp-tracker-core/src/statistics/event/listener.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
use tokio::sync::broadcast;
2+
use torrust_tracker_clock::clock::Time;
23

34
use super::handler::handle_event;
45
use crate::event::Event;
56
use crate::statistics::repository::Repository;
7+
use crate::CurrentClock;
68

79
pub async fn dispatch_events(mut receiver: broadcast::Receiver<Event>, stats_repository: Repository) {
810
loop {
911
match receiver.recv().await {
10-
Ok(event) => handle_event(event, &stats_repository).await,
12+
Ok(event) => handle_event(event, &stats_repository, CurrentClock::now()).await,
1113
Err(e) => {
1214
tracing::error!("Error receiving udp tracker core event: {:?}", e);
1315
break;

packages/udp-tracker-core/src/statistics/metrics.rs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
use serde::Serialize;
2+
use torrust_tracker_metrics::label::LabelSet;
3+
use torrust_tracker_metrics::metric::MetricName;
4+
use torrust_tracker_metrics::metric_collection::MetricCollection;
5+
use torrust_tracker_primitives::DurationSinceUnixEpoch;
6+
17
/// Metrics collected by the tracker.
28
///
39
/// - Number of connections handled
@@ -6,7 +12,7 @@
612
///
713
/// These metrics are collected for each connection type: UDP and HTTP
814
/// and also for each IP version used by the peers: IPv4 and IPv6.
9-
#[derive(Debug, PartialEq, Default)]
15+
#[derive(Debug, PartialEq, Default, Serialize)]
1016
pub struct Metrics {
1117
/// Total number of UDP (UDP tracker) connections from IPv4 peers.
1218
pub udp4_connections_handled: u64,
@@ -25,4 +31,17 @@ pub struct Metrics {
2531

2632
/// Total number of UDP (UDP tracker) `scrape` requests from IPv6 peers.
2733
pub udp6_scrapes_handled: u64,
34+
35+
/// A collection of metrics.
36+
pub metric_collection: MetricCollection,
37+
}
38+
39+
impl Metrics {
40+
pub fn increase_counter(&mut self, metric_name: &MetricName, labels: &LabelSet, now: DurationSinceUnixEpoch) {
41+
self.metric_collection.increase_counter(metric_name, labels, now);
42+
}
43+
44+
pub fn set_gauge(&mut self, metric_name: &MetricName, labels: &LabelSet, value: f64, now: DurationSinceUnixEpoch) {
45+
self.metric_collection.set_gauge(metric_name, labels, value, now);
46+
}
2847
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,33 @@ pub mod metrics;
44
pub mod repository;
55
pub mod services;
66
pub mod setup;
7+
8+
use metrics::Metrics;
9+
use torrust_tracker_metrics::metric::description::MetricDescription;
10+
use torrust_tracker_metrics::metric::MetricName;
11+
use torrust_tracker_metrics::unit::Unit;
12+
13+
#[must_use]
14+
pub fn describe_metrics() -> Metrics {
15+
let mut metrics = Metrics::default();
16+
17+
metrics.metric_collection.describe_counter(
18+
&MetricName::new("udp_tracker_core_connect_requests_received_total"),
19+
Some(Unit::Count),
20+
Some(MetricDescription::new("Total number of UDP connect requests received")),
21+
);
22+
23+
metrics.metric_collection.describe_counter(
24+
&MetricName::new("udp_tracker_core_announce_requests_received_total"),
25+
Some(Unit::Count),
26+
Some(MetricDescription::new("Total number of UDP announce requests received")),
27+
);
28+
29+
metrics.metric_collection.describe_counter(
30+
&MetricName::new("udp_tracker_core_scrape_requests_received_total"),
31+
Some(Unit::Count),
32+
Some(MetricDescription::new("Total number of UDP scrape requests received")),
33+
);
34+
35+
metrics
36+
}

0 commit comments

Comments
 (0)