mirror of
https://relay.ngit.dev/npub15qydau2hjma6ngxkl2cyar74wzyjshvl65za5k5rl69264ar2exs5cyejr/ngit-grasp.git
synced 2026-10-05 15:08:24 +00:00
Production exposes ngit_websocket_connection_duration_seconds, but after thousands of observed connection closures every bucket, the sum, and the count remained zero. The timer implementation existed but no request path ever constructed it. Create the timer only after a WebSocket upgrade succeeds and retain it for the full relay connection future. Its drop therefore observes every normally returned or errored established session, while failed HTTP upgrades remain excluded. This changes metrics wiring only; connection admission, protocol handling, and timeout behavior are deliberately unchanged. A real-socket integration scenario closes one established WebSocket and polls the metrics endpoint until exactly one duration observation appears. Validated with cargo test --test websocket_metrics (45 passed), cargo test --lib (650 passed), git diff --check, and nix build .#ngit-grasp.
41 lines
1.1 KiB
Rust
41 lines
1.1 KiB
Rust
//! WebSocket lifecycle metrics scenarios.
|
|
|
|
mod common;
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
use common::{sync_helpers::fetch_metrics, TestRelay};
|
|
|
|
fn connection_duration_count(metrics: &str) -> Option<u64> {
|
|
metrics.lines().find_map(|line| {
|
|
line.strip_prefix("ngit_websocket_connection_duration_seconds_count ")?
|
|
.parse()
|
|
.ok()
|
|
})
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn closed_websocket_records_its_connection_duration() {
|
|
let relay = TestRelay::start().await;
|
|
let (mut stream, _) = tokio_tungstenite::connect_async(relay.url())
|
|
.await
|
|
.expect("connect to relay");
|
|
|
|
stream.close(None).await.expect("close websocket");
|
|
|
|
let deadline = Instant::now() + Duration::from_secs(5);
|
|
loop {
|
|
let metrics = fetch_metrics(relay.url()).await.expect("fetch metrics");
|
|
if connection_duration_count(&metrics) == Some(1) {
|
|
break;
|
|
}
|
|
assert!(
|
|
Instant::now() < deadline,
|
|
"closed connection was not observed in duration histogram: {metrics}"
|
|
);
|
|
tokio::time::sleep(Duration::from_millis(25)).await;
|
|
}
|
|
|
|
relay.stop().await;
|
|
}
|