From 5bd7c6fd1dc77ecbbc8fbc6ed09084ec946a6f87 Mon Sep 17 00:00:00 2001 From: niko Date: Sat, 20 Apr 2024 21:49:17 -0700 Subject: [PATCH] feat: logging --- crates/alerion_core/src/config.rs | 7 ++++++ crates/alerion_core/src/filesystem.rs | 3 +++ crates/alerion_core/src/logging.rs | 10 -------- crates/alerion_core/src/servers.rs | 28 +++++++++++++++++++--- crates/alerion_core/src/sftp/server.rs | 1 + crates/alerion_core/src/websocket/conn.rs | 6 +++++ crates/alerion_core/src/websocket/relay.rs | 16 ++++++++++++- 7 files changed, 57 insertions(+), 14 deletions(-) diff --git a/crates/alerion_core/src/config.rs b/crates/alerion_core/src/config.rs index 7cd1047..ae62adb 100644 --- a/crates/alerion_core/src/config.rs +++ b/crates/alerion_core/src/config.rs @@ -266,6 +266,7 @@ impl From for AlerionConfig { impl AlerionConfig { pub fn load(project_dirs: &directories::ProjectDirs) -> anyhow::Result { + tracing::info!("Loading Alerion config from {}", project_dirs.config_dir().display()); let config_path = project_dirs.config_dir().join("config.json"); let config = std::fs::read_to_string(&config_path).map_err(|e| { anyhow::anyhow!( @@ -276,6 +277,7 @@ impl AlerionConfig { })?; let config: AlerionConfig = serde_json::from_str(&config)?; + tracing::debug!("Loaded Alerion config: {:?}", config); Ok(config) } @@ -291,6 +293,8 @@ impl AlerionConfig { ) })?; + tracing::info!("Saved Alerion config to {}", config_path.display()); + Ok(()) } @@ -308,6 +312,9 @@ impl AlerionConfig { })?; let config: wings::Config = serde_yaml::from_str(&config)?; + + tracing::debug!("Imported Wings config: {:?}", config); + Ok(config.into()) } } diff --git a/crates/alerion_core/src/filesystem.rs b/crates/alerion_core/src/filesystem.rs index 78fae26..7b7f808 100644 --- a/crates/alerion_core/src/filesystem.rs +++ b/crates/alerion_core/src/filesystem.rs @@ -1,6 +1,7 @@ use anyhow::Context; use directories::ProjectDirs; +#[tracing::instrument] pub async fn setup_directories() -> anyhow::Result { let project_dirs = ProjectDirs::from("host", "pyro", "alerion") .context("couldn't determine a home directory for your operating system")?; @@ -8,6 +9,8 @@ pub async fn setup_directories() -> anyhow::Result { tokio::fs::create_dir_all(project_dirs.config_dir()).await?; tokio::fs::create_dir_all(project_dirs.data_dir()).await?; tokio::fs::create_dir_all(project_dirs.cache_dir()).await?; + + tracing::info!("Directories created"); Ok(project_dirs) } diff --git a/crates/alerion_core/src/logging.rs b/crates/alerion_core/src/logging.rs index 7f5930b..f16d769 100644 --- a/crates/alerion_core/src/logging.rs +++ b/crates/alerion_core/src/logging.rs @@ -1,5 +1,3 @@ -// use env_logger::TimestampPrecision; - pub fn splash() { println!( " @@ -10,11 +8,3 @@ pub fn splash() { ██ ██ ███████ ███████ ██ ██ ██ ██████ ██ ████ " ); } - -//pub fn setup() { -// env_logger::Builder::from_default_env() -// .filter_level(log::LevelFilter::Debug) -// .format_timestamp(Some(TimestampPrecision::Seconds)) -// .format_module_path(true) -// .init(); -//} diff --git a/crates/alerion_core/src/servers.rs b/crates/alerion_core/src/servers.rs index a3c209f..447b945 100644 --- a/crates/alerion_core/src/servers.rs +++ b/crates/alerion_core/src/servers.rs @@ -57,8 +57,11 @@ impl ServerPoolBuilder { Ok(self) } - + + #[tracing::instrument(skip(self))] pub fn build(self) -> ServerPool { + tracing::debug!("Server pool built"); + ServerPool { servers: RwLock::new(self.servers), remote_api: self.remote_api, @@ -84,9 +87,13 @@ impl ServerPool { let map = self.servers.read().await; match map.get(&uuid) { - Some(s) => Ok(Arc::clone(s)), + Some(s) => { + tracing::debug!("Server {uuid} found"); + Ok(Arc::clone(s)) + }, None => { + tracing::debug!("Server {uuid} not found, creating"); drop(map); self.create_server(uuid).await } @@ -112,7 +119,8 @@ impl ServerPool { self.servers.read().await.get(&uuid).cloned() } } - +//TODO: Remove allow(dead_code) when implemented +#[allow(dead_code)] pub struct ServerInfo { container: ContainerConfig, } @@ -142,6 +150,8 @@ impl From for String { } } +//TODO: Remove allow(dead_code) when implemented +#[allow(dead_code)] pub struct Server { start_time: Instant, uuid: Uuid, @@ -155,12 +165,14 @@ pub struct Server { } impl Server { + #[tracing::instrument(skip(server_info, remote_api, docker))] pub async fn new( uuid: Uuid, server_info: ServerInfo, remote_api: Arc, docker: Arc, ) -> Result, ServerError> { + tracing::debug!("Creating new server {uuid}"); let (send, recv) = channel(128); let server = Arc::new(Self { @@ -180,6 +192,8 @@ impl Server { server.create_docker_container().await?; + tracing::info!("Server {uuid} created"); + Ok(server) } @@ -215,6 +229,8 @@ impl Server { where F: FnOnce(ServerConnection) -> actix_web::Result<(ConnectionAddr, HttpResponse)>, { + tracing::info!("Setting up new websocket connection"); + let id = self.websocket_id_counter.fetch_add(1, Ordering::SeqCst); // setup the request channel for the websocket @@ -235,6 +251,9 @@ impl Server { } pub async fn send_to_available_websockets(&self, msg: ServerMessage) { + tracing::info!("Sending message to all available websockets"); + tracing::debug!("message: {:?}", msg); + let lock = self.websockets.read().await; for sender in lock.values() { @@ -247,7 +266,10 @@ impl Server { } } +#[tracing::instrument(skip(server))] async fn monitor_performance_metrics(server: Arc) { + tracing::info!("Starting performance metrics monitor for {}", &server.uuid); + loop { tokio::time::sleep(std::time::Duration::from_secs(1)).await; diff --git a/crates/alerion_core/src/sftp/server.rs b/crates/alerion_core/src/sftp/server.rs index e69de29..68007d8 100644 --- a/crates/alerion_core/src/sftp/server.rs +++ b/crates/alerion_core/src/sftp/server.rs @@ -0,0 +1 @@ +// This file intentionally left blank. diff --git a/crates/alerion_core/src/websocket/conn.rs b/crates/alerion_core/src/websocket/conn.rs index 8905394..eed9fd0 100644 --- a/crates/alerion_core/src/websocket/conn.rs +++ b/crates/alerion_core/src/websocket/conn.rs @@ -35,6 +35,8 @@ impl actix::Message for PanelMessage { pub type ConnectionAddr = Addr; +//TODO: Remove allow(dead_code) when implemented +#[allow(dead_code)] enum MessageError { InvalidJwt, Generic(String), @@ -102,6 +104,10 @@ impl WebsocketConnectionImpl { permissions: Cell::new(Permissions::empty()), } } + + pub fn id(&self) -> Uuid { + self.server_uuid + } pub fn handle_text(&self, msg: &str, ctx: &mut ::Context) -> Option<()> { // todo: behavior on bad JSON payload? right now just ignore diff --git a/crates/alerion_core/src/websocket/relay.rs b/crates/alerion_core/src/websocket/relay.rs index 17a668f..dc05693 100644 --- a/crates/alerion_core/src/websocket/relay.rs +++ b/crates/alerion_core/src/websocket/relay.rs @@ -17,6 +17,8 @@ impl ServerConnection { sender: Sender<(u32, PanelMessage)>, id: u32, ) -> Self { + tracing::debug!("Server connection created with id {}", id); + ServerConnection { auth_tracker, sender, @@ -25,24 +27,29 @@ impl ServerConnection { } pub fn set_authenticated(&self) { + tracing::debug!("Server connection {} authenticated", self.id); self.auth_tracker.set_auth(true); } pub fn is_authenticated(&self) -> bool { + tracing::debug!("Checking if server connection {} is authenticated", self.id); self.auth_tracker.get_auth() } pub fn send_if_authenticated(&self, msg: PanelMessage) { + tracing::debug!("Sending message to server connection {}", self.id); if self.auth_tracker.get_auth() { let _ = self.sender.try_send((self.id, msg)); } } pub fn force_send(&self, msg: PanelMessage) { + tracing::debug!("Forcing message to server connection {}", self.id); let _ = self.sender.try_send((self.id, msg)); } pub fn auth_tracker(&self) -> Arc { + tracing::debug!("Getting auth tracker for server connection {}", self.id); Arc::clone(&self.auth_tracker) } } @@ -53,7 +60,10 @@ pub struct ClientConnection { } impl ClientConnection { + #[tracing::instrument(skip(auth_tracker))] pub fn new(auth_tracker: Arc, ws_sender: ConnectionAddr) -> Self { + tracing::info!("Client connection created"); + Self { auth_tracker, ws_sender, @@ -82,12 +92,16 @@ impl ClientConnection { /// /// This would easily be fixable with another atomic check, but I'd rather avoid /// seemingly unnecessary atomic loads. + #[tracing::instrument(skip(self), fields(id=format!("{:?}", self.ws_sender)))] pub fn terminate(&self) { + tracing::info!("Terminating websocket connection"); self.expire_auth(); self.ws_sender.do_send(ServerMessage::Kill); } - + + #[tracing::instrument(skip(self), fields(id=format!("{:?}", self.ws_sender)))] pub fn expire_auth(&self) { + tracing::debug!("Auth expired."); self.auth_tracker.set_auth(false); }