Skip to content

Commit 90b09ed

Browse files
committed
split log tags
1 parent e9b3f7c commit 90b09ed

File tree

3 files changed

+52
-47
lines changed

3 files changed

+52
-47
lines changed

src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
//! agent.stop();
2525
//! ```
2626
27-
pub(crate) static LOG_TAG: &str = "pyroscope";
28-
2927
// Re-exports structs
3028
pub use crate::pyroscope::PyroscopeAgent;
3129
pub use error::{PyroscopeError, Result};

src/pyroscope.rs

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use crate::{
1717
timer::Timer,
1818
};
1919

20+
const LOG_TAG: &str = "Pyroscope::Agent";
21+
2022
/// Pyroscope Agent Configuration. This is the configuration that is passed to the agent.
2123
/// # Example
2224
/// ```
@@ -178,15 +180,15 @@ impl PyroscopeAgentBuilder {
178180
// Initiliaze the backend
179181
let backend = Arc::clone(&self.backend);
180182
backend.lock()?.initialize(self.config.sample_rate)?;
181-
log::trace!(target: LOG_TAG, "Backend initialized");
183+
log::trace!(target: LOG_TAG, "Backend initialized");
182184

183185
// Start Timer
184186
let timer = Timer::default().initialize()?;
185-
log::trace!(target: LOG_TAG, "Timer initialized");
187+
log::trace!(target: LOG_TAG, "Timer initialized");
186188

187189
// Start the SessionManager
188190
let session_manager = SessionManager::new()?;
189-
log::trace!(target: LOG_TAG, "SessionManager initialized");
191+
log::trace!(target: LOG_TAG, "SessionManager initialized");
190192

191193
// Return PyroscopeAgent
192194
Ok(PyroscopeAgent {
@@ -220,41 +222,36 @@ pub struct PyroscopeAgent {
220222
impl Drop for PyroscopeAgent {
221223
/// Properly shutdown the agent.
222224
fn drop(&mut self) {
223-
log::debug!(target: LOG_TAG, "Dropping Agent");
225+
log::debug!(target: LOG_TAG, "PyroscopeAgent::drop()");
224226

225227
// Drop Timer listeners
226228
match self.timer.drop_listeners() {
227-
Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer listeners"),
228-
Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer listeners"),
229+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer listeners"),
230+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer listeners"),
229231
}
230232

231233
// Wait for the Timer thread to finish
232-
if let Some(handle) = self.timer.handle.take() {
233-
match handle.join() {
234-
Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer thread"),
235-
Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer thread"),
236-
}
234+
match self.timer.handle.take().unwrap().join() {
235+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer thread"),
236+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer thread"),
237237
}
238238

239239
// Stop the SessionManager
240240
match self.session_manager.push(SessionSignal::Kill) {
241-
Ok(_) => log::trace!(target: LOG_TAG, "Sent kill signal to SessionManager"),
242-
Err(_) => log::error!(target: LOG_TAG, "Error sending kill signal to SessionManager"),
241+
Ok(_) => log::trace!(target: LOG_TAG, "Sent kill signal to SessionManager"),
242+
Err(_) => log::error!(target: LOG_TAG, "Error sending kill signal to SessionManager"),
243243
}
244244

245-
if let Some(handle) = self.session_manager.handle.take() {
246-
match handle.join() {
247-
Ok(_) => log::trace!(target: LOG_TAG, "Dropped SessionManager thread"),
248-
Err(_) => log::error!(target: LOG_TAG, "Error Dropping SessionManager thread"),
249-
}
245+
// Stop SessionManager
246+
match self.session_manager.handle.take().unwrap().join() {
247+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped SessionManager thread"),
248+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping SessionManager thread"),
250249
}
251250

252251
// Wait for main thread to finish
253-
if let Some(handle) = self.handle.take() {
254-
match handle.join() {
255-
Ok(_) => log::trace!(target: LOG_TAG, "Dropped main thread"),
256-
Err(_) => log::error!(target: LOG_TAG, "Error Dropping main thread"),
257-
}
252+
match self.handle.take().unwrap().join() {
253+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped main thread"),
254+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping main thread"),
258255
}
259256

260257
log::debug!(target: LOG_TAG, "Agent Dropped");
@@ -274,7 +271,7 @@ impl PyroscopeAgent {
274271
}
275272

276273
fn _start(&mut self) -> Result<()> {
277-
log::debug!(target: LOG_TAG, "Starting");
274+
log::debug!(target: LOG_TAG, "Starting");
278275

279276
// Create a clone of Backend
280277
let backend = Arc::clone(&self.backend);
@@ -297,23 +294,23 @@ impl PyroscopeAgent {
297294
let stx = self.session_manager.tx.clone();
298295

299296
self.handle = Some(std::thread::spawn(move || {
300-
log::trace!(target: LOG_TAG, "Main Thread started");
297+
log::trace!(target: LOG_TAG, "Main Thread started");
301298

302-
while let Ok(until) = rx.recv() {
303-
log::trace!(target: LOG_TAG, "Sending session {}", until);
299+
while let Ok(time) = rx.recv() {
300+
log::trace!(target: LOG_TAG, "Sending session {}", time);
304301

305302
// Generate report from backend
306303
let report = backend.lock()?.report()?;
307304

308305
// Send new Session to SessionManager
309306
stx.send(SessionSignal::Session(Session::new(
310-
until,
307+
time,
311308
config.clone(),
312309
report,
313310
)?))?;
314311

315-
if until == 0 {
316-
log::trace!(target: LOG_TAG, "Session Killed");
312+
if time == 0 {
313+
log::trace!(target: LOG_TAG, "Session Killed");
317314

318315
let (lock, cvar) = &*pair;
319316
let mut running = lock.lock()?;
@@ -337,13 +334,13 @@ impl PyroscopeAgent {
337334
/// ```
338335
pub fn start(&mut self) {
339336
match self._start() {
340-
Ok(_) => log::trace!(target: LOG_TAG, "Agent started"),
341-
Err(_) => log::error!(target: LOG_TAG, "Error starting agent"),
337+
Ok(_) => log::trace!(target: LOG_TAG, "Agent started"),
338+
Err(_) => log::error!(target: LOG_TAG, "Error starting agent"),
342339
}
343340
}
344341

345342
fn _stop(&mut self) -> Result<()> {
346-
log::debug!(target: LOG_TAG, "Stopping");
343+
log::debug!(target: LOG_TAG, "Stopping");
347344
// get tx and send termination signal
348345
if let Some(sender) = self.tx.take() {
349346
sender.send(0)?;
@@ -374,8 +371,8 @@ impl PyroscopeAgent {
374371
/// ```
375372
pub fn stop(&mut self) {
376373
match self._stop() {
377-
Ok(_) => log::trace!(target: LOG_TAG, "Agent stopped"),
378-
Err(_) => log::error!(target: LOG_TAG, "Error stopping agent"),
374+
Ok(_) => log::trace!(target: LOG_TAG, "Agent stopped"),
375+
Err(_) => log::error!(target: LOG_TAG, "Error stopping agent"),
379376
}
380377
}
381378

@@ -390,7 +387,7 @@ impl PyroscopeAgent {
390387
/// agent.stop()?;
391388
/// ```
392389
pub fn add_tags(&mut self, tags: &[(&str, &str)]) -> Result<()> {
393-
log::debug!(target: LOG_TAG, "Adding tags");
390+
log::debug!(target: LOG_TAG, "Adding tags");
394391
// Check that tags are not empty
395392
if tags.is_empty() {
396393
return Ok(());
@@ -433,7 +430,7 @@ impl PyroscopeAgent {
433430
/// # }
434431
/// ```
435432
pub fn remove_tags(&mut self, tags: &[&str]) -> Result<()> {
436-
log::debug!(target: LOG_TAG, "Removing tags");
433+
log::debug!(target: LOG_TAG, "Removing tags");
437434

438435
// Check that tags are not empty
439436
if tags.is_empty() {

src/session.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::utils::get_time_range;
99
use crate::utils::merge_tags_with_app_name;
1010
use crate::Result;
1111

12+
const LOG_TAG: &str = "Pyroscope::Session";
13+
1214
/// Session Signal
1315
///
1416
/// 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 {
3234
impl SessionManager {
3335
/// Create a new SessionManager
3436
pub fn new() -> Result<Self> {
35-
log::info!("SessionManager - Creating SessionManager");
37+
log::info!(target: LOG_TAG, "Creating SessionManager");
3638

3739
// Create a channel for sending and receiving sessions
3840
let (tx, rx): (SyncSender<SessionSignal>, Receiver<SessionSignal>) = sync_channel(10);
3941

4042
// Create a thread for the SessionManager
4143
let handle = Some(thread::spawn(move || {
42-
log::trace!("SessionManager - SessionManager thread started");
43-
// This thread should only return if a kill signal is received.
44+
log::trace!(target: LOG_TAG, "Started");
4445
while let Ok(signal) = rx.recv() {
4546
match signal {
4647
SessionSignal::Session(session) => {
@@ -54,7 +55,7 @@ impl SessionManager {
5455
}
5556
SessionSignal::Kill => {
5657
// Kill the session manager
57-
log::trace!("SessionManager - Kill signal received");
58+
log::trace!(target: LOG_TAG, "Kill signal received");
5859
return Ok(());
5960
}
6061
}
@@ -70,7 +71,7 @@ impl SessionManager {
7071
// Push the session into the SessionManager
7172
self.tx.send(session)?;
7273

73-
log::trace!("SessionManager - SessionSignal pushed");
74+
log::trace!(target: LOG_TAG, "SessionSignal pushed");
7475

7576
Ok(())
7677
}
@@ -96,8 +97,17 @@ impl Session {
9697
/// let until = 154065120;
9798
/// let session = Session::new(until, config, report)?;
9899
/// ```
99-
pub fn new(until: u64, config: PyroscopeConfig, report: Vec<u8>) -> Result<Self> {
100-
log::info!("Session - Creating Session");
100+
pub fn new(mut until: u64, config: PyroscopeConfig, report: Vec<u8>) -> Result<Self> {
101+
log::info!(target: LOG_TAG, "Creating Session");
102+
// Session interrupted (0 signal), determine the time
103+
if until == 0 {
104+
let now = std::time::SystemTime::now()
105+
.duration_since(std::time::UNIX_EPOCH)?
106+
.as_secs();
107+
until = now
108+
.checked_add(10u64.checked_sub(now.checked_rem(10).unwrap()).unwrap())
109+
.unwrap();
110+
}
101111

102112
// get_time_range should be used with "from". We balance this by reducing
103113
// 10s from the returned range.
@@ -121,7 +131,7 @@ impl Session {
121131
/// session.send()?;
122132
/// ```
123133
pub fn send(self) -> Result<()> {
124-
log::info!("Session - Sending Session {} - {}", self.from, self.until);
134+
log::info!(target: LOG_TAG, "Sending Session: {} - {}", self.from, self.until);
125135

126136
// Check if the report is empty
127137
if self.report.is_empty() {

0 commit comments

Comments
 (0)