18
18
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19
19
// DEALINGS IN THE SOFTWARE.
20
20
21
+ use crate :: protocol_stack;
21
22
use prometheus_client:: encoding:: text:: Encode ;
22
23
use prometheus_client:: metrics:: counter:: Counter ;
23
24
use prometheus_client:: metrics:: family:: Family ;
24
25
use prometheus_client:: registry:: Registry ;
25
26
26
27
pub struct Metrics {
27
- connections_incoming : Counter ,
28
+ connections_incoming : Family < AddressLabels , Counter > ,
28
29
connections_incoming_error : Family < IncomingConnectionErrorLabels , Counter > ,
29
30
30
31
connections_established : Family < ConnectionEstablishedLabels , Counter > ,
31
32
connections_closed : Family < ConnectionClosedLabels , Counter > ,
32
33
33
- new_listen_addr : Counter ,
34
- expired_listen_addr : Counter ,
34
+ new_listen_addr : Family < AddressLabels , Counter > ,
35
+ expired_listen_addr : Family < AddressLabels , Counter > ,
35
36
36
- listener_closed : Counter ,
37
+ listener_closed : Family < AddressLabels , Counter > ,
37
38
listener_error : Counter ,
38
39
39
40
dial_attempt : Counter ,
40
41
outgoing_connection_error : Family < OutgoingConnectionErrorLabels , Counter > ,
41
- connected_to_banned_peer : Counter ,
42
+ connected_to_banned_peer : Family < AddressLabels , Counter > ,
42
43
}
43
44
44
45
impl Metrics {
45
46
pub fn new ( registry : & mut Registry ) -> Self {
46
47
let sub_registry = registry. sub_registry_with_prefix ( "swarm" ) ;
47
48
48
- let connections_incoming = Counter :: default ( ) ;
49
+ let connections_incoming = Family :: default ( ) ;
49
50
sub_registry. register (
50
51
"connections_incoming" ,
51
- "Number of incoming connections" ,
52
+ "Number of incoming connections per address stack " ,
52
53
Box :: new ( connections_incoming. clone ( ) ) ,
53
54
) ;
54
55
@@ -59,21 +60,21 @@ impl Metrics {
59
60
Box :: new ( connections_incoming_error. clone ( ) ) ,
60
61
) ;
61
62
62
- let new_listen_addr = Counter :: default ( ) ;
63
+ let new_listen_addr = Family :: default ( ) ;
63
64
sub_registry. register (
64
65
"new_listen_addr" ,
65
66
"Number of new listen addresses" ,
66
67
Box :: new ( new_listen_addr. clone ( ) ) ,
67
68
) ;
68
69
69
- let expired_listen_addr = Counter :: default ( ) ;
70
+ let expired_listen_addr = Family :: default ( ) ;
70
71
sub_registry. register (
71
72
"expired_listen_addr" ,
72
73
"Number of expired listen addresses" ,
73
74
Box :: new ( expired_listen_addr. clone ( ) ) ,
74
75
) ;
75
76
76
- let listener_closed = Counter :: default ( ) ;
77
+ let listener_closed = Family :: default ( ) ;
77
78
sub_registry. register (
78
79
"listener_closed" ,
79
80
"Number of listeners closed" ,
@@ -101,7 +102,7 @@ impl Metrics {
101
102
Box :: new ( outgoing_connection_error. clone ( ) ) ,
102
103
) ;
103
104
104
- let connected_to_banned_peer = Counter :: default ( ) ;
105
+ let connected_to_banned_peer = Family :: default ( ) ;
105
106
sub_registry. register (
106
107
"connected_to_banned_peer" ,
107
108
"Number of connection attempts to banned peer" ,
@@ -146,23 +147,34 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
146
147
self . connections_established
147
148
. get_or_create ( & ConnectionEstablishedLabels {
148
149
role : endpoint. into ( ) ,
150
+ protocols : protocol_stack:: as_string ( endpoint. get_remote_address ( ) ) ,
149
151
} )
150
152
. inc ( ) ;
151
153
}
152
154
libp2p_swarm:: SwarmEvent :: ConnectionClosed { endpoint, .. } => {
153
155
self . connections_closed
154
156
. get_or_create ( & ConnectionClosedLabels {
155
157
role : endpoint. into ( ) ,
158
+ protocols : protocol_stack:: as_string ( endpoint. get_remote_address ( ) ) ,
156
159
} )
157
160
. inc ( ) ;
158
161
}
159
- libp2p_swarm:: SwarmEvent :: IncomingConnection { .. } => {
160
- self . connections_incoming . inc ( ) ;
162
+ libp2p_swarm:: SwarmEvent :: IncomingConnection { send_back_addr, .. } => {
163
+ self . connections_incoming
164
+ . get_or_create ( & AddressLabels {
165
+ protocols : protocol_stack:: as_string ( send_back_addr) ,
166
+ } )
167
+ . inc ( ) ;
161
168
}
162
- libp2p_swarm:: SwarmEvent :: IncomingConnectionError { error, .. } => {
169
+ libp2p_swarm:: SwarmEvent :: IncomingConnectionError {
170
+ error,
171
+ send_back_addr,
172
+ ..
173
+ } => {
163
174
self . connections_incoming_error
164
175
. get_or_create ( & IncomingConnectionErrorLabels {
165
176
error : error. into ( ) ,
177
+ protocols : protocol_stack:: as_string ( send_back_addr) ,
166
178
} )
167
179
. inc ( ) ;
168
180
}
@@ -221,17 +233,35 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
221
233
}
222
234
} ;
223
235
}
224
- libp2p_swarm:: SwarmEvent :: BannedPeer { .. } => {
225
- self . connected_to_banned_peer . inc ( ) ;
236
+ libp2p_swarm:: SwarmEvent :: BannedPeer { endpoint, .. } => {
237
+ self . connected_to_banned_peer
238
+ . get_or_create ( & AddressLabels {
239
+ protocols : protocol_stack:: as_string ( endpoint. get_remote_address ( ) ) ,
240
+ } )
241
+ . inc ( ) ;
226
242
}
227
- libp2p_swarm:: SwarmEvent :: NewListenAddr { .. } => {
228
- self . new_listen_addr . inc ( ) ;
243
+ libp2p_swarm:: SwarmEvent :: NewListenAddr { address, .. } => {
244
+ self . new_listen_addr
245
+ . get_or_create ( & AddressLabels {
246
+ protocols : protocol_stack:: as_string ( address) ,
247
+ } )
248
+ . inc ( ) ;
229
249
}
230
- libp2p_swarm:: SwarmEvent :: ExpiredListenAddr { .. } => {
231
- self . expired_listen_addr . inc ( ) ;
250
+ libp2p_swarm:: SwarmEvent :: ExpiredListenAddr { address, .. } => {
251
+ self . expired_listen_addr
252
+ . get_or_create ( & AddressLabels {
253
+ protocols : protocol_stack:: as_string ( address) ,
254
+ } )
255
+ . inc ( ) ;
232
256
}
233
- libp2p_swarm:: SwarmEvent :: ListenerClosed { .. } => {
234
- self . listener_closed . inc ( ) ;
257
+ libp2p_swarm:: SwarmEvent :: ListenerClosed { addresses, .. } => {
258
+ for address in addresses {
259
+ self . listener_closed
260
+ . get_or_create ( & AddressLabels {
261
+ protocols : protocol_stack:: as_string ( address) ,
262
+ } )
263
+ . inc ( ) ;
264
+ }
235
265
}
236
266
libp2p_swarm:: SwarmEvent :: ListenerError { .. } => {
237
267
self . listener_error . inc ( ) ;
@@ -246,11 +276,18 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
246
276
#[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
247
277
struct ConnectionEstablishedLabels {
248
278
role : Role ,
279
+ protocols : String ,
249
280
}
250
281
251
282
#[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
252
283
struct ConnectionClosedLabels {
253
284
role : Role ,
285
+ protocols : String ,
286
+ }
287
+
288
+ #[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
289
+ struct AddressLabels {
290
+ protocols : String ,
254
291
}
255
292
256
293
#[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
@@ -298,6 +335,7 @@ enum OutgoingConnectionErrorError {
298
335
#[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
299
336
struct IncomingConnectionErrorLabels {
300
337
error : PendingInboundConnectionError ,
338
+ protocols : String ,
301
339
}
302
340
303
341
#[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
0 commit comments