diff --git a/src/pyroscope.rs b/src/pyroscope.rs index 26787ba1..bad41c80 100644 --- a/src/pyroscope.rs +++ b/src/pyroscope.rs @@ -14,6 +14,8 @@ use crate::{ timer::Timer, }; +const LOG_TAG: &str = "Pyroscope::Agent"; + /// Pyroscope Agent Configuration. This is the configuration that is passed to the agent. /// # Example /// ``` @@ -58,7 +60,7 @@ impl PyroscopeConfig { /// ```ignore /// let mut config = PyroscopeConfig::new("http://localhost:8080", "my-app"); /// config.set_sample_rate(10) - /// .unwrap(); + /// ?; /// ``` pub fn sample_rate(self, sample_rate: i32) -> Self { Self { @@ -72,8 +74,7 @@ impl PyroscopeConfig { /// ```ignore /// use pyroscope::pyroscope::PyroscopeConfig; /// let config = PyroscopeConfig::new("http://localhost:8080", "my-app") - /// .tags(vec![("env", "dev")]) - /// .unwrap(); + /// .tags(vec![("env", "dev")])?; /// ``` pub fn tags(self, tags: &[(&str, &str)]) -> Self { // Convert &[(&str, &str)] to HashMap(String, String) @@ -100,7 +101,7 @@ impl PyroscopeConfig { /// ```ignore /// use pyroscope::pyroscope::PyroscopeAgentBuilder; /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app"); -/// let agent = builder.build().unwrap(); +/// let agent = builder.build()?; /// ``` pub struct PyroscopeAgentBuilder { /// Profiler backend @@ -130,7 +131,7 @@ impl PyroscopeAgentBuilder { /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app") /// .backend(Pprof::default()) /// .build() - /// .unwrap(); + /// ?; /// ``` pub fn backend(self, backend: T) -> Self where T: 'static + Backend { @@ -146,7 +147,7 @@ impl PyroscopeAgentBuilder { /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app") /// .sample_rate(99) /// .build() - /// .unwrap(); + /// ?; /// ``` pub fn sample_rate(self, sample_rate: i32) -> Self { Self { @@ -161,7 +162,7 @@ impl PyroscopeAgentBuilder { /// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app") /// .tags(vec![("env", "dev")]) /// .build() - /// .unwrap(); + /// ?; /// ``` pub fn tags(self, tags: &[(&str, &str)]) -> Self { Self { @@ -175,15 +176,15 @@ impl PyroscopeAgentBuilder { // Initiliaze the backend let backend = Arc::clone(&self.backend); backend.lock()?.initialize(self.config.sample_rate)?; - log::trace!("PyroscopeAgent - Backend initialized"); + log::trace!(target: LOG_TAG, "Backend initialized"); // Start Timer let timer = Timer::default().initialize()?; - log::trace!("PyroscopeAgent - Timer initialized"); + log::trace!(target: LOG_TAG, "Timer initialized"); // Start the SessionManager let session_manager = SessionManager::new()?; - log::trace!("PyroscopeAgent - SessionManager initialized"); + log::trace!(target: LOG_TAG, "SessionManager initialized"); // Return PyroscopeAgent Ok(PyroscopeAgent { @@ -217,44 +218,45 @@ pub struct PyroscopeAgent { impl Drop for PyroscopeAgent { /// Properly shutdown the agent. fn drop(&mut self) { - log::debug!("PyroscopeAgent - Dropping Agent"); + log::debug!(target: LOG_TAG, "PyroscopeAgent::drop()"); // Drop Timer listeners match self.timer.drop_listeners() { - Ok(_) => log::trace!("PyroscopeAgent - Dropped timer listeners"), - Err(_) => log::error!("PyroscopeAgent - Error Dropping timer listeners"), + Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer listeners"), + Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer listeners"), } // Wait for the Timer thread to finish if let Some(handle) = self.timer.handle.take() { match handle.join() { - Ok(_) => log::trace!("PyroscopeAgent - Dropped timer thread"), - Err(_) => log::error!("PyroscopeAgent - Error Dropping timer thread"), + Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer thread"), + Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer thread"), } } // Stop the SessionManager match self.session_manager.push(SessionSignal::Kill) { - Ok(_) => log::trace!("PyroscopeAgent - Sent kill signal to SessionManager"), - Err(_) => log::error!("PyroscopeAgent - Error sending kill signal to SessionManager"), + Ok(_) => log::trace!(target: LOG_TAG, "Sent kill signal to SessionManager"), + Err(_) => log::error!(target: LOG_TAG, "Error sending kill signal to SessionManager"), } if let Some(handle) = self.session_manager.handle.take() { match handle.join() { - Ok(_) => log::trace!("PyroscopeAgent - Dropped SessionManager thread"), - Err(_) => log::error!("PyroscopeAgent - Error Dropping SessionManager thread"), + Ok(_) => log::trace!(target: LOG_TAG, "Dropped SessionManager thread"), + Err(_) => log::error!(target: LOG_TAG, "Error Dropping SessionManager thread"), } } // Wait for main thread to finish + if let Some(handle) = self.handle.take() { match handle.join() { - Ok(_) => log::trace!("PyroscopeAgent - Dropped main thread"), - Err(_) => log::error!("PyroscopeAgent - Error Dropping main thread"), + Ok(_) => log::trace!(target: LOG_TAG, "Dropped main thread"), + Err(_) => log::error!(target: LOG_TAG, "Error Dropping main thread"), } } - log::debug!("PyroscopeAgent - Agent Dropped"); + log::debug!(target: LOG_TAG, "Agent Dropped"); } } @@ -263,7 +265,7 @@ impl PyroscopeAgent { /// /// # Example /// ```ignore - /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap(); + /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?; /// ``` pub fn builder>(url: S, application_name: S) -> PyroscopeAgentBuilder { // Build PyroscopeAgent @@ -271,7 +273,7 @@ impl PyroscopeAgent { } fn _start(&mut self) -> Result<()> { - log::debug!("PyroscopeAgent - Starting"); + log::debug!(target: LOG_TAG, "Starting"); // Create a clone of Backend let backend = Arc::clone(&self.backend); @@ -294,10 +296,10 @@ impl PyroscopeAgent { let stx = self.session_manager.tx.clone(); self.handle = Some(std::thread::spawn(move || { - log::trace!("PyroscopeAgent - Main Thread started"); + log::trace!(target: LOG_TAG, "Main Thread started"); while let Ok(until) = rx.recv() { - log::trace!("PyroscopeAgent - Sending session {}", until); + log::trace!(target: LOG_TAG, "Sending session {}", until); // Generate report from backend let report = backend.lock()?.report()?; @@ -310,7 +312,7 @@ impl PyroscopeAgent { )?))?; if until == 0 { - log::trace!("PyroscopeAgent - Session Killed"); + log::trace!(target: LOG_TAG, "Session Killed"); let (lock, cvar) = &*pair; let mut running = lock.lock()?; @@ -329,18 +331,18 @@ impl PyroscopeAgent { /// Start profiling and sending data. The agent will keep running until stopped. The agent will send data to the server every 10s secondy. /// # Example /// ```ignore - /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap(); + /// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?; /// agent.start(); /// ``` pub fn start(&mut self) { match self._start() { - Ok(_) => log::trace!("PyroscopeAgent - Agent started"), - Err(_) => log::error!("PyroscopeAgent - Error starting agent"), + Ok(_) => log::trace!(target: LOG_TAG, "Agent started"), + Err(_) => log::error!(target: LOG_TAG, "Error starting agent"), } } fn _stop(&mut self) -> Result<()> { - log::debug!("PyroscopeAgent - Stopping"); + log::debug!(target: LOG_TAG, "Stopping"); // get tx and send termination signal if let Some(sender) = self.tx.take() { sender.send(0)?; @@ -371,8 +373,8 @@ impl PyroscopeAgent { /// ``` pub fn stop(&mut self) { match self._stop() { - Ok(_) => log::trace!("PyroscopeAgent - Agent stopped"), - Err(_) => log::error!("PyroscopeAgent - Error stopping agent"), + Ok(_) => log::trace!(target: LOG_TAG, "Agent stopped"), + Err(_) => log::error!(target: LOG_TAG, "Error stopping agent"), } } @@ -387,7 +389,7 @@ impl PyroscopeAgent { /// agent.stop()?; /// ``` pub fn add_tags(&mut self, tags: &[(&str, &str)]) -> Result<()> { - log::debug!("PyroscopeAgent - Adding tags"); + log::debug!(target: LOG_TAG, "Adding tags"); // Check that tags are not empty if tags.is_empty() { return Ok(()); @@ -430,7 +432,7 @@ impl PyroscopeAgent { /// # } /// ``` pub fn remove_tags(&mut self, tags: &[&str]) -> Result<()> { - log::debug!("PyroscopeAgent - Removing tags"); + log::debug!(target: LOG_TAG, "Removing tags"); // Check that tags are not empty if tags.is_empty() { diff --git a/src/session.rs b/src/session.rs index 9204c8f5..458c3fb7 100644 --- a/src/session.rs +++ b/src/session.rs @@ -9,6 +9,8 @@ use crate::utils::get_time_range; use crate::utils::merge_tags_with_app_name; use crate::Result; +const LOG_TAG: &str = "Pyroscope::Session"; + /// Session Signal /// /// This enum is used to send data to the session thread. It can also kill the session thread. @@ -32,15 +34,14 @@ pub struct SessionManager { impl SessionManager { /// Create a new SessionManager pub fn new() -> Result { - log::info!("SessionManager - Creating SessionManager"); + log::info!(target: LOG_TAG, "Creating SessionManager"); // Create a channel for sending and receiving sessions let (tx, rx): (SyncSender, Receiver) = sync_channel(10); // Create a thread for the SessionManager let handle = Some(thread::spawn(move || { - log::trace!("SessionManager - SessionManager thread started"); - // This thread should only return if a kill signal is received. + log::trace!(target: LOG_TAG, "Started"); while let Ok(signal) = rx.recv() { match signal { SessionSignal::Session(session) => { @@ -54,7 +55,7 @@ impl SessionManager { } SessionSignal::Kill => { // Kill the session manager - log::trace!("SessionManager - Kill signal received"); + log::trace!(target: LOG_TAG, "Kill signal received"); return Ok(()); } } @@ -70,7 +71,7 @@ impl SessionManager { // Push the session into the SessionManager self.tx.send(session)?; - log::trace!("SessionManager - SessionSignal pushed"); + log::trace!(target: LOG_TAG, "SessionSignal pushed"); Ok(()) } @@ -97,7 +98,7 @@ impl Session { /// let session = Session::new(until, config, report)?; /// ``` pub fn new(until: u64, config: PyroscopeConfig, report: Vec) -> Result { - log::info!("Session - Creating Session"); + log::info!(target: LOG_TAG, "Creating Session"); // get_time_range should be used with "from". We balance this by reducing // 10s from the returned range. @@ -121,7 +122,7 @@ impl Session { /// session.send()?; /// ``` pub fn send(self) -> Result<()> { - log::info!("Session - Sending Session {} - {}", self.from, self.until); + log::info!(target: LOG_TAG, "Sending Session: {} - {}", self.from, self.until); // Check if the report is empty if self.report.is_empty() {