Skip to content

Commit

Permalink
Merge pull request #1109 from AppFlowy-IO/measure-redis-stream-read-l…
Browse files Browse the repository at this point in the history
…atency

chore: add prometheus histogram metrics for collab redis update read latency
  • Loading branch information
Horusiath authored Dec 31, 2024
2 parents 327ba71 + a0bed04 commit 21d5325
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
3 changes: 2 additions & 1 deletion services/appflowy-collaborate/src/group/group_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,8 @@ impl CollabGroup {
}
res = updates.next() => {
match res {
Some(Ok((_message_id, update))) => {
Some(Ok((message_id, update))) => {
state.metrics.observe_collab_stream_latency(message_id.timestamp_ms);
Self::handle_inbound_update(&state, update).await;
},
Some(Err(err)) => {
Expand Down
24 changes: 24 additions & 0 deletions services/appflowy-collaborate/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use chrono::Utc;
use prometheus_client::metrics::counter::Counter;
use prometheus_client::metrics::gauge::Gauge;
use prometheus_client::metrics::histogram::Histogram;
Expand All @@ -22,6 +23,8 @@ pub struct CollabRealtimeMetrics {
pub(crate) collab_size: Histogram,
/// How big is the collab (with history, after applying all updates).
pub(crate) full_collab_size: Histogram,
/// How long does it take since collab update is send to a stream to be read from it.
pub(crate) collab_stream_latency: Histogram,
}

impl CollabRealtimeMetrics {
Expand Down Expand Up @@ -54,6 +57,13 @@ impl CollabRealtimeMetrics {
]
.into_iter(),
),
// collab update xadd-to-xread latency: 5ms, 50ms, 100ms, 500ms, 1s, 5s, 10s, 30s, 60s
collab_stream_latency: Histogram::new(
[
5.0, 50.0, 100.0, 500.0, 1000.0, 5000.0, 10000.0, 30000.0, 60000.0,
]
.into_iter(),
),
load_collab_count: Default::default(),
load_full_collab_count: Default::default(),
}
Expand Down Expand Up @@ -112,8 +122,22 @@ impl CollabRealtimeMetrics {
"number of collab loads (with history)",
metrics.load_full_collab_count.clone(),
);
realtime_registry.register(
"collab_stream_latency",
"latency since collab update is send to a stream to be read from it",
metrics.collab_stream_latency.clone(),
);
metrics
}

pub fn observe_collab_stream_latency(&self, message_id_timestamp: u64) {
let now = Utc::now().timestamp_millis() as u64;
if now > message_id_timestamp {
self
.collab_stream_latency
.observe((now - message_id_timestamp) as f64);
}
}
}

#[derive(Clone)]
Expand Down

0 comments on commit 21d5325

Please sign in to comment.