Skip to content

Commit

Permalink
imp(logging): initial logging implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
omarabid committed Jan 20, 2022
1 parent 1111115 commit 47385f3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ readme = "README.md"

[dependencies]
thiserror ="1.0"
log = "0.4"
reqwest = {version = "0.11", features = ["blocking"]}
pprof = { version="0.6.2"}
libc = "^0.2.66"

[dev-dependencies]
tokio = { version = "1.13", features = ["full"] }
pretty_env_logger = "0.4.0"

[profile.dev]
opt-level=0
Expand Down
23 changes: 23 additions & 0 deletions src/pyroscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,15 @@ impl PyroscopeAgentBuilder {
// Initiliaze the backend
let backend = Arc::clone(&self.backend);
backend.lock()?.initialize(self.config.sample_rate)?;
log::trace!("PyroscopeAgent - Backend initialized");

// Start Timer
let timer = Timer::default().initialize()?;
log::trace!("PyroscopeAgent - Timer initialized");

// Start the SessionManager
let session_manager = SessionManager::new()?;
log::trace!("PyroscopeAgent - SessionManager initialized");

// Return PyroscopeAgent
Ok(PyroscopeAgent {
Expand Down Expand Up @@ -162,22 +165,29 @@ pub struct PyroscopeAgent {
impl Drop for PyroscopeAgent {
/// Properly shutdown the agent.
fn drop(&mut self) {
log::debug!("PyroscopeAgent::drop()");

// Stop Timer
self.timer.drop_listeners().unwrap(); // Drop listeners
log::trace!("PyroscopeAgent - Dropped timer listeners");
self.timer.handle.take().unwrap().join().unwrap().unwrap(); // Wait for the Timer thread to finish
log::trace!("PyroscopeAgent - Dropped timer thread");

// Stop the SessionManager
self.session_manager.push(SessionSignal::Kill).unwrap();
log::trace!("PyroscopeAgent - Sent kill signal to SessionManager");
self.session_manager
.handle
.take()
.unwrap()
.join()
.unwrap()
.unwrap();
log::trace!("PyroscopeAgent - Dropped SessionManager thread");

// Wait for main thread to finish
self.handle.take().unwrap().join().unwrap().unwrap();
log::trace!("PyroscopeAgent - Dropped main thread");
}
}

Expand All @@ -190,6 +200,8 @@ impl PyroscopeAgent {

/// Start profiling and sending data. The agent will keep running until stopped.
pub fn start(&mut self) -> Result<()> {
log::debug!("PyroscopeAgent - Starting");

// Create a clone of Backend
let backend = Arc::clone(&self.backend);
// Call start()
Expand All @@ -213,8 +225,14 @@ impl PyroscopeAgent {
let stx = self.session_manager.tx.clone();

self.handle = Some(std::thread::spawn(move || {
log::trace!("PyroscopeAgent - Main Thread started");

while let Ok(time) = rx.recv() {
log::trace!("PyroscopeAgent - Sending session {}", time);

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

// Send new Session to SessionManager
stx.send(SessionSignal::Session(Session::new(
time,
Expand All @@ -223,6 +241,8 @@ impl PyroscopeAgent {
)?))?;

if time == 0 {
log::trace!("PyroscopeAgent - Session Killed");

let (lock, cvar) = &*pair;
let mut running = lock.lock()?;
*running = false;
Expand All @@ -240,6 +260,7 @@ impl PyroscopeAgent {

/// Stop the agent.
pub fn stop(&mut self) -> Result<()> {
log::debug!("PyroscopeAgent - Stopping");
// get tx and send termination signal
self.tx.take().unwrap().send(0)?;

Expand All @@ -258,6 +279,7 @@ impl PyroscopeAgent {

/// Add tags. This will restart the agent.
pub fn add_tags(&mut self, tags: &[(&str, &str)]) -> Result<()> {
log::debug!("PyroscopeAgent - Adding tags");
// Stop Agent
self.stop()?;

Expand All @@ -279,6 +301,7 @@ impl PyroscopeAgent {

/// Remove tags. This will restart the agent.
pub fn remove_tags(&mut self, tags: &[&str]) -> Result<()> {
log::debug!("PyroscopeAgent - Removing tags");
// Stop Agent
self.stop()?;

Expand Down
12 changes: 12 additions & 0 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,25 @@ pub struct SessionManager {
impl SessionManager {
/// Create a new SessionManager
pub fn new() -> Result<Self> {
log::info!("SessionManager - Creating SessionManager");

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

// Create a thread for the SessionManager
let handle = Some(thread::spawn(move || {
log::trace!("SessionManager - SessionManager thread started");
while let Ok(signal) = rx.recv() {
match signal {
SessionSignal::Session(session) => {
// Send the session
session.send()?;
log::trace!("SessionManager - Session sent");
}
SessionSignal::Kill => {
// Kill the session manager
return Ok(());
log::trace!("SessionManager - Kill signal received");
}
}
}
Expand All @@ -56,7 +61,11 @@ impl SessionManager {

/// Push a new session into the SessionManager
pub fn push(&self, session: SessionSignal) -> Result<()> {
// Push the session into the SessionManager
self.tx.send(session)?;

log::trace!("SessionManager - SessionSignal pushed");

Ok(())
}
}
Expand All @@ -72,6 +81,7 @@ pub struct Session {

impl Session {
pub fn new(mut until: u64, config: PyroscopeConfig, report: Vec<u8>) -> Result<Self> {
log::info!("Session - Creating Session");
// Session interrupted (0 signal), determine the time
if until == 0 {
let now = std::time::SystemTime::now()
Expand All @@ -94,6 +104,8 @@ impl Session {
}

pub fn send(self) -> Result<()> {
log::info!("Session - Sending Session");

let _handle: JoinHandle<Result<()>> = thread::spawn(move || {
if self.report.is_empty() {
return Ok(());
Expand Down

0 comments on commit 47385f3

Please sign in to comment.