From 22dd7f1a54251310aa19b6a5b0e90d518ccfb56a Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Tue, 4 Jun 2024 12:24:01 -0700 Subject: [PATCH] move FoundIn to observability.rs --- src/api/server_server.rs | 5 ++-- src/database/key_value/rooms/auth_chain.rs | 5 +--- src/database/key_value/rooms/short.rs | 6 ++-- src/database/key_value/rooms/state_cache.rs | 5 ++-- src/database/key_value/rooms/timeline.rs | 6 ++-- src/observability.rs | 31 +++++++++++++++++++++ src/service/rooms/state_accessor.rs | 3 +- src/service/rooms/state_compressor.rs | 6 +--- src/utils.rs | 29 ------------------- 9 files changed, 43 insertions(+), 53 deletions(-) diff --git a/src/api/server_server.rs b/src/api/server_server.rs index 07fda0e7..f9d56705 100644 --- a/src/api/server_server.rs +++ b/src/api/server_server.rs @@ -67,10 +67,9 @@ use tracing::{debug, error, field, warn}; use crate::{ api::client_server::{self, claim_keys_helper, get_keys_helper}, + observability::FoundIn, service::pdu::{gen_event_id_canonical_json, PduBuilder}, - services, - utils::{self, FoundIn}, - Ar, Error, PduEvent, Ra, Result, + services, utils, Ar, Error, PduEvent, Ra, Result, }; /// Wraps either an literal IP address plus port, or a hostname plus complement diff --git a/src/database/key_value/rooms/auth_chain.rs b/src/database/key_value/rooms/auth_chain.rs index 248eb53f..f1e23491 100644 --- a/src/database/key_value/rooms/auth_chain.rs +++ b/src/database/key_value/rooms/auth_chain.rs @@ -1,10 +1,7 @@ use std::{collections::HashSet, mem::size_of, sync::Arc}; use crate::{ - database::KeyValueDatabase, - service, - utils::{self, FoundIn}, - Result, + database::KeyValueDatabase, observability::FoundIn, service, utils, Result, }; impl service::rooms::auth_chain::Data for KeyValueDatabase { diff --git a/src/database/key_value/rooms/short.rs b/src/database/key_value/rooms/short.rs index bacbab4c..4cc7fc47 100644 --- a/src/database/key_value/rooms/short.rs +++ b/src/database/key_value/rooms/short.rs @@ -3,10 +3,8 @@ use std::sync::Arc; use ruma::{events::StateEventType, EventId, RoomId}; use crate::{ - database::KeyValueDatabase, - service, services, - utils::{self, FoundIn}, - Error, Result, + database::KeyValueDatabase, observability::FoundIn, service, services, + utils, Error, Result, }; impl service::rooms::short::Data for KeyValueDatabase { diff --git a/src/database/key_value/rooms/state_cache.rs b/src/database/key_value/rooms/state_cache.rs index ce8ce4df..84a8f668 100644 --- a/src/database/key_value/rooms/state_cache.rs +++ b/src/database/key_value/rooms/state_cache.rs @@ -8,10 +8,9 @@ use ruma::{ use crate::{ database::KeyValueDatabase, + observability::FoundIn, service::{self, appservice::RegistrationInfo}, - services, - utils::{self, FoundIn}, - Error, Result, + services, utils, Error, Result, }; impl service::rooms::state_cache::Data for KeyValueDatabase { diff --git a/src/database/key_value/rooms/timeline.rs b/src/database/key_value/rooms/timeline.rs index 5a33f72d..3bfbff8d 100644 --- a/src/database/key_value/rooms/timeline.rs +++ b/src/database/key_value/rooms/timeline.rs @@ -8,10 +8,8 @@ use service::rooms::timeline::PduCount; use tracing::error; use crate::{ - database::KeyValueDatabase, - service, services, - utils::{self, FoundIn}, - Error, PduEvent, Result, + database::KeyValueDatabase, observability::FoundIn, service, services, + utils, Error, PduEvent, Result, }; impl service::rooms::timeline::Data for KeyValueDatabase { diff --git a/src/observability.rs b/src/observability.rs index 5bdbccff..e1e681ef 100644 --- a/src/observability.rs +++ b/src/observability.rs @@ -40,6 +40,37 @@ impl Drop for Guard { } } +/// Type to record cache performance in a tracing span field. +pub(crate) enum FoundIn { + /// Found in cache + Cache, + /// Cache miss, but it was in the database. The cache has been updated. + Database, + /// Cache and database miss, but another server had it. The cache has been + /// updated. + Remote, + /// The entry could not be found anywhere. + Nothing, +} + +impl FoundIn { + /// Returns a stringified representation of the current value + fn value(&self) -> &'static str { + match self { + FoundIn::Cache => "hit", + FoundIn::Database => "miss-database", + FoundIn::Remote => "miss-remote", + FoundIn::Nothing => "not-found", + } + } + + /// Record the current value to the current [`tracing::Span`] + // TODO: use tracing::Value instead if it ever becomes accessible + pub(crate) fn record(&self, field: &str) { + tracing::Span::current().record(field, self.value()); + } +} + /// Initialize observability pub(crate) fn init(config: &Config) -> Result { let config_filter_layer = || EnvFilter::try_new(&config.log); diff --git a/src/service/rooms/state_accessor.rs b/src/service/rooms/state_accessor.rs index 844cccc0..67ebf8ec 100644 --- a/src/service/rooms/state_accessor.rs +++ b/src/service/rooms/state_accessor.rs @@ -28,7 +28,8 @@ use tokio::sync::MutexGuard; use tracing::{error, warn}; use crate::{ - service::pdu::PduBuilder, services, utils::FoundIn, Error, PduEvent, Result, + observability::FoundIn, service::pdu::PduBuilder, services, Error, + PduEvent, Result, }; pub(crate) struct Service { diff --git a/src/service/rooms/state_compressor.rs b/src/service/rooms/state_compressor.rs index 85d55e97..942dd67c 100644 --- a/src/service/rooms/state_compressor.rs +++ b/src/service/rooms/state_compressor.rs @@ -10,11 +10,7 @@ use lru_cache::LruCache; use ruma::{EventId, RoomId}; use self::data::StateDiff; -use crate::{ - services, - utils::{self, FoundIn}, - Result, -}; +use crate::{observability::FoundIn, services, utils, Result}; #[derive(Clone)] pub(crate) struct CompressedStateLayer { diff --git a/src/utils.rs b/src/utils.rs index 746d0b84..5b59c8aa 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -245,35 +245,6 @@ pub(crate) fn truncate_str_for_debug( } } -/// Type to record cache performance in a tracing span field. -pub(crate) enum FoundIn { - /// Found in cache - Cache, - /// Cache miss, but it was in the database. The cache has been updated. - Database, - /// Cache and database miss, but another server had it. The cache has been - /// updated. - Remote, - /// The entry could not be found anywhere. - Nothing, -} - -impl FoundIn { - fn value(&self) -> &'static str { - match self { - FoundIn::Cache => "hit", - FoundIn::Database => "miss-database", - FoundIn::Remote => "miss-remote", - FoundIn::Nothing => "not-found", - } - } - - // TODO: use tracing::Value instead if it ever becomes accessible - pub(crate) fn record(&self, field: &str) { - tracing::Span::current().record(field, self.value()); - } -} - #[cfg(test)] mod tests { use crate::utils::truncate_str_for_debug;