Skip to content

Commit fc848fc

Browse files
committed
feat: metrics refactor
1 parent 8a24a95 commit fc848fc

File tree

19 files changed

+261
-185
lines changed

19 files changed

+261
-185
lines changed

Cargo.lock

Lines changed: 4 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ["cfg(iroh_docsrs)", "cfg(iroh_l
4040

4141
[workspace.lints.clippy]
4242
unused-async = "warn"
43+
44+
[patch.crates-io]
45+
iroh-metrics = { path = "../iroh-metrics" }
46+
portmapper = { path = "../net-tools/portmapper" }

iroh-dns-server/src/metrics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Metrics support for the server
22
3-
use iroh_metrics::core::{Core, Counter, Metric};
3+
use iroh_metrics::core::{Core, Counter, Metric, MetricExt};
44
use struct_iterable::Iterable;
55

66
/// Metrics for iroh-dns-server
@@ -51,7 +51,7 @@ impl Default for Metrics {
5151
}
5252

5353
impl Metric for Metrics {
54-
fn name() -> &'static str {
54+
fn name(&self) -> &'static str {
5555
"dns_server"
5656
}
5757
}

iroh-relay/src/main.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -724,54 +724,6 @@ async fn build_relay_config(cfg: Config) -> Result<relay::ServerConfig<std::io::
724724
})
725725
}
726726

727-
mod metrics {
728-
use iroh_metrics::{
729-
core::{Counter, Metric},
730-
struct_iterable::Iterable,
731-
};
732-
733-
/// StunMetrics tracked for the relay server
734-
#[allow(missing_docs)]
735-
#[derive(Debug, Clone, Iterable)]
736-
pub struct StunMetrics {
737-
/*
738-
* Metrics about STUN requests over ipv6
739-
*/
740-
/// Number of stun requests made
741-
pub requests: Counter,
742-
/// Number of successful requests over ipv4
743-
pub ipv4_success: Counter,
744-
/// Number of successful requests over ipv6
745-
pub ipv6_success: Counter,
746-
747-
/// Number of bad requests, either non-stun packets or incorrect binding request
748-
pub bad_requests: Counter,
749-
/// Number of failures
750-
pub failures: Counter,
751-
}
752-
753-
impl Default for StunMetrics {
754-
fn default() -> Self {
755-
Self {
756-
/*
757-
* Metrics about STUN requests
758-
*/
759-
requests: Counter::new("Number of STUN requests made to the server."),
760-
ipv4_success: Counter::new("Number of successful ipv4 STUN requests served."),
761-
ipv6_success: Counter::new("Number of successful ipv6 STUN requests served."),
762-
bad_requests: Counter::new("Number of bad requests made to the STUN endpoint."),
763-
failures: Counter::new("Number of STUN requests that end in failure."),
764-
}
765-
}
766-
}
767-
768-
impl Metric for StunMetrics {
769-
fn name() -> &'static str {
770-
"stun"
771-
}
772-
}
773-
}
774-
775727
#[cfg(test)]
776728
mod tests {
777729
use std::num::NonZeroU32;

iroh-relay/src/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ impl Server {
283283
#[cfg(feature = "metrics")]
284284
if let Some(addr) = config.metrics_addr {
285285
debug!("Starting metrics server");
286-
use iroh_metrics::core::Metric;
286+
use iroh_metrics::core::MetricExt;
287287

288288
iroh_metrics::core::Core::init(|reg, metrics| {
289289
metrics.insert(metrics::Metrics::new(reg));

iroh-relay/src/server/metrics.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl Default for Metrics {
123123
}
124124

125125
impl Metric for Metrics {
126-
fn name() -> &'static str {
126+
fn name(&self) -> &'static str {
127127
"relayserver"
128128
}
129129
}
@@ -163,7 +163,7 @@ impl Default for StunMetrics {
163163
}
164164

165165
impl Metric for StunMetrics {
166-
fn name() -> &'static str {
166+
fn name(&self) -> &'static str {
167167
"stun"
168168
}
169169
}

iroh/bench/src/bin/bulk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use clap::Parser;
55
#[cfg(not(any(target_os = "freebsd", target_os = "openbsd", target_os = "netbsd")))]
66
use iroh_bench::quinn;
77
use iroh_bench::{configure_tracing_subscriber, iroh, rt, s2n, Commands, Opt};
8+
use iroh_metrics::core::MetricExt;
89

910
fn main() {
1011
let cmd = Commands::parse();
@@ -34,7 +35,6 @@ pub fn run_iroh(opt: Opt) -> Result<()> {
3435
if opt.metrics {
3536
// enable recording metrics
3637
iroh_metrics::core::Core::try_init(|reg, metrics| {
37-
use iroh_metrics::core::Metric;
3838
metrics.insert(::iroh::metrics::MagicsockMetrics::new(reg));
3939
metrics.insert(::iroh::metrics::NetReportMetrics::new(reg));
4040
metrics.insert(::iroh::metrics::PortmapMetrics::new(reg));

iroh/src/endpoint.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ use crate::{
4141
DiscoveryTask, Lagged, UserData,
4242
},
4343
magicsock::{self, Handle, NodeIdMappedAddr},
44+
metrics::EndpointMetrics,
4445
tls,
4546
watchable::Watcher,
4647
RelayProtocol,
@@ -192,6 +193,8 @@ impl Builder {
192193
};
193194
let server_config = static_config.create_server_config(self.alpn_protocols)?;
194195

196+
let metrics = EndpointMetrics::default();
197+
195198
let msock_opts = magicsock::Options {
196199
addr_v4: self.addr_v4,
197200
addr_v6: self.addr_v6,
@@ -209,6 +212,7 @@ impl Builder {
209212
insecure_skip_relay_cert_verify: self.insecure_skip_relay_cert_verify,
210213
#[cfg(any(test, feature = "test-utils"))]
211214
path_selection: self.path_selection,
215+
metrics,
212216
};
213217
Endpoint::bind(static_config, msock_opts).await
214218
}
@@ -620,7 +624,7 @@ impl Endpoint {
620624

621625
let ep = Self {
622626
msock: msock.clone(),
623-
rtt_actor: Arc::new(rtt_actor::RttHandle::new()),
627+
rtt_actor: Arc::new(rtt_actor::RttHandle::new(msock.metrics.magicsock.clone())),
624628
static_config: Arc::new(static_config),
625629
session_store: Arc::new(rustls::client::ClientSessionMemoryCache::new(
626630
MAX_TLS_TICKETS,
@@ -1103,6 +1107,12 @@ impl Endpoint {
11031107
self.msock.discovery()
11041108
}
11051109

1110+
/// Returns metrics collected for this endpoint.
1111+
#[cfg(feature = "metrics")]
1112+
pub fn metrics(&self) -> &EndpointMetrics {
1113+
&self.msock.metrics
1114+
}
1115+
11061116
// # Methods for less common state updates.
11071117

11081118
/// Notifies the system of potential network changes.

iroh/src/endpoint/rtt_actor.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use std::{pin::Pin, task::Poll};
44

55
use iroh_base::NodeId;
6-
use iroh_metrics::inc;
76
use n0_future::{
87
task::{self, AbortOnDropHandle},
98
MergeUnbounded, Stream, StreamExt,
@@ -21,9 +20,10 @@ pub(super) struct RttHandle {
2120
}
2221

2322
impl RttHandle {
24-
pub(super) fn new() -> Self {
23+
pub(super) fn new(metrics: MagicsockMetrics) -> Self {
2524
let mut actor = RttActor {
2625
connection_events: Default::default(),
26+
metrics,
2727
};
2828
let (msg_tx, msg_rx) = mpsc::channel(16);
2929
let handle = task::spawn(
@@ -62,6 +62,7 @@ struct RttActor {
6262
/// Stream of connection type changes.
6363
#[debug("MergeUnbounded<WatcherStream<ConnectionType>>")]
6464
connection_events: MergeUnbounded<MappedStream>,
65+
metrics: MagicsockMetrics,
6566
}
6667

6768
#[derive(Debug)]
@@ -75,8 +76,12 @@ struct MappedStream {
7576
was_direct_before: bool,
7677
}
7778

79+
struct ConnectionEvent {
80+
became_direct: bool,
81+
}
82+
7883
impl Stream for MappedStream {
79-
type Item = ConnectionType;
84+
type Item = ConnectionEvent;
8085

8186
/// Performs the congestion controller reset for a magic socket path change.
8287
///
@@ -90,6 +95,7 @@ impl Stream for MappedStream {
9095
) -> Poll<Option<Self::Item>> {
9196
match Pin::new(&mut self.stream).poll_next(cx) {
9297
Poll::Ready(Some(new_conn_type)) => {
98+
let mut became_direct = false;
9399
if self.connection.network_path_changed() {
94100
debug!(
95101
node_id = %self.node_id.fmt_short(),
@@ -99,10 +105,10 @@ impl Stream for MappedStream {
99105
if !self.was_direct_before && matches!(new_conn_type, ConnectionType::Direct(_))
100106
{
101107
self.was_direct_before = true;
102-
inc!(MagicsockMetrics, connection_became_direct);
108+
became_direct = true
103109
}
104-
}
105-
Poll::Ready(Some(new_conn_type))
110+
};
111+
Poll::Ready(Some(ConnectionEvent { became_direct }))
106112
}
107113
Poll::Ready(None) => Poll::Ready(None),
108114
Poll::Pending => Poll::Pending,
@@ -124,7 +130,11 @@ impl RttActor {
124130
None => break,
125131
}
126132
}
127-
_item = self.connection_events.next(), if !self.connection_events.is_empty() => {}
133+
event = self.connection_events.next(), if !self.connection_events.is_empty() => {
134+
if event.map(|e| e.became_direct).unwrap_or(false) {
135+
self.metrics.connection_became_direct.inc();
136+
}
137+
}
128138
}
129139
}
130140
debug!("rtt-actor finished");
@@ -156,6 +166,6 @@ impl RttActor {
156166
node_id,
157167
was_direct_before: false,
158168
});
159-
inc!(MagicsockMetrics, connection_handshake_success);
169+
self.metrics.connection_handshake_success.inc();
160170
}
161171
}

0 commit comments

Comments
 (0)