2021-08-13 22:51:54 +02:00
|
|
|
// Copyright 2021 Protocol Labs.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
//! Example demonstrating `libp2p-metrics`.
|
|
|
|
//!
|
|
|
|
//! In one terminal run:
|
|
|
|
//!
|
|
|
|
//! ```
|
2023-04-11 22:47:43 +02:00
|
|
|
//! cargo run
|
2021-08-13 22:51:54 +02:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! In a second terminal run:
|
|
|
|
//!
|
|
|
|
//! ```
|
2023-04-11 22:47:43 +02:00
|
|
|
//! cargo run -- <listen-addr-of-first-node>
|
2021-08-13 22:51:54 +02:00
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Where `<listen-addr-of-first-node>` is replaced by the listen address of the
|
|
|
|
//! first node reported in the first terminal. Look for `NewListenAddr`.
|
|
|
|
//!
|
|
|
|
//! In a third terminal run:
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! curl localhost:<metrics-port-of-first-or-second-node>/metrics
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! Where `<metrics-port-of-first-or-second-node>` is replaced by the listen
|
|
|
|
//! port of the metrics server of the first or the second node. Look for
|
|
|
|
//! `tide::server Server listening on`.
|
|
|
|
//!
|
|
|
|
//! You should see a long list of metrics printed to the terminal. Check the
|
|
|
|
//! `libp2p_ping` metrics, they should be `>0`.
|
|
|
|
|
2022-10-06 03:50:11 +11:00
|
|
|
use env_logger::Env;
|
2021-08-13 22:51:54 +02:00
|
|
|
use futures::executor::block_on;
|
|
|
|
use futures::stream::StreamExt;
|
2023-04-11 22:47:43 +02:00
|
|
|
use libp2p::core::{upgrade::Version, Multiaddr, Transport};
|
|
|
|
use libp2p::identity::PeerId;
|
|
|
|
use libp2p::metrics::{Metrics, Recorder};
|
|
|
|
use libp2p::swarm::{keep_alive, NetworkBehaviour, SwarmBuilder, SwarmEvent};
|
|
|
|
use libp2p::{identify, identity, noise, ping, tcp, yamux};
|
2022-10-06 03:50:11 +11:00
|
|
|
use log::info;
|
2022-02-03 11:31:41 +01:00
|
|
|
use prometheus_client::registry::Registry;
|
2021-08-13 22:51:54 +02:00
|
|
|
use std::error::Error;
|
|
|
|
use std::thread;
|
|
|
|
|
2022-03-05 09:15:10 -05:00
|
|
|
mod http_service;
|
|
|
|
|
2021-08-13 22:51:54 +02:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2022-03-05 09:15:10 -05:00
|
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("info")).init();
|
2021-08-13 22:51:54 +02:00
|
|
|
|
|
|
|
let local_key = identity::Keypair::generate_ed25519();
|
|
|
|
let local_peer_id = PeerId::from(local_key.public());
|
2022-11-15 15:45:14 -05:00
|
|
|
let local_pub_key = local_key.public();
|
2022-12-13 07:58:01 +11:00
|
|
|
info!("Local peer id: {local_peer_id:?}");
|
2021-08-13 22:51:54 +02:00
|
|
|
|
2023-03-13 20:53:14 +01:00
|
|
|
let mut swarm = SwarmBuilder::without_executor(
|
2022-12-13 07:58:01 +11:00
|
|
|
tcp::async_io::Transport::default()
|
2023-04-21 11:58:08 +02:00
|
|
|
.upgrade(Version::V1Lazy)
|
2022-12-13 07:58:01 +11:00
|
|
|
.authenticate(noise::NoiseAuthenticated::xx(&local_key)?)
|
|
|
|
.multiplex(yamux::YamuxConfig::default())
|
|
|
|
.boxed(),
|
2022-11-15 15:45:14 -05:00
|
|
|
Behaviour::new(local_pub_key),
|
2021-08-13 22:51:54 +02:00
|
|
|
local_peer_id,
|
2023-03-13 20:53:14 +01:00
|
|
|
)
|
|
|
|
.build();
|
2022-03-05 09:15:10 -05:00
|
|
|
|
2021-08-13 22:51:54 +02:00
|
|
|
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?;
|
2022-03-05 09:15:10 -05:00
|
|
|
|
2021-08-13 22:51:54 +02:00
|
|
|
if let Some(addr) = std::env::args().nth(1) {
|
2021-11-15 14:17:23 +01:00
|
|
|
let remote: Multiaddr = addr.parse()?;
|
|
|
|
swarm.dial(remote)?;
|
2022-03-05 09:15:10 -05:00
|
|
|
info!("Dialed {}", addr)
|
2021-08-13 22:51:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut metric_registry = Registry::default();
|
|
|
|
let metrics = Metrics::new(&mut metric_registry);
|
2022-03-05 09:15:10 -05:00
|
|
|
thread::spawn(move || block_on(http_service::metrics_server(metric_registry)));
|
2021-08-13 22:51:54 +02:00
|
|
|
|
|
|
|
block_on(async {
|
|
|
|
loop {
|
|
|
|
match swarm.select_next_some().await {
|
2022-10-06 03:50:11 +11:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping_event)) => {
|
2022-03-05 09:15:10 -05:00
|
|
|
info!("{:?}", ping_event);
|
2021-08-13 22:51:54 +02:00
|
|
|
metrics.record(&ping_event);
|
|
|
|
}
|
2022-11-15 15:45:14 -05:00
|
|
|
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify_event)) => {
|
|
|
|
info!("{:?}", identify_event);
|
|
|
|
metrics.record(&identify_event);
|
|
|
|
}
|
2021-08-13 22:51:54 +02:00
|
|
|
swarm_event => {
|
2022-03-05 09:15:10 -05:00
|
|
|
info!("{:?}", swarm_event);
|
2021-08-13 22:51:54 +02:00
|
|
|
metrics.record(&swarm_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-05 09:15:10 -05:00
|
|
|
});
|
2021-08-13 22:51:54 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-06 03:50:11 +11:00
|
|
|
|
|
|
|
/// Our network behaviour.
|
|
|
|
///
|
|
|
|
/// For illustrative purposes, this includes the [`keep_alive::Behaviour`]) behaviour so the ping actually happen
|
|
|
|
/// and can be observed via the metrics.
|
2022-11-15 15:45:14 -05:00
|
|
|
#[derive(NetworkBehaviour)]
|
2022-10-06 03:50:11 +11:00
|
|
|
struct Behaviour {
|
2022-11-15 15:45:14 -05:00
|
|
|
identify: identify::Behaviour,
|
2022-10-06 03:50:11 +11:00
|
|
|
keep_alive: keep_alive::Behaviour,
|
|
|
|
ping: ping::Behaviour,
|
|
|
|
}
|
2022-11-15 15:45:14 -05:00
|
|
|
|
|
|
|
impl Behaviour {
|
2022-12-13 07:58:01 +11:00
|
|
|
fn new(local_pub_key: identity::PublicKey) -> Self {
|
2022-11-15 15:45:14 -05:00
|
|
|
Self {
|
|
|
|
ping: ping::Behaviour::default(),
|
|
|
|
identify: identify::Behaviour::new(identify::Config::new(
|
|
|
|
"/ipfs/0.1.0".into(),
|
|
|
|
local_pub_key,
|
|
|
|
)),
|
|
|
|
keep_alive: keep_alive::Behaviour::default(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|