|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#![doc = include_str!("../README.md")] |
|
|
|
|
|
use futures::StreamExt; |
|
|
use libp2p::core::Multiaddr; |
|
|
use libp2p::metrics::{Metrics, Recorder}; |
|
|
use libp2p::swarm::{NetworkBehaviour, SwarmEvent}; |
|
|
use libp2p::{identify, identity, noise, ping, tcp, yamux}; |
|
|
use opentelemetry::KeyValue; |
|
|
use prometheus_client::registry::Registry; |
|
|
use std::error::Error; |
|
|
use std::time::Duration; |
|
|
use tracing_subscriber::layer::SubscriberExt; |
|
|
use tracing_subscriber::util::SubscriberInitExt; |
|
|
use tracing_subscriber::{EnvFilter, Layer}; |
|
|
|
|
|
mod http_service; |
|
|
|
|
|
#[tokio::main] |
|
|
async fn main() -> Result<(), Box<dyn Error>> { |
|
|
setup_tracing()?; |
|
|
|
|
|
let mut metric_registry = Registry::default(); |
|
|
|
|
|
let mut swarm = libp2p::SwarmBuilder::with_new_identity() |
|
|
.with_tokio() |
|
|
.with_tcp( |
|
|
tcp::Config::default(), |
|
|
noise::Config::new, |
|
|
yamux::Config::default, |
|
|
)? |
|
|
.with_bandwidth_metrics(&mut metric_registry) |
|
|
.with_behaviour(|key| Behaviour::new(key.public()))? |
|
|
.with_swarm_config(|cfg| cfg.with_idle_connection_timeout(Duration::from_secs(u64::MAX))) |
|
|
.build(); |
|
|
|
|
|
swarm.listen_on("/ip4/0.0.0.0/tcp/0".parse()?)?; |
|
|
|
|
|
if let Some(addr) = std::env::args().nth(1) { |
|
|
let remote: Multiaddr = addr.parse()?; |
|
|
swarm.dial(remote)?; |
|
|
tracing::info!(address=%addr, "Dialed address") |
|
|
} |
|
|
|
|
|
let metrics = Metrics::new(&mut metric_registry); |
|
|
tokio::spawn(http_service::metrics_server(metric_registry)); |
|
|
|
|
|
loop { |
|
|
match swarm.select_next_some().await { |
|
|
SwarmEvent::NewListenAddr { address, .. } => { |
|
|
tracing::info!( |
|
|
"Local node is listening on\n {}/p2p/{}", |
|
|
address, |
|
|
swarm.local_peer_id() |
|
|
); |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Ping(ping_event)) => { |
|
|
tracing::info!(?ping_event); |
|
|
metrics.record(&ping_event); |
|
|
} |
|
|
SwarmEvent::Behaviour(BehaviourEvent::Identify(identify_event)) => { |
|
|
tracing::info!(?identify_event); |
|
|
metrics.record(&identify_event); |
|
|
} |
|
|
swarm_event => { |
|
|
tracing::info!(?swarm_event); |
|
|
metrics.record(&swarm_event); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
fn setup_tracing() -> Result<(), Box<dyn Error>> { |
|
|
let tracer = opentelemetry_otlp::new_pipeline() |
|
|
.tracing() |
|
|
.with_exporter(opentelemetry_otlp::new_exporter().tonic()) |
|
|
.with_trace_config(opentelemetry_sdk::trace::Config::default().with_resource( |
|
|
opentelemetry_sdk::Resource::new(vec![KeyValue::new("service.name", "libp2p")]), |
|
|
)) |
|
|
.install_batch(opentelemetry_sdk::runtime::Tokio)?; |
|
|
|
|
|
tracing_subscriber::registry() |
|
|
.with(tracing_subscriber::fmt::layer().with_filter(EnvFilter::from_default_env())) |
|
|
.with( |
|
|
tracing_opentelemetry::layer() |
|
|
.with_tracer(tracer) |
|
|
.with_filter(EnvFilter::from_default_env()), |
|
|
) |
|
|
.try_init()?; |
|
|
|
|
|
Ok(()) |
|
|
} |
|
|
|
|
|
|
|
|
#[derive(NetworkBehaviour)] |
|
|
struct Behaviour { |
|
|
identify: identify::Behaviour, |
|
|
ping: ping::Behaviour, |
|
|
} |
|
|
|
|
|
impl Behaviour { |
|
|
fn new(local_pub_key: identity::PublicKey) -> Self { |
|
|
Self { |
|
|
ping: ping::Behaviour::default(), |
|
|
identify: identify::Behaviour::new(identify::Config::new( |
|
|
"/ipfs/0.1.0".into(), |
|
|
local_pub_key, |
|
|
)), |
|
|
} |
|
|
} |
|
|
} |
|
|
|