Skip to content

Commit f8831d7

Browse files
Preslav LeConvex, Inc.
authored andcommitted
Add debug gauge to web sockets (#28685)
This will help us triage which worker is getting stuck if it ever does. GitOrigin-RevId: 4693b9c08ccf49dd591d807e2913042e22150645
1 parent 7d9c9f6 commit f8831d7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

crates/local_backend/src/subs/metrics.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,14 @@ register_convex_counter!(pub WEBSOCKET_CONNECTION_RESET_TOTAL, "Number of websoc
119119
pub fn log_websocket_connection_reset() {
120120
log_counter(&WEBSOCKET_CONNECTION_RESET_TOTAL, 1)
121121
}
122+
123+
register_convex_gauge!(
124+
DEBUG_SYNC_PROTOCOL_WEBSOCKETS_TOTAL,
125+
"Number of WebSocket connected to a backend at a given state",
126+
&["tag"]
127+
);
128+
pub fn log_debug_sync_protocol_websockets_total(tag: &'static str, delta: i8) {
129+
DEBUG_SYNC_PROTOCOL_WEBSOCKETS_TOTAL
130+
.with_label_values(&[tag])
131+
.add(delta as f64)
132+
}

crates/local_backend/src/subs/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ use sync_types::IdentityVersion;
6060
mod metrics;
6161

6262
use metrics::{
63+
log_debug_sync_protocol_websockets_total,
6364
log_sync_protocol_websockets_total,
6465
log_websocket_client_timeout,
6566
log_websocket_closed,
@@ -96,6 +97,25 @@ impl Drop for SyncSocketDropToken {
9697
}
9798
}
9899

100+
// TODO(presley): Remove. Used for debugging.
101+
struct DebugSyncSocketDropToken {
102+
tag: &'static str,
103+
}
104+
105+
/// Tracker that exists for the lifetime of a run_sync_socket.
106+
impl DebugSyncSocketDropToken {
107+
fn new(tag: &'static str) -> Self {
108+
log_debug_sync_protocol_websockets_total(tag, 1);
109+
DebugSyncSocketDropToken { tag }
110+
}
111+
}
112+
113+
impl Drop for DebugSyncSocketDropToken {
114+
fn drop(&mut self) {
115+
log_debug_sync_protocol_websockets_total(self.tag, -1);
116+
}
117+
}
118+
99119
// The WebSocket layer for the sync protocol has three asynchronous processes:
100120
//
101121
// 1) A `receive_messages` loop that consumes messages from the WebSocket,
@@ -124,6 +144,7 @@ async fn run_sync_socket(
124144

125145
let (client_tx, client_rx) = mpsc::unbounded();
126146
let receive_messages = async {
147+
let _receive_message_drop_token = DebugSyncSocketDropToken::new("receive_message");
127148
while let Some(message_r) = rx.next().await {
128149
let message = match message_r {
129150
Ok(message) => message,
@@ -175,6 +196,7 @@ async fn run_sync_socket(
175196

176197
let (server_tx, mut server_rx) = measurable_unbounded_channel();
177198
let send_messages = async {
199+
let _send_message_drop_token = DebugSyncSocketDropToken::new("send_message");
178200
let mut ping_ticker = tokio::time::interval(HEARTBEAT_INTERVAL);
179201
'top: loop {
180202
select_biased! {
@@ -209,6 +231,7 @@ async fn run_sync_socket(
209231
};
210232
let mut identity_version: Option<IdentityVersion> = None;
211233
let sync_worker_go = async {
234+
let _sync_worker_drop_token = DebugSyncSocketDropToken::new("sync_worker");
212235
let mut sync_worker = SyncWorker::new(
213236
st.api.clone(),
214237
st.runtime.clone(),

0 commit comments

Comments
 (0)