Skip to content

Commit 6f86bc9

Browse files
feat(hermes): observe WS latency after flush; observe SSE latency post-encoding
Co-Authored-By: Tejas Badadare <[email protected]>
1 parent bb80ea5 commit 6f86bc9

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

apps/hermes/server/src/api/rest/v2/sse.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,30 +206,30 @@ where
206206
return Ok(None);
207207
}
208208

209+
210+
let price_update_data = price_feeds_with_update_data.update_data;
211+
let encoded_data: Vec<String> = price_update_data
212+
.into_iter()
213+
.map(|data| encoding.encode_str(&data))
214+
.collect();
215+
let binary_price_update = BinaryUpdate {
216+
encoding,
217+
data: encoded_data,
218+
};
219+
209220
let now_secs = std::time::SystemTime::now()
210221
.duration_since(std::time::UNIX_EPOCH)
211222
.map(|d| d.as_secs_f64())
212223
.unwrap_or(0.0);
213224
for pu in &parsed_price_updates {
214225
if let Some(receive_time) = pu.metadata.proof_available_time {
215-
let latency = now_secs - (receive_time as f64);
216226
state
217227
.metrics
218228
.sse_broadcast_latency
219-
.observe(latency.max(0.0));
229+
.observe((now_secs - (receive_time as f64)).max(0.0));
220230
}
221231
}
222232

223-
let price_update_data = price_feeds_with_update_data.update_data;
224-
let encoded_data: Vec<String> = price_update_data
225-
.into_iter()
226-
.map(|data| encoding.encode_str(&data))
227-
.collect();
228-
let binary_price_update = BinaryUpdate {
229-
encoding,
230-
data: encoded_data,
231-
};
232-
233233
Ok(Some(PriceUpdate {
234234
binary: binary_price_update,
235235
parsed: if parsed {

apps/hermes/server/src/api/ws.rs

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,28 @@ where
419419
}
420420
};
421421

422+
let batch_min_received_at = updates
423+
.price_feeds
424+
.iter()
425+
.filter_map(|u| u.received_at)
426+
.min();
427+
let batch_min_publish_time = updates
428+
.price_feeds
429+
.iter()
430+
.map(|u| u.price_feed.get_price_unchecked().publish_time)
431+
.min();
432+
433+
let batch_min_received_at = updates
434+
.price_feeds
435+
.iter()
436+
.filter_map(|u| u.received_at)
437+
.min();
438+
let batch_min_publish_time = updates
439+
.price_feeds
440+
.iter()
441+
.map(|u| u.price_feed.get_price_unchecked().publish_time)
442+
.min();
443+
422444
for update in updates.price_feeds {
423445
let config = self
424446
.price_feeds_with_config
@@ -433,17 +455,6 @@ where
433455
}
434456
}
435457

436-
let now_secs = std::time::SystemTime::now()
437-
.duration_since(std::time::UNIX_EPOCH)
438-
.map(|d| d.as_secs_f64())
439-
.unwrap_or(0.0);
440-
if let Some(received_at) = update.received_at {
441-
let latency = now_secs - (received_at as f64);
442-
self.ws_state
443-
.metrics
444-
.broadcast_latency
445-
.observe(latency.max(0.0));
446-
}
447458

448459
let message = serde_json::to_string(&ServerMessage::PriceUpdate {
449460
price_feed: RpcPriceFeed::from_price_feed_update(
@@ -510,6 +521,21 @@ where
510521
}
511522

512523
self.sender.flush().await?;
524+
let now_secs = std::time::SystemTime::now()
525+
.duration_since(std::time::UNIX_EPOCH)
526+
.map(|d| d.as_secs_f64())
527+
.unwrap_or(0.0);
528+
if let Some(min_recv) = batch_min_received_at {
529+
self.ws_state
530+
.metrics
531+
.broadcast_latency
532+
.observe((now_secs - (min_recv as f64)).max(0.0));
533+
} else if let Some(min_pub) = batch_min_publish_time {
534+
self.ws_state
535+
.metrics
536+
.broadcast_latency
537+
.observe((now_secs - (min_pub as f64)).max(0.0));
538+
}
513539
Ok(())
514540
}
515541

@@ -518,8 +544,7 @@ where
518544
let maybe_client_message = match message {
519545
Message::Close(_) => {
520546
// Closing the connection. We don't remove it from the subscribers
521-
// list, instead when the Subscriber struct is dropped the channel
522-
// to subscribers list will be closed and it will eventually get
547+
523548
// removed.
524549
tracing::trace!(id = self.id, "Subscriber Closed Connection.");
525550
self.ws_state

0 commit comments

Comments
 (0)