Skip to content

Commit a5a35ee

Browse files
authored
Update identify.rs
1 parent 6b5bea5 commit a5a35ee

File tree

1 file changed

+72
-8
lines changed

1 file changed

+72
-8
lines changed

misc/metrics/src/identify.rs

Lines changed: 72 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use prometheus_client::{
3434

3535
use crate::protocol_stack;
3636

37-
const ALLOWED_PROTOCOLS: &[StreamProtocol] = &[
37+
const BASE_ALLOWED_PROTOCOLS: &[StreamProtocol] = &[
3838
#[cfg(feature = "dcutr")]
3939
libp2p_dcutr::PROTOCOL_NAME,
4040
// NOTE: Not including gossipsub here as users may configure custom protocol IDs
@@ -62,8 +62,57 @@ pub(crate) struct Metrics {
6262
impl Metrics {
6363
pub(crate) fn new(registry: &mut Registry) -> Self {
6464
let sub_registry = registry.sub_registry_with_prefix("identify");
65+
let peers = Peers::new(Vec::new());
66+
sub_registry.register_collector(Box::new(peers.clone()));
67+
68+
let error = Counter::default();
69+
sub_registry.register(
70+
"errors",
71+
"Number of errors while attempting to identify the remote",
72+
error.clone(),
73+
);
74+
75+
let pushed = Counter::default();
76+
sub_registry.register(
77+
"pushed",
78+
"Number of times identification information of the local node has \
79+
been actively pushed to a peer.",
80+
pushed.clone(),
81+
);
82+
83+
let received = Counter::default();
84+
sub_registry.register(
85+
"received",
86+
"Number of times identification information has been received from \
87+
a peer",
88+
received.clone(),
89+
);
6590

66-
let peers = Peers::default();
91+
let sent = Counter::default();
92+
sub_registry.register(
93+
"sent",
94+
"Number of times identification information of the local node has \
95+
been sent to a peer in response to an identification request",
96+
sent.clone(),
97+
);
98+
99+
Self {
100+
peers,
101+
error,
102+
pushed,
103+
received,
104+
sent,
105+
}
106+
}
107+
108+
/// Create Identify metrics with additional allowed protocols used for classification.
109+
pub(crate) fn new_with_allowed_protocols(
110+
registry: &mut Registry,
111+
extra_allowed_protocols: impl IntoIterator<Item = StreamProtocol>,
112+
) -> Self {
113+
let sub_registry = registry.sub_registry_with_prefix("identify");
114+
115+
let peers = Peers::new(extra_allowed_protocols.into_iter().collect());
67116
sub_registry.register_collector(Box::new(peers.clone()));
68117

69118
let error = Counter::default();
@@ -142,16 +191,29 @@ impl<TBvEv> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv>> for Metrics {
142191
}
143192
}
144193

145-
#[derive(Default, Debug, Clone)]
146-
struct Peers(Arc<Mutex<HashMap<PeerId, libp2p_identify::Info>>>);
194+
#[derive(Debug, Clone)]
195+
struct Peers(Arc<PeersInner>);
196+
197+
#[derive(Debug)]
198+
struct PeersInner {
199+
infos: Mutex<HashMap<PeerId, libp2p_identify::Info>>,
200+
extra_allowed_protocols: Vec<StreamProtocol>,
201+
}
147202

148203
impl Peers {
204+
fn new(extra_allowed_protocols: Vec<StreamProtocol>) -> Self {
205+
Self(Arc::new(PeersInner {
206+
infos: Mutex::new(HashMap::new()),
207+
extra_allowed_protocols,
208+
}))
209+
}
210+
149211
fn record(&self, peer_id: PeerId, info: libp2p_identify::Info) {
150-
self.0.lock().unwrap().insert(peer_id, info);
212+
self.0.infos.lock().unwrap().insert(peer_id, info);
151213
}
152214

153215
fn remove(&self, peer_id: PeerId) {
154-
self.0.lock().unwrap().remove(&peer_id);
216+
self.0.infos.lock().unwrap().remove(&peer_id);
155217
}
156218
}
157219

@@ -161,13 +223,15 @@ impl Collector for Peers {
161223
let mut count_by_listen_addresses: HashMap<String, i64> = Default::default();
162224
let mut count_by_observed_addresses: HashMap<String, i64> = Default::default();
163225

164-
for (_, peer_info) in self.0.lock().unwrap().iter() {
226+
for (_, peer_info) in self.0.infos.lock().unwrap().iter() {
165227
{
166228
let mut protocols: Vec<_> = peer_info
167229
.protocols
168230
.iter()
169231
.map(|p| {
170-
if ALLOWED_PROTOCOLS.contains(p) {
232+
if BASE_ALLOWED_PROTOCOLS.contains(p)
233+
|| self.0.extra_allowed_protocols.contains(p)
234+
{
171235
p.to_string()
172236
} else {
173237
"unrecognized".to_string()

0 commit comments

Comments
 (0)