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 7076c13058
commit 72860d98db
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
8 changed files with 139 additions and 67 deletions

View file

@ -8,7 +8,7 @@ use ruma::{
use crate::{
database::KeyValueDatabase,
observability::FoundIn,
observability::{FoundIn, FoundKind, METRICS},
service::{self, appservice::RegistrationInfo},
services, utils, Error, Result,
};
@ -171,19 +171,21 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
Ok(())
}
#[tracing::instrument(skip(self), fields(cache_result))]
#[tracing::instrument(skip(self))]
fn get_our_real_users(
&self,
room_id: &RoomId,
) -> Result<Arc<HashSet<OwnedUserId>>> {
let found_kind = FoundKind::OurRealUsers;
let maybe =
self.our_real_users_cache.read().unwrap().get(room_id).cloned();
if let Some(users) = maybe {
FoundIn::Cache.record("cache_result");
METRICS.record_lookup(found_kind, FoundIn::Cache);
Ok(users)
} else {
self.update_joined_count(room_id)?;
FoundIn::Database.record("cache_result");
METRICS.record_lookup(found_kind, FoundIn::Database);
Ok(Arc::clone(
self.our_real_users_cache.read().unwrap().get(room_id).unwrap(),
))
@ -192,13 +194,15 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
#[tracing::instrument(
skip(self, appservice),
fields(cache_result, appservice_id = appservice.registration.id),
fields(appservice_id = appservice.registration.id),
)]
fn appservice_in_room(
&self,
room_id: &RoomId,
appservice: &RegistrationInfo,
) -> Result<bool> {
let found_kind = FoundKind::AppserviceInRoom;
let maybe = self
.appservice_in_room_cache
.read()
@ -208,7 +212,7 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
.copied();
if let Some(b) = maybe {
FoundIn::Cache.record("cache_result");
METRICS.record_lookup(found_kind, FoundIn::Cache);
Ok(b)
} else {
let bridge_user_id = UserId::parse_with_server_name(
@ -225,7 +229,7 @@ impl service::rooms::state_cache::Data for KeyValueDatabase {
})
});
FoundIn::Database.record("cache_result");
METRICS.record_lookup(found_kind, FoundIn::Database);
self.appservice_in_room_cache
.write()
.unwrap()