From 799332946ce734fae0f9ae25c14a2dc8c4a304bc Mon Sep 17 00:00:00 2001 From: Nikita Vilunov Date: Thu, 25 Apr 2024 18:10:56 +0200 Subject: [PATCH] add instrumentation --- crates/lavina-core/src/player.rs | 14 ++++++++++++++ crates/lavina-core/src/repo/mod.rs | 1 + crates/lavina-core/src/room.rs | 9 +++++++++ crates/projection-irc/src/lib.rs | 1 + 4 files changed, 25 insertions(+) diff --git a/crates/lavina-core/src/player.rs b/crates/lavina-core/src/player.rs index 2f5edb4..9f85ace 100644 --- a/crates/lavina-core/src/player.rs +++ b/crates/lavina-core/src/player.rs @@ -59,6 +59,7 @@ pub struct PlayerConnection { } impl PlayerConnection { /// Handled in [Player::send_message]. + #[tracing::instrument(skip(self, body), name = "PlayerConnection::send_message")] pub async fn send_message(&mut self, room_id: RoomId, body: Str) -> Result { let (promise, deferred) = oneshot(); let cmd = ClientCommand::SendMessage { room_id, body, promise }; @@ -67,6 +68,7 @@ impl PlayerConnection { } /// Handled in [Player::join_room]. + #[tracing::instrument(skip(self), name = "PlayerConnection::join_room")] pub async fn join_room(&mut self, room_id: RoomId) -> Result { let (promise, deferred) = oneshot(); let cmd = ClientCommand::JoinRoom { room_id, promise }; @@ -75,6 +77,7 @@ impl PlayerConnection { } /// Handled in [Player::change_topic]. + #[tracing::instrument(skip(self, new_topic), name = "PlayerConnection::change_topic")] pub async fn change_topic(&mut self, room_id: RoomId, new_topic: Str) -> Result<()> { let (promise, deferred) = oneshot(); let cmd = ClientCommand::ChangeTopic { @@ -87,6 +90,7 @@ impl PlayerConnection { } /// Handled in [Player::leave_room]. + #[tracing::instrument(skip(self), name = "PlayerConnection::leave_room")] pub async fn leave_room(&mut self, room_id: RoomId) -> Result<()> { let (promise, deferred) = oneshot(); let cmd = ClientCommand::LeaveRoom { room_id, promise }; @@ -99,6 +103,7 @@ impl PlayerConnection { } /// Handled in [Player::get_rooms]. + #[tracing::instrument(skip(self), name = "PlayerConnection::get_rooms")] pub async fn get_rooms(&self) -> Result> { let (promise, deferred) = oneshot(); let cmd = ClientCommand::GetRooms { promise }; @@ -107,6 +112,7 @@ impl PlayerConnection { } /// Handler in [Player::send_dialog_message]. + #[tracing::instrument(skip(self, body), name = "PlayerConnection::send_dialog_message")] pub async fn send_dialog_message(&self, recipient: PlayerId, body: Str) -> Result<()> { let (promise, deferred) = oneshot(); let cmd = ClientCommand::SendDialogMessage { @@ -400,6 +406,7 @@ impl Player { } /// Handle an incoming update by changing the internal state and broadcasting it to all connections if necessary. + #[tracing::instrument(skip(self, update), name = "Player::handle_update")] async fn handle_update(&mut self, update: Updates) { log::debug!( "Player received an update, broadcasting to {} connections", @@ -461,6 +468,7 @@ impl Player { } } + #[tracing::instrument(skip(self), name = "Player::join_room")] async fn join_room(&mut self, connection_id: ConnectionId, room_id: RoomId) -> JoinResult { if self.banned_from.contains(&room_id) { return JoinResult::Banned; @@ -488,6 +496,7 @@ impl Player { JoinResult::Success(room_info) } + #[tracing::instrument(skip(self), name = "Player::leave_room")] async fn leave_room(&mut self, connection_id: ConnectionId, room_id: RoomId) { let room = self.my_rooms.remove(&room_id); if let Some(room) = room { @@ -501,6 +510,7 @@ impl Player { self.broadcast_update(update, connection_id).await; } + #[tracing::instrument(skip(self, body), name = "Player::send_message")] async fn send_message(&mut self, connection_id: ConnectionId, room_id: RoomId, body: Str) -> SendMessageResult { let Some(room) = self.my_rooms.get(&room_id) else { tracing::info!("no room found"); @@ -518,6 +528,7 @@ impl Player { SendMessageResult::Success(created_at) } + #[tracing::instrument(skip(self, new_topic), name = "Player::change_topic")] async fn change_topic(&mut self, connection_id: ConnectionId, room_id: RoomId, new_topic: Str) { let Some(room) = self.my_rooms.get(&room_id) else { tracing::info!("no room found"); @@ -528,6 +539,7 @@ impl Player { self.broadcast_update(update, connection_id).await; } + #[tracing::instrument(skip(self), name = "Player::get_rooms")] async fn get_rooms(&self) -> Vec { let mut response = vec![]; for (_, handle) in &self.my_rooms { @@ -536,6 +548,7 @@ impl Player { response } + #[tracing::instrument(skip(self, body), name = "Player::send_dialog_message")] async fn send_dialog_message(&self, connection_id: ConnectionId, recipient: PlayerId, body: Str) { let created_at = chrono::Utc::now(); self.dialogs.send_message(self.player_id.clone(), recipient.clone(), body.clone(), &created_at).await.unwrap(); @@ -552,6 +565,7 @@ impl Player { /// /// This is called after handling a client command. /// Sending the update to the connection which sent the command is handled by the connection itself. + #[tracing::instrument(skip(self, update), name = "Player::broadcast_update")] async fn broadcast_update(&self, update: Updates, except: ConnectionId) { for (a, b) in &self.connections { if ConnectionId(a) == except { diff --git a/crates/lavina-core/src/repo/mod.rs b/crates/lavina-core/src/repo/mod.rs index 9c7aff6..ee92e21 100644 --- a/crates/lavina-core/src/repo/mod.rs +++ b/crates/lavina-core/src/repo/mod.rs @@ -84,6 +84,7 @@ impl Storage { Ok(id) } + #[tracing::instrument(skip(self, content, created_at), name = "Storage::insert_message")] pub async fn insert_message( &mut self, room_id: u32, diff --git a/crates/lavina-core/src/room.rs b/crates/lavina-core/src/room.rs index d50e169..b8ce505 100644 --- a/crates/lavina-core/src/room.rs +++ b/crates/lavina-core/src/room.rs @@ -138,12 +138,14 @@ impl RoomRegistryInner { #[derive(Clone)] pub struct RoomHandle(Arc>); impl RoomHandle { + #[tracing::instrument(skip(self, player_handle), name = "RoomHandle::subscribe")] pub async fn subscribe(&self, player_id: &PlayerId, player_handle: PlayerHandle) { let mut lock = self.0.write().await; tracing::info!("Adding a subscriber to a room"); lock.subscriptions.insert(player_id.clone(), player_handle); } + #[tracing::instrument(skip(self), name = "RoomHandle::add_member")] pub async fn add_member(&self, player_id: &PlayerId, player_storage_id: u32) { let mut lock = self.0.write().await; tracing::info!("Adding a new member to a room"); @@ -157,11 +159,13 @@ impl RoomHandle { lock.broadcast_update(update, player_id).await; } + #[tracing::instrument(skip(self), name = "RoomHandle::unsubscribe")] pub async fn unsubscribe(&self, player_id: &PlayerId) { let mut lock = self.0.write().await; lock.subscriptions.remove(player_id); } + #[tracing::instrument(skip(self), name = "RoomHandle::remove_member")] pub async fn remove_member(&self, player_id: &PlayerId, player_storage_id: u32) { let mut lock = self.0.write().await; tracing::info!("Removing a member from a room"); @@ -175,6 +179,7 @@ impl RoomHandle { lock.broadcast_update(update, player_id).await; } + #[tracing::instrument(skip(self, body, created_at), name = "RoomHandle::send_message")] pub async fn send_message(&self, player_id: &PlayerId, body: Str, created_at: DateTime) { let mut lock = self.0.write().await; let res = lock.send_message(player_id, body, created_at).await; @@ -183,6 +188,7 @@ impl RoomHandle { } } + #[tracing::instrument(skip(self), name = "RoomHandle::get_room_info")] pub async fn get_room_info(&self) -> RoomInfo { let lock = self.0.read().await; RoomInfo { @@ -192,6 +198,7 @@ impl RoomHandle { } } + #[tracing::instrument(skip(self, new_topic), name = "RoomHandle::set_topic")] pub async fn set_topic(&self, changer_id: &PlayerId, new_topic: Str) { let mut lock = self.0.write().await; let storage_id = lock.storage_id; @@ -220,6 +227,7 @@ struct Room { storage: Storage, } impl Room { + #[tracing::instrument(skip(self, body, created_at), name = "Room::send_message")] async fn send_message(&mut self, author_id: &PlayerId, body: Str, created_at: DateTime) -> Result<()> { tracing::info!("Adding a message to room"); self.storage @@ -246,6 +254,7 @@ impl Room { /// /// This is called after handling a client command. /// Sending the update to the player who sent the command is handled by the player actor. + #[tracing::instrument(skip(self, update), name = "Room::broadcast_update")] async fn broadcast_update(&self, update: Updates, except: &PlayerId) { tracing::debug!("Broadcasting an update to {} subs", self.subscriptions.len()); for (player_id, sub) in &self.subscriptions { diff --git a/crates/projection-irc/src/lib.rs b/crates/projection-irc/src/lib.rs index 342682a..a320c5d 100644 --- a/crates/projection-irc/src/lib.rs +++ b/crates/projection-irc/src/lib.rs @@ -678,6 +678,7 @@ enum HandleResult { Leave, } +#[tracing::instrument(skip_all, name = "handle_incoming_message")] async fn handle_incoming_message( buffer: &str, config: &ServerConfig,