Skip to content

Commit e3ac9f4

Browse files
emmaling27Convex, Inc.
authored andcommitted
Add metric for size of ClientMessages received by sync socket (#40977)
GitOrigin-RevId: 6cb2520286cdb58096beef354c84572545c7205c
1 parent 1638a74 commit e3ac9f4

File tree

8 files changed

+99
-3
lines changed

8 files changed

+99
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/convex/Cargo.oss.lock

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ dependencies = [
207207
"rand 0.9.0",
208208
"serde",
209209
"serde_json",
210+
"strum",
210211
"uuid",
211212
]
212213

@@ -536,6 +537,12 @@ dependencies = [
536537
"http",
537538
]
538539

540+
[[package]]
541+
name = "heck"
542+
version = "0.5.0"
543+
source = "registry+https://github.com/rust-lang/crates.io-index"
544+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
545+
539546
[[package]]
540547
name = "http"
541548
version = "1.3.1"
@@ -1279,6 +1286,12 @@ dependencies = [
12791286
"untrusted",
12801287
]
12811288

1289+
[[package]]
1290+
name = "rustversion"
1291+
version = "1.0.12"
1292+
source = "registry+https://github.com/rust-lang/crates.io-index"
1293+
checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06"
1294+
12821295
[[package]]
12831296
name = "rusty-fork"
12841297
version = "0.3.0"
@@ -1447,6 +1460,28 @@ version = "1.2.0"
14471460
source = "registry+https://github.com/rust-lang/crates.io-index"
14481461
checksum = "a8f112729512f8e442d81f95a8a7ddf2b7c6b8a1a6f509a95864142b30cab2d3"
14491462

1463+
[[package]]
1464+
name = "strum"
1465+
version = "0.27.1"
1466+
source = "registry+https://github.com/rust-lang/crates.io-index"
1467+
checksum = "f64def088c51c9510a8579e3c5d67c65349dcf755e5479ad3d010aa6454e2c32"
1468+
dependencies = [
1469+
"strum_macros",
1470+
]
1471+
1472+
[[package]]
1473+
name = "strum_macros"
1474+
version = "0.27.1"
1475+
source = "registry+https://github.com/rust-lang/crates.io-index"
1476+
checksum = "c77a8c5abcaf0f9ce05d62342b7d298c346515365c36b673df4ebe3ced01fde8"
1477+
dependencies = [
1478+
"heck",
1479+
"proc-macro2",
1480+
"quote",
1481+
"rustversion",
1482+
"syn 2.0.95",
1483+
]
1484+
14501485
[[package]]
14511486
name = "subtle"
14521487
version = "2.5.0"

crates/convex/sync_types/Cargo.oss.lock

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ dependencies = [
9191
"rand 0.9.0",
9292
"serde",
9393
"serde_json",
94+
"strum",
9495
"uuid",
9596
]
9697

@@ -252,6 +253,12 @@ dependencies = [
252253
"http",
253254
]
254255

256+
[[package]]
257+
name = "heck"
258+
version = "0.5.0"
259+
source = "registry+https://github.com/rust-lang/crates.io-index"
260+
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
261+
255262
[[package]]
256263
name = "http"
257264
version = "1.3.1"
@@ -508,6 +515,12 @@ dependencies = [
508515
"windows-sys",
509516
]
510517

518+
[[package]]
519+
name = "rustversion"
520+
version = "1.0.12"
521+
source = "registry+https://github.com/rust-lang/crates.io-index"
522+
checksum = "4f3208ce4d8448b3f3e7d168a73f5e0c43a61e32930de3bceeccedb388b6bf06"
523+
511524
[[package]]
512525
name = "rusty-fork"
513526
version = "0.3.0"
@@ -570,6 +583,28 @@ dependencies = [
570583
"digest",
571584
]
572585

586+
[[package]]
587+
name = "strum"
588+
version = "0.27.1"
589+
source = "registry+https://github.com/rust-lang/crates.io-index"
590+
checksum = "f64def088c51c9510a8579e3c5d67c65349dcf755e5479ad3d010aa6454e2c32"
591+
dependencies = [
592+
"strum_macros",
593+
]
594+
595+
[[package]]
596+
name = "strum_macros"
597+
version = "0.27.1"
598+
source = "registry+https://github.com/rust-lang/crates.io-index"
599+
checksum = "c77a8c5abcaf0f9ce05d62342b7d298c346515365c36b673df4ebe3ced01fde8"
600+
dependencies = [
601+
"heck",
602+
"proc-macro2",
603+
"quote",
604+
"rustversion",
605+
"syn 2.0.95",
606+
]
607+
573608
[[package]]
574609
name = "syn"
575610
version = "1.0.109"

crates/convex/sync_types/Cargo.oss.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ proptest-derive = { optional = true, version = "0.5.0" }
2424
rand = { version = "0.9" }
2525
serde = { features = [ "derive" ], version = "1" }
2626
serde_json = { features = [ "float_roundtrip", "preserve_order" ], version = "1" }
27+
strum = { version = "0.27", features = [ "derive" ] }
2728
uuid = { features = [ "serde", "v4" ], version = "1.6" }
2829

2930
[dev-dependencies]

crates/convex/sync_types/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ serde_json = { workspace = true, features = [
2727
"float_roundtrip",
2828
"preserve_order",
2929
] }
30+
strum = { workspace = true }
3031
uuid = { workspace = true, features = ["serde", "v4"] }
3132

3233
[dev-dependencies]

crates/convex/sync_types/src/types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use serde::{
1515
Serialize,
1616
};
1717
use serde_json::Value as JsonValue;
18+
use strum::AsRefStr;
1819
use uuid::Uuid;
1920

2021
#[cfg(any(test, feature = "testing"))]
@@ -100,7 +101,7 @@ pub enum QuerySetModification {
100101
Remove { query_id: QueryId },
101102
}
102103

103-
#[derive(Clone, Debug, Eq, PartialEq)]
104+
#[derive(Clone, Debug, Eq, PartialEq, AsRefStr)]
104105
#[cfg_attr(any(test, feature = "testing"), derive(proptest_derive::Arbitrary))]
105106
pub enum ClientMessage {
106107
Connect {

crates/local_backend/src/subs/metrics.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ pub fn log_websocket_message_in() {
2828
log_counter(&BACKEND_WS_IN_TOTAL, 1);
2929
}
3030

31+
register_convex_histogram!(
32+
WS_CLIENT_MESSAGE_BYTES,
33+
"Size of client message received by the sync socket",
34+
&["type"]
35+
);
36+
pub fn log_websocket_client_message_bytes(bytes: usize, message_type: String) {
37+
log_distribution_with_labels(
38+
&WS_CLIENT_MESSAGE_BYTES,
39+
bytes as f64,
40+
vec![StaticMetricLabel::new("type", message_type)],
41+
);
42+
}
43+
3144
register_convex_counter!(BACKEND_PING_TOTAL, "Number of websocket pings sent");
3245
pub fn log_websocket_ping() {
3346
log_counter(&BACKEND_PING_TOTAL, 1);

crates/local_backend/src/subs/mod.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ use sync::{
5454
SyncWorkerConfig,
5555
};
5656
use sync_types::{
57+
ClientMessage,
5758
IdentityVersion,
5859
SessionId,
5960
};
@@ -76,7 +77,10 @@ use metrics::{
7677
websocket_upgrade_timer,
7778
};
7879

79-
use crate::RouterState;
80+
use crate::{
81+
subs::metrics::log_websocket_client_message_bytes,
82+
RouterState,
83+
};
8084

8185
/// How often heartbeat pings are sent.
8286
const HEARTBEAT_INTERVAL: Duration = Duration::from_secs(5);
@@ -161,7 +165,8 @@ async fn run_sync_socket(
161165

162166
match message {
163167
Message::Text(s) => {
164-
let body = serde_json::from_str::<JsonValue>(&s)
168+
let client_message_size = s.len();
169+
let body: ClientMessage = serde_json::from_str::<JsonValue>(&s)
165170
.map_err(|e| anyhow::anyhow!(e))
166171
.and_then(|body| body.try_into())
167172
.map_err(|e| {
@@ -170,6 +175,10 @@ async fn run_sync_socket(
170175
format!("Received Invalid JSON on websocket: {e}"),
171176
))
172177
})?;
178+
log_websocket_client_message_bytes(
179+
client_message_size,
180+
body.as_ref().to_string(),
181+
);
173182
log_websocket_message_in();
174183
if client_tx.send((body, st.runtime.monotonic_now())).is_err() {
175184
break;

0 commit comments

Comments
 (0)