record FoundIn with metrics instead of traces

This is much more efficient in terms of network use and data storage,
and also easier to visualize.
This commit is contained in:
Charles Hall 2024-06-04 13:26:23 -07:00
parent 9364d44ce2
commit 0c2094a56f
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
8 changed files with 142 additions and 68 deletions

View file

@ -8,17 +8,20 @@ use service::rooms::timeline::PduCount;
use tracing::error;
use crate::{
database::KeyValueDatabase, observability::FoundIn, service, services,
utils, Error, PduEvent, Result,
database::KeyValueDatabase,
observability::{FoundIn, Lookup, METRICS},
service, services, utils, Error, PduEvent, Result,
};
impl service::rooms::timeline::Data for KeyValueDatabase {
#[tracing::instrument(skip(self), fields(cache_result))]
#[tracing::instrument(skip(self))]
fn last_timeline_count(
&self,
sender_user: &UserId,
room_id: &RoomId,
) -> Result<PduCount> {
let lookup = Lookup::LastTimelineCount;
match self
.lasttimelinecount_cache
.lock()
@ -35,15 +38,15 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
r.ok()
})
{
FoundIn::Database.record("cache_result");
METRICS.record_lookup(lookup, FoundIn::Database);
Ok(*v.insert(last_count.0))
} else {
FoundIn::Nothing.record("cache_result");
METRICS.record_lookup(lookup, FoundIn::Nothing);
Ok(PduCount::Normal(0))
}
}
hash_map::Entry::Occupied(o) => {
FoundIn::Cache.record("cache_result");
METRICS.record_lookup(lookup, FoundIn::Cache);
Ok(*o.get())
}
}
@ -125,10 +128,12 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
/// Returns the pdu.
///
/// Checks the `eventid_outlierpdu` Tree if not found in the timeline.
#[tracing::instrument(skip(self), fields(cache_result))]
#[tracing::instrument(skip(self))]
fn get_pdu(&self, event_id: &EventId) -> Result<Option<Arc<PduEvent>>> {
let lookup = Lookup::Pdu;
if let Some(p) = self.pdu_cache.lock().unwrap().get_mut(event_id) {
FoundIn::Cache.record("cache_result");
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(Some(Arc::clone(p)));
}
@ -149,14 +154,14 @@ impl service::rooms::timeline::Data for KeyValueDatabase {
)?
.map(Arc::new)
{
FoundIn::Database.record("cache_result");
METRICS.record_lookup(lookup, FoundIn::Database);
self.pdu_cache
.lock()
.unwrap()
.insert(event_id.to_owned(), Arc::clone(&pdu));
Ok(Some(pdu))
} else {
FoundIn::Nothing.record("cache_result");
METRICS.record_lookup(lookup, FoundIn::Nothing);
Ok(None)
}
}