Skip to content

Commit 7be0366

Browse files
committed
feat: [#1534] add new metric to count peers reverting state from complete to any other state
The metric is: ``` swarm_coordination_registry_peers_completed_state_reverted_total 1 ```
1 parent b2feb7b commit 7be0366

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

packages/swarm-coordination-registry/src/statistics/event/handler.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ use torrust_tracker_primitives::DurationSinceUnixEpoch;
88
use crate::event::Event;
99
use crate::statistics::repository::Repository;
1010
use crate::statistics::{
11-
SWARM_COORDINATION_REGISTRY_PEERS_ADDED_TOTAL, SWARM_COORDINATION_REGISTRY_PEERS_REMOVED_TOTAL,
12-
SWARM_COORDINATION_REGISTRY_PEERS_UPDATED_TOTAL, SWARM_COORDINATION_REGISTRY_PEER_CONNECTIONS_TOTAL,
13-
SWARM_COORDINATION_REGISTRY_TORRENTS_ADDED_TOTAL, SWARM_COORDINATION_REGISTRY_TORRENTS_DOWNLOADS_TOTAL,
14-
SWARM_COORDINATION_REGISTRY_TORRENTS_REMOVED_TOTAL, SWARM_COORDINATION_REGISTRY_TORRENTS_TOTAL,
11+
SWARM_COORDINATION_REGISTRY_PEERS_ADDED_TOTAL, SWARM_COORDINATION_REGISTRY_PEERS_COMPLETED_STATE_REVERTED_TOTAL,
12+
SWARM_COORDINATION_REGISTRY_PEERS_REMOVED_TOTAL, SWARM_COORDINATION_REGISTRY_PEERS_UPDATED_TOTAL,
13+
SWARM_COORDINATION_REGISTRY_PEER_CONNECTIONS_TOTAL, SWARM_COORDINATION_REGISTRY_TORRENTS_ADDED_TOTAL,
14+
SWARM_COORDINATION_REGISTRY_TORRENTS_DOWNLOADS_TOTAL, SWARM_COORDINATION_REGISTRY_TORRENTS_REMOVED_TOTAL,
15+
SWARM_COORDINATION_REGISTRY_TORRENTS_TOTAL,
1516
};
1617

1718
#[allow(clippy::too_many_lines)]
@@ -103,6 +104,8 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
103104
} => {
104105
tracing::debug!(info_hash = ?info_hash, old_peer = ?old_peer, new_peer = ?new_peer, "Peer updated", );
105106

107+
// If the peer's role has changed, we need to adjust the number of
108+
// connections
106109
if old_peer.role() != new_peer.role() {
107110
let _unused = stats_repository
108111
.increment_gauge(
@@ -121,6 +124,20 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
121124
.await;
122125
}
123126

127+
// If the peer reverted from a completed state to any other state,
128+
// we need to increment the counter for reverted completed.
129+
if old_peer.is_completed() && !new_peer.is_completed() {
130+
let _unused = stats_repository
131+
.increment_counter(
132+
&metric_name!(SWARM_COORDINATION_REGISTRY_PEERS_COMPLETED_STATE_REVERTED_TOTAL),
133+
&LabelSet::default(),
134+
now,
135+
)
136+
.await;
137+
}
138+
139+
// Regardless of the role change, we still need to increment the
140+
// counter for updated peers.
124141
let label_set = label_set_for_peer(&new_peer);
125142

126143
let _unused = stats_repository
@@ -134,7 +151,7 @@ pub async fn handle_event(event: Event, stats_repository: &Arc<Repository>, now:
134151
Event::PeerDownloadCompleted { info_hash, peer } => {
135152
tracing::debug!(info_hash = ?info_hash, peer = ?peer, "Peer download completed", );
136153

137-
let _unused = stats_repository
154+
let _unused: Result<(), torrust_tracker_metrics::metric_collection::Error> = stats_repository
138155
.increment_counter(
139156
&metric_name!(SWARM_COORDINATION_REGISTRY_TORRENTS_DOWNLOADS_TOTAL),
140157
&label_set_for_peer(&peer),

packages/swarm-coordination-registry/src/statistics/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ const SWARM_COORDINATION_REGISTRY_PEERS_UPDATED_TOTAL: &str = "swarm_coordinatio
2626
const SWARM_COORDINATION_REGISTRY_PEER_CONNECTIONS_TOTAL: &str = "swarm_coordination_registry_peer_connections_total";
2727
const SWARM_COORDINATION_REGISTRY_UNIQUE_PEERS_TOTAL: &str = "swarm_coordination_registry_unique_peers_total"; // todo: not implemented yet
2828
const SWARM_COORDINATION_REGISTRY_PEERS_INACTIVE_TOTAL: &str = "swarm_coordination_registry_peers_inactive_total";
29+
const SWARM_COORDINATION_REGISTRY_PEERS_COMPLETED_STATE_REVERTED_TOTAL: &str =
30+
"swarm_coordination_registry_peers_completed_state_reverted_total";
2931

3032
#[must_use]
3133
pub fn describe_metrics() -> Metrics {
@@ -103,5 +105,13 @@ pub fn describe_metrics() -> Metrics {
103105
Some(MetricDescription::new("The total number of inactive peers.")),
104106
);
105107

108+
metrics.metric_collection.describe_counter(
109+
&metric_name!(SWARM_COORDINATION_REGISTRY_PEERS_COMPLETED_STATE_REVERTED_TOTAL),
110+
Some(Unit::Count),
111+
Some(MetricDescription::new(
112+
"The total number of peers whose completed state was reverted.",
113+
)),
114+
);
115+
106116
metrics
107117
}

0 commit comments

Comments
 (0)