@@ -6,10 +6,13 @@ use libp2p::swarm::{NetworkBehaviour, SwarmEvent};
66use libp2p:: {
77 autonat, identify, identity, kad, ping, relay, rendezvous, Multiaddr , StreamProtocol , Swarm ,
88} ;
9+ use libp2p_metrics:: { Metrics , Recorder , Registry } ;
910use tracing:: info;
1011use tracing_subscriber:: prelude:: * ;
1112use tracing_subscriber:: EnvFilter ;
1213
14+ mod http_service;
15+
1316const PROTOCOL_VERSION : & str = concat ! ( "/" , env!( "CARGO_PKG_NAME" ) , "/" , env!( "CARGO_PKG_VERSION" ) ) ;
1417const CALIMERO_KAD_PROTO_NAME : StreamProtocol = StreamProtocol :: new ( "/calimero/kad/1.0.0" ) ;
1518const MAX_RELAY_CIRCUIT_BYTES : u64 = 100 << 20 ; // 100 MiB
@@ -56,6 +59,8 @@ async fn main() -> eyre::Result<()> {
5659
5760 info ! ( "Peer id: {:?}" , peer_id) ;
5861
62+ let mut metric_registry = Registry :: default ( ) ;
63+
5964 let mut swarm = libp2p:: SwarmBuilder :: with_existing_identity ( keypair)
6065 . with_tokio ( )
6166 . with_tcp (
@@ -64,15 +69,15 @@ async fn main() -> eyre::Result<()> {
6469 libp2p:: yamux:: Config :: default,
6570 ) ?
6671 . with_quic ( )
72+ . with_bandwidth_metrics ( & mut metric_registry)
6773 . with_behaviour ( |keypair| Behaviour {
6874 autonat : autonat:: Behaviour :: new ( peer_id. clone ( ) , Default :: default ( ) ) ,
6975 identify : identify:: Behaviour :: new ( identify:: Config :: new (
7076 PROTOCOL_VERSION . to_owned ( ) ,
7177 keypair. public ( ) ,
7278 ) ) ,
7379 kad : {
74- let mut kademlia_config = kad:: Config :: default ( ) ;
75- kademlia_config. set_protocol_names ( vec ! [ CALIMERO_KAD_PROTO_NAME ] ) ;
80+ let mut kademlia_config = kad:: Config :: new ( CALIMERO_KAD_PROTO_NAME ) ;
7681 // Instantly remove records and provider records.
7782 // TODO: figure out what to do with these values, ref: https://github.com/libp2p/rust-libp2p/blob/1aa016e1c7e3976748a726eab37af44d1c5b7a6e/misc/server/src/behaviour.rs#L38
7883 kademlia_config. set_record_ttl ( Some ( std:: time:: Duration :: from_secs ( 0 ) ) ) ;
@@ -116,16 +121,28 @@ async fn main() -> eyre::Result<()> {
116121 . with ( multiaddr:: Protocol :: QuicV1 ) ;
117122 swarm. listen_on ( listen_addr_quic) ?;
118123
124+ let metrics = Metrics :: new ( & mut metric_registry) ;
125+ tokio:: spawn ( http_service:: metrics_server ( metric_registry) ) ;
126+
119127 loop {
120128 let event = swarm. next ( ) . await ;
121- handle_swarm_event ( & mut swarm, event. expect ( "Swarm stream to be infinite." ) ) . await ;
129+ handle_swarm_event (
130+ & mut swarm,
131+ event. expect ( "Swarm stream to be infinite." ) ,
132+ & metrics,
133+ )
134+ . await ;
122135 }
123136}
124137
125- async fn handle_swarm_event ( swarm : & mut Swarm < Behaviour > , event : SwarmEvent < BehaviourEvent > ) {
138+ async fn handle_swarm_event (
139+ swarm : & mut Swarm < Behaviour > ,
140+ event : SwarmEvent < BehaviourEvent > ,
141+ metrics : & Metrics ,
142+ ) {
126143 match event {
127144 SwarmEvent :: Behaviour ( event) => {
128- handle_swarm_behaviour_event ( swarm, event) . await ;
145+ handle_swarm_behaviour_event ( swarm, event, metrics ) . await ;
129146 }
130147 SwarmEvent :: NewListenAddr { address, .. } => {
131148 info ! ( "Listening on {address:?}" ) ;
@@ -134,12 +151,17 @@ async fn handle_swarm_event(swarm: &mut Swarm<Behaviour>, event: SwarmEvent<Beha
134151 }
135152}
136153
137- async fn handle_swarm_behaviour_event ( swarm : & mut Swarm < Behaviour > , event : BehaviourEvent ) {
154+ async fn handle_swarm_behaviour_event (
155+ swarm : & mut Swarm < Behaviour > ,
156+ event : BehaviourEvent ,
157+ metrics : & Metrics ,
158+ ) {
138159 match event {
139160 BehaviourEvent :: Autonat ( event) => {
140161 info ! ( "AutoNat event: {event:?}" ) ;
141162 }
142163 BehaviourEvent :: Identify ( event) => {
164+ metrics. record ( & event) ;
143165 info ! ( "Identify event: {event:?}" ) ;
144166 match event {
145167 identify:: Event :: Received {
@@ -153,9 +175,11 @@ async fn handle_swarm_behaviour_event(swarm: &mut Swarm<Behaviour>, event: Behav
153175 }
154176 }
155177 BehaviourEvent :: Kad ( event) => {
178+ metrics. record ( & event) ;
156179 info ! ( "Kad event: {event:?}" ) ;
157180 }
158181 BehaviourEvent :: Relay ( event) => {
182+ metrics. record ( & event) ;
159183 info ! ( "Relay event: {event:?}" ) ;
160184 }
161185 BehaviourEvent :: Rendezvous ( event) => {
0 commit comments