@@ -34,7 +34,7 @@ use prometheus_client::{
34
34
35
35
use crate :: protocol_stack;
36
36
37
- const ALLOWED_PROTOCOLS : & [ StreamProtocol ] = & [
37
+ const BASE_ALLOWED_PROTOCOLS : & [ StreamProtocol ] = & [
38
38
#[ cfg( feature = "dcutr" ) ]
39
39
libp2p_dcutr:: PROTOCOL_NAME ,
40
40
// NOTE: Not including gossipsub here as users may configure custom protocol IDs
@@ -62,8 +62,57 @@ pub(crate) struct Metrics {
62
62
impl Metrics {
63
63
pub ( crate ) fn new ( registry : & mut Registry ) -> Self {
64
64
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
+ ) ;
65
90
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 ( ) ) ;
67
116
sub_registry. register_collector ( Box :: new ( peers. clone ( ) ) ) ;
68
117
69
118
let error = Counter :: default ( ) ;
@@ -142,16 +191,29 @@ impl<TBvEv> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv>> for Metrics {
142
191
}
143
192
}
144
193
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
+ }
147
202
148
203
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
+
149
211
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) ;
151
213
}
152
214
153
215
fn remove ( & self , peer_id : PeerId ) {
154
- self . 0 . lock ( ) . unwrap ( ) . remove ( & peer_id) ;
216
+ self . 0 . infos . lock ( ) . unwrap ( ) . remove ( & peer_id) ;
155
217
}
156
218
}
157
219
@@ -161,13 +223,15 @@ impl Collector for Peers {
161
223
let mut count_by_listen_addresses: HashMap < String , i64 > = Default :: default ( ) ;
162
224
let mut count_by_observed_addresses: HashMap < String , i64 > = Default :: default ( ) ;
163
225
164
- for ( _, peer_info) in self . 0 . lock ( ) . unwrap ( ) . iter ( ) {
226
+ for ( _, peer_info) in self . 0 . infos . lock ( ) . unwrap ( ) . iter ( ) {
165
227
{
166
228
let mut protocols: Vec < _ > = peer_info
167
229
. protocols
168
230
. iter ( )
169
231
. map ( |p| {
170
- if ALLOWED_PROTOCOLS . contains ( p) {
232
+ if BASE_ALLOWED_PROTOCOLS . contains ( p)
233
+ || self . 0 . extra_allowed_protocols . contains ( p)
234
+ {
171
235
p. to_string ( )
172
236
} else {
173
237
"unrecognized" . to_string ( )
0 commit comments