Skip to content

Commit 3df0adf

Browse files
authored
refactor(portmapper)!: non-global metrics collection
## Description Needed for n0-computer/iroh#3262 Updates `iroh-metrics` to 0.34, and makes the metrics collection non-global. Metrics are now collected per portmapper service. ## Breaking Changes * `portmapper::metrics::Metrics` now implements `MetricsGroup` from `iroh-metrics@0.34`, and no longer implements `Metric` from `iroh-metrics@0.32`. ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented.
1 parent 9da8a0b commit 3df0adf

File tree

6 files changed

+91
-204
lines changed

6 files changed

+91
-204
lines changed

Cargo.lock

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

portmapper/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ derive_more = { version = "1.0.0", features = ["debug", "display", "from", "try_
2222
futures-lite = "2.5"
2323
futures-util = "0.3.25"
2424
igd-next = { version = "0.15.1", features = ["aio_tokio"] }
25-
iroh-metrics = { version = "0.32", default-features = false }
25+
iroh-metrics = { version = "0.34", default-features = false }
2626
libc = "0.2.139"
2727
nested_enum_utils = "0.2.0"
2828
netwatch = { version = "0.4.0", path = "../netwatch" }

portmapper/src/current_mapping.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@ use std::{
55
net::{Ipv4Addr, SocketAddrV4},
66
num::NonZeroU16,
77
pin::Pin,
8+
sync::Arc,
89
task::Poll,
910
time::Duration,
1011
};
1112

12-
use iroh_metrics::inc;
1313
use tokio::{sync::watch, time};
1414
use tracing::{debug, trace};
1515

16+
use crate::Metrics;
17+
1618
/// This is an implementation detail to facilitate testing.
1719
pub(super) trait Mapping: std::fmt::Debug + Unpin {
1820
fn external(&self) -> (Ipv4Addr, NonZeroU16);
@@ -73,16 +75,18 @@ pub(super) struct CurrentMapping<M = super::mapping::Mapping> {
7375
/// Waker to ensure this is polled when needed.
7476
#[debug(skip)]
7577
waker: Option<std::task::Waker>,
78+
metrics: Arc<Metrics>,
7679
}
7780

7881
impl<M: Mapping> CurrentMapping<M> {
7982
/// Creates a new [`CurrentMapping`] and returns the watcher over its external address.
80-
pub(super) fn new() -> (Self, watch::Receiver<Option<SocketAddrV4>>) {
83+
pub(super) fn new(metrics: Arc<Metrics>) -> (Self, watch::Receiver<Option<SocketAddrV4>>) {
8184
let (address_tx, address_rx) = watch::channel(None);
8285
let wrapper = CurrentMapping {
8386
mapping: None,
8487
address_tx,
8588
waker: None,
89+
metrics,
8690
};
8791
(wrapper, address_rx)
8892
}
@@ -108,7 +112,7 @@ impl<M: Mapping> CurrentMapping<M> {
108112
// inform only if this produces a different external address
109113
let update = old_addr != maybe_external_addr;
110114
if update {
111-
inc!(super::Metrics, external_address_updated);
115+
self.metrics.external_address_updated.inc();
112116
};
113117
update
114118
});
@@ -201,7 +205,7 @@ mod tests {
201205
const TEST_PORT: NonZeroU16 = // SAFETY: it's clearly non zero
202206
unsafe { NonZeroU16::new_unchecked(9586) };
203207
const TEST_IP: std::net::Ipv4Addr = std::net::Ipv4Addr::LOCALHOST;
204-
let (mut c, mut watcher) = CurrentMapping::<M>::new();
208+
let (mut c, mut watcher) = CurrentMapping::<M>::new(Default::default());
205209
let now = std::time::Instant::now();
206210
c.update(Some((TEST_IP, TEST_PORT)));
207211

0 commit comments

Comments
 (0)