20
20
21
21
use crate :: protocol_stack;
22
22
use prometheus_client:: encoding:: text:: Encode ;
23
- use prometheus_client:: metrics:: counter:: Counter ;
24
- use prometheus_client:: metrics:: family:: Family ;
23
+ use prometheus_client:: metrics:: {
24
+ counter:: Counter ,
25
+ family:: Family ,
26
+ histogram:: { exponential_buckets, Histogram } ,
27
+ } ;
25
28
use prometheus_client:: registry:: Registry ;
26
29
27
30
pub struct Metrics {
28
31
connections_incoming : Family < AddressLabels , Counter > ,
29
32
connections_incoming_error : Family < IncomingConnectionErrorLabels , Counter > ,
30
33
31
34
connections_established : Family < ConnectionEstablishedLabels , Counter > ,
35
+ connections_establishment_duration : Family < ConnectionEstablishmentDurationLabels , Histogram > ,
32
36
connections_closed : Family < ConnectionClosedLabels , Counter > ,
33
37
34
38
new_listen_addr : Family < AddressLabels , Counter > ,
@@ -123,6 +127,15 @@ impl Metrics {
123
127
Box :: new ( connections_closed. clone ( ) ) ,
124
128
) ;
125
129
130
+ let connections_establishment_duration = Family :: new_with_constructor (
131
+ create_connection_establishment_duration_histogram as fn ( ) -> Histogram ,
132
+ ) ;
133
+ sub_registry. register (
134
+ "connections_establishment_duration" ,
135
+ "Time it took (locally) to establish connections" ,
136
+ Box :: new ( connections_establishment_duration. clone ( ) ) ,
137
+ ) ;
138
+
126
139
Self {
127
140
connections_incoming,
128
141
connections_incoming_error,
@@ -135,6 +148,7 @@ impl Metrics {
135
148
dial_attempt,
136
149
outgoing_connection_error,
137
150
connected_to_banned_peer,
151
+ connections_establishment_duration,
138
152
}
139
153
}
140
154
}
@@ -143,13 +157,19 @@ impl<TBvEv, THandleErr> super::Recorder<libp2p_swarm::SwarmEvent<TBvEv, THandleE
143
157
fn record ( & self , event : & libp2p_swarm:: SwarmEvent < TBvEv , THandleErr > ) {
144
158
match event {
145
159
libp2p_swarm:: SwarmEvent :: Behaviour ( _) => { }
146
- libp2p_swarm:: SwarmEvent :: ConnectionEstablished { endpoint, .. } => {
147
- self . connections_established
148
- . get_or_create ( & ConnectionEstablishedLabels {
149
- role : endpoint. into ( ) ,
150
- protocols : protocol_stack:: as_string ( endpoint. get_remote_address ( ) ) ,
151
- } )
152
- . inc ( ) ;
160
+ libp2p_swarm:: SwarmEvent :: ConnectionEstablished {
161
+ endpoint,
162
+ established_in : time_taken,
163
+ ..
164
+ } => {
165
+ let labels = ConnectionEstablishedLabels {
166
+ role : endpoint. into ( ) ,
167
+ protocols : protocol_stack:: as_string ( endpoint. get_remote_address ( ) ) ,
168
+ } ;
169
+ self . connections_established . get_or_create ( & labels) . inc ( ) ;
170
+ self . connections_establishment_duration
171
+ . get_or_create ( & labels)
172
+ . observe ( time_taken. as_secs_f64 ( ) ) ;
153
173
}
154
174
libp2p_swarm:: SwarmEvent :: ConnectionClosed { endpoint, .. } => {
155
175
self . connections_closed
@@ -279,6 +299,8 @@ struct ConnectionEstablishedLabels {
279
299
protocols : String ,
280
300
}
281
301
302
+ type ConnectionEstablishmentDurationLabels = ConnectionEstablishedLabels ;
303
+
282
304
#[ derive( Encode , Hash , Clone , Eq , PartialEq ) ]
283
305
struct ConnectionClosedLabels {
284
306
role : Role ,
@@ -372,3 +394,7 @@ impl<TTransErr> From<&libp2p_swarm::PendingInboundConnectionError<TTransErr>>
372
394
}
373
395
}
374
396
}
397
+
398
+ fn create_connection_establishment_duration_histogram ( ) -> Histogram {
399
+ Histogram :: new ( exponential_buckets ( 1e-3 , 2. , 10 ) )
400
+ }
0 commit comments