11pub use lighthouse_metrics:: * ;
22
33lazy_static ! {
4- pub static ref NAT_OPEN : Result <IntGaugeVec > = try_create_int_gauge_vec (
4+ pub static ref NAT_OPEN : Result <IntCounter > = try_create_int_counter (
55 "nat_open" ,
6- "An estimate indicating if the local node is reachable from external nodes" ,
7- & [ "protocol" ]
6+ "An estimate indicating if the local node is exposed to the internet."
87 ) ;
98 pub static ref ADDRESS_UPDATE_COUNT : Result <IntCounter > = try_create_int_counter(
109 "libp2p_address_update_total" ,
1110 "Count of libp2p socked updated events (when our view of our IP address has changed)"
1211 ) ;
13- pub static ref PEERS_CONNECTED : Result <IntGaugeVec > =
14- try_create_int_gauge_vec( "libp2p_peers" , "Count of libp2p peers currently connected" , & [ "direction" , "transport" ] ) ;
12+ pub static ref PEERS_CONNECTED : Result <IntGauge > = try_create_int_gauge(
13+ "libp2p_peers" ,
14+ "Count of libp2p peers currently connected"
15+ ) ;
16+
17+ pub static ref TCP_PEERS_CONNECTED : Result <IntGauge > = try_create_int_gauge(
18+ "libp2p_tcp_peers" ,
19+ "Count of libp2p peers currently connected via TCP"
20+ ) ;
21+
22+ pub static ref QUIC_PEERS_CONNECTED : Result <IntGauge > = try_create_int_gauge(
23+ "libp2p_quic_peers" ,
24+ "Count of libp2p peers currently connected via QUIC"
25+ ) ;
26+
1527 pub static ref PEER_CONNECT_EVENT_COUNT : Result <IntCounter > = try_create_int_counter(
1628 "libp2p_peer_connect_event_total" ,
1729 "Count of libp2p peer connect events (not the current number of connected peers)"
@@ -20,10 +32,13 @@ lazy_static! {
2032 "libp2p_peer_disconnect_event_total" ,
2133 "Count of libp2p peer disconnect events"
2234 ) ;
23- pub static ref DISCOVERY_BYTES : Result <IntGaugeVec > = try_create_int_gauge_vec(
24- "discovery_bytes" ,
25- "The number of bytes sent and received in discovery" ,
26- & [ "direction" ]
35+ pub static ref DISCOVERY_SENT_BYTES : Result <IntGauge > = try_create_int_gauge(
36+ "discovery_sent_bytes" ,
37+ "The number of bytes sent in discovery"
38+ ) ;
39+ pub static ref DISCOVERY_RECV_BYTES : Result <IntGauge > = try_create_int_gauge(
40+ "discovery_recv_bytes" ,
41+ "The number of bytes received in discovery"
2742 ) ;
2843 pub static ref DISCOVERY_QUEUE : Result <IntGauge > = try_create_int_gauge(
2944 "discovery_queue_size" ,
@@ -120,6 +135,17 @@ lazy_static! {
120135 & [ "type" ]
121136 ) ;
122137
138+ /*
139+ * Inbound/Outbound peers
140+ */
141+ /// The number of peers that dialed us.
142+ pub static ref NETWORK_INBOUND_PEERS : Result <IntGauge > =
143+ try_create_int_gauge( "network_inbound_peers" , "The number of peers that are currently connected that have dialed us." ) ;
144+
145+ /// The number of peers that we dialed us.
146+ pub static ref NETWORK_OUTBOUND_PEERS : Result <IntGauge > =
147+ try_create_int_gauge( "network_outbound_peers" , "The number of peers that are currently connected that we dialed." ) ;
148+
123149 /*
124150 * Peer Reporting
125151 */
@@ -130,11 +156,31 @@ lazy_static! {
130156 ) ;
131157}
132158
159+ /// Checks if we consider the NAT open.
160+ ///
161+ /// Conditions for an open NAT:
162+ /// 1. We have 1 or more SOCKET_UPDATED messages. This occurs when discovery has a majority of
163+ /// users reporting an external port and our ENR gets updated.
164+ /// 2. We have 0 SOCKET_UPDATED messages (can be true if the port was correct on boot), then we
165+ /// rely on whether we have any inbound messages. If we have no socket update messages, but
166+ /// manage to get at least one inbound peer, we are exposed correctly.
167+ pub fn check_nat ( ) {
168+ // NAT is already deemed open.
169+ if NAT_OPEN . as_ref ( ) . map ( |v| v. get ( ) ) . unwrap_or ( 0 ) != 0 {
170+ return ;
171+ }
172+ if ADDRESS_UPDATE_COUNT . as_ref ( ) . map ( |v| v. get ( ) ) . unwrap_or ( 0 ) != 0
173+ || NETWORK_INBOUND_PEERS . as_ref ( ) . map ( |v| v. get ( ) ) . unwrap_or ( 0 ) != 0_i64
174+ {
175+ inc_counter ( & NAT_OPEN ) ;
176+ }
177+ }
178+
133179pub fn scrape_discovery_metrics ( ) {
134180 let metrics =
135181 discv5:: metrics:: Metrics :: from ( discv5:: Discv5 :: < discv5:: DefaultProtocolId > :: raw_metrics ( ) ) ;
136182 set_float_gauge ( & DISCOVERY_REQS , metrics. unsolicited_requests_per_second ) ;
137183 set_gauge ( & DISCOVERY_SESSIONS , metrics. active_sessions as i64 ) ;
138- set_gauge_vec ( & DISCOVERY_BYTES , & [ "inbound" ] , metrics. bytes_recv as i64 ) ;
139- set_gauge_vec ( & DISCOVERY_BYTES , & [ "outbound" ] , metrics. bytes_sent as i64 ) ;
184+ set_gauge ( & DISCOVERY_SENT_BYTES , metrics. bytes_sent as i64 ) ;
185+ set_gauge ( & DISCOVERY_RECV_BYTES , metrics. bytes_recv as i64 ) ;
140186}
0 commit comments