Skip to content

Commit b7e5bb0

Browse files
authored
feat(metrics): add connection latency tracking (#3606)
## Description <!-- A summary of what this pull request achieves and a rough list of changes. --> ## Breaking Changes <!-- Optional, if there are any breaking changes document them, including how to migrate older code. --> ## Notes & open questions <!-- Any notes, remarks or open questions you have to make about the PR. --> ## Change checklist <!-- Remove any that are not relevant. --> - [ ] Self-review. - [ ] 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. - [ ] Tests if relevant. - [ ] All breaking changes documented. - [ ] List all breaking changes in the above "Breaking Changes" section. - [ ] Open an issue or PR on any number0 repos that are affected by this breaking change. Give guidance on how the updates should be handled or do the actual updates themselves. The major ones are: - [ ] [`quic-rpc`](https://github.com/n0-computer/quic-rpc) - [ ] [`iroh-gossip`](https://github.com/n0-computer/iroh-gossip) - [ ] [`iroh-blobs`](https://github.com/n0-computer/iroh-blobs) - [ ] [`dumbpipe`](https://github.com/n0-computer/dumbpipe) - [ ] [`sendme`](https://github.com/n0-computer/sendme)
1 parent 1cc5897 commit b7e5bb0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

iroh/src/magicsock/endpoint_map/path_validity.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ impl PathValidity {
285285

286286
let quality = state.congestion_metrics.quality_score();
287287
metrics.path_quality_score.observe(quality);
288+
289+
let latency_ms = state.latency.as_secs_f64() * 1000.0;
290+
metrics.connection_latency_ms.observe(latency_ms);
288291
}
289292
}
290293

@@ -395,4 +398,36 @@ mod tests {
395398
let quality = validity.quality_score();
396399
assert!(quality < 0.9); // Should be penalized
397400
}
401+
402+
#[tokio::test]
403+
async fn test_connection_latency_histogram() {
404+
use crate::magicsock::Metrics as MagicsockMetrics;
405+
406+
let metrics = MagicsockMetrics::default();
407+
let mut validity = PathValidity::new(Instant::now(), Duration::from_millis(10));
408+
409+
validity.record_metrics(&metrics);
410+
assert_eq!(metrics.connection_latency_ms.count(), 1);
411+
412+
validity.update_pong(Instant::now(), Duration::from_millis(25));
413+
validity.record_metrics(&metrics);
414+
assert_eq!(metrics.connection_latency_ms.count(), 2);
415+
416+
validity.update_pong(Instant::now(), Duration::from_millis(50));
417+
validity.record_metrics(&metrics);
418+
assert_eq!(metrics.connection_latency_ms.count(), 3);
419+
420+
validity.update_pong(Instant::now(), Duration::from_millis(100));
421+
validity.record_metrics(&metrics);
422+
assert_eq!(metrics.connection_latency_ms.count(), 4);
423+
424+
let buckets = metrics.connection_latency_ms.buckets();
425+
assert!(!buckets.is_empty());
426+
427+
let p50 = metrics.connection_latency_ms.percentile(0.5);
428+
assert!(p50 > 10.0 && p50 < 100.0);
429+
430+
let p95 = metrics.connection_latency_ms.percentile(0.95);
431+
assert!(p95 >= p50);
432+
}
398433
}

iroh/src/magicsock/metrics.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ pub struct Metrics {
7777
pub connection_handshake_success: Counter,
7878
/// Number of connections with a successful handshake that became direct.
7979
pub connection_became_direct: Counter,
80+
/// Histogram of connection latency in milliseconds across all endpoint connections.
81+
#[default(Histogram::new(vec![1.0, 5.0, 10.0, 25.0, 50.0, 100.0, 250.0, 500.0, 1000.0, f64::INFINITY]))]
82+
pub connection_latency_ms: Histogram,
8083

8184
/*
8285
* Path Congestion Metrics

0 commit comments

Comments
 (0)