Skip to content

Commit 04c2643

Browse files
authored
Merge pull request #10 from drahnr/main
introduce LOG_TAGs, avoid repetitive prefixes
2 parents 055576f + c0a64b7 commit 04c2643

File tree

2 files changed

+45
-42
lines changed

2 files changed

+45
-42
lines changed

src/pyroscope.rs

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ use crate::{
1414
timer::Timer,
1515
};
1616

17+
const LOG_TAG: &str = "Pyroscope::Agent";
18+
1719
/// Pyroscope Agent Configuration. This is the configuration that is passed to the agent.
1820
/// # Example
1921
/// ```
@@ -58,7 +60,7 @@ impl PyroscopeConfig {
5860
/// ```ignore
5961
/// let mut config = PyroscopeConfig::new("http://localhost:8080", "my-app");
6062
/// config.set_sample_rate(10)
61-
/// .unwrap();
63+
/// ?;
6264
/// ```
6365
pub fn sample_rate(self, sample_rate: i32) -> Self {
6466
Self {
@@ -72,8 +74,7 @@ impl PyroscopeConfig {
7274
/// ```ignore
7375
/// use pyroscope::pyroscope::PyroscopeConfig;
7476
/// let config = PyroscopeConfig::new("http://localhost:8080", "my-app")
75-
/// .tags(vec![("env", "dev")])
76-
/// .unwrap();
77+
/// .tags(vec![("env", "dev")])?;
7778
/// ```
7879
pub fn tags(self, tags: &[(&str, &str)]) -> Self {
7980
// Convert &[(&str, &str)] to HashMap(String, String)
@@ -100,7 +101,7 @@ impl PyroscopeConfig {
100101
/// ```ignore
101102
/// use pyroscope::pyroscope::PyroscopeAgentBuilder;
102103
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app");
103-
/// let agent = builder.build().unwrap();
104+
/// let agent = builder.build()?;
104105
/// ```
105106
pub struct PyroscopeAgentBuilder {
106107
/// Profiler backend
@@ -130,7 +131,7 @@ impl PyroscopeAgentBuilder {
130131
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
131132
/// .backend(Pprof::default())
132133
/// .build()
133-
/// .unwrap();
134+
/// ?;
134135
/// ```
135136
pub fn backend<T>(self, backend: T) -> Self
136137
where T: 'static + Backend {
@@ -146,7 +147,7 @@ impl PyroscopeAgentBuilder {
146147
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
147148
/// .sample_rate(99)
148149
/// .build()
149-
/// .unwrap();
150+
/// ?;
150151
/// ```
151152
pub fn sample_rate(self, sample_rate: i32) -> Self {
152153
Self {
@@ -161,7 +162,7 @@ impl PyroscopeAgentBuilder {
161162
/// let builder = PyroscopeAgentBuilder::new("http://localhost:8080", "my-app")
162163
/// .tags(vec![("env", "dev")])
163164
/// .build()
164-
/// .unwrap();
165+
/// ?;
165166
/// ```
166167
pub fn tags(self, tags: &[(&str, &str)]) -> Self {
167168
Self {
@@ -175,15 +176,15 @@ impl PyroscopeAgentBuilder {
175176
// Initiliaze the backend
176177
let backend = Arc::clone(&self.backend);
177178
backend.lock()?.initialize(self.config.sample_rate)?;
178-
log::trace!("PyroscopeAgent - Backend initialized");
179+
log::trace!(target: LOG_TAG, "Backend initialized");
179180

180181
// Start Timer
181182
let timer = Timer::default().initialize()?;
182-
log::trace!("PyroscopeAgent - Timer initialized");
183+
log::trace!(target: LOG_TAG, "Timer initialized");
183184

184185
// Start the SessionManager
185186
let session_manager = SessionManager::new()?;
186-
log::trace!("PyroscopeAgent - SessionManager initialized");
187+
log::trace!(target: LOG_TAG, "SessionManager initialized");
187188

188189
// Return PyroscopeAgent
189190
Ok(PyroscopeAgent {
@@ -217,44 +218,45 @@ pub struct PyroscopeAgent {
217218
impl Drop for PyroscopeAgent {
218219
/// Properly shutdown the agent.
219220
fn drop(&mut self) {
220-
log::debug!("PyroscopeAgent - Dropping Agent");
221+
log::debug!(target: LOG_TAG, "PyroscopeAgent::drop()");
221222

222223
// Drop Timer listeners
223224
match self.timer.drop_listeners() {
224-
Ok(_) => log::trace!("PyroscopeAgent - Dropped timer listeners"),
225-
Err(_) => log::error!("PyroscopeAgent - Error Dropping timer listeners"),
225+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer listeners"),
226+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer listeners"),
226227
}
227228

228229
// Wait for the Timer thread to finish
229230
if let Some(handle) = self.timer.handle.take() {
230231
match handle.join() {
231-
Ok(_) => log::trace!("PyroscopeAgent - Dropped timer thread"),
232-
Err(_) => log::error!("PyroscopeAgent - Error Dropping timer thread"),
232+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped timer thread"),
233+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping timer thread"),
233234
}
234235
}
235236

236237
// Stop the SessionManager
237238
match self.session_manager.push(SessionSignal::Kill) {
238-
Ok(_) => log::trace!("PyroscopeAgent - Sent kill signal to SessionManager"),
239-
Err(_) => log::error!("PyroscopeAgent - Error sending kill signal to SessionManager"),
239+
Ok(_) => log::trace!(target: LOG_TAG, "Sent kill signal to SessionManager"),
240+
Err(_) => log::error!(target: LOG_TAG, "Error sending kill signal to SessionManager"),
240241
}
241242

242243
if let Some(handle) = self.session_manager.handle.take() {
243244
match handle.join() {
244-
Ok(_) => log::trace!("PyroscopeAgent - Dropped SessionManager thread"),
245-
Err(_) => log::error!("PyroscopeAgent - Error Dropping SessionManager thread"),
245+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped SessionManager thread"),
246+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping SessionManager thread"),
246247
}
247248
}
248249

249250
// Wait for main thread to finish
251+
250252
if let Some(handle) = self.handle.take() {
251253
match handle.join() {
252-
Ok(_) => log::trace!("PyroscopeAgent - Dropped main thread"),
253-
Err(_) => log::error!("PyroscopeAgent - Error Dropping main thread"),
254+
Ok(_) => log::trace!(target: LOG_TAG, "Dropped main thread"),
255+
Err(_) => log::error!(target: LOG_TAG, "Error Dropping main thread"),
254256
}
255257
}
256258

257-
log::debug!("PyroscopeAgent - Agent Dropped");
259+
log::debug!(target: LOG_TAG, "Agent Dropped");
258260
}
259261
}
260262

@@ -263,15 +265,15 @@ impl PyroscopeAgent {
263265
///
264266
/// # Example
265267
/// ```ignore
266-
/// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap();
268+
/// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?;
267269
/// ```
268270
pub fn builder<S: AsRef<str>>(url: S, application_name: S) -> PyroscopeAgentBuilder {
269271
// Build PyroscopeAgent
270272
PyroscopeAgentBuilder::new(url, application_name)
271273
}
272274

273275
fn _start(&mut self) -> Result<()> {
274-
log::debug!("PyroscopeAgent - Starting");
276+
log::debug!(target: LOG_TAG, "Starting");
275277

276278
// Create a clone of Backend
277279
let backend = Arc::clone(&self.backend);
@@ -294,10 +296,10 @@ impl PyroscopeAgent {
294296
let stx = self.session_manager.tx.clone();
295297

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

299301
while let Ok(until) = rx.recv() {
300-
log::trace!("PyroscopeAgent - Sending session {}", until);
302+
log::trace!(target: LOG_TAG, "Sending session {}", until);
301303

302304
// Generate report from backend
303305
let report = backend.lock()?.report()?;
@@ -310,7 +312,7 @@ impl PyroscopeAgent {
310312
)?))?;
311313

312314
if until == 0 {
313-
log::trace!("PyroscopeAgent - Session Killed");
315+
log::trace!(target: LOG_TAG, "Session Killed");
314316

315317
let (lock, cvar) = &*pair;
316318
let mut running = lock.lock()?;
@@ -329,18 +331,18 @@ impl PyroscopeAgent {
329331
/// Start profiling and sending data. The agent will keep running until stopped. The agent will send data to the server every 10s secondy.
330332
/// # Example
331333
/// ```ignore
332-
/// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build().unwrap();
334+
/// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?;
333335
/// agent.start();
334336
/// ```
335337
pub fn start(&mut self) {
336338
match self._start() {
337-
Ok(_) => log::trace!("PyroscopeAgent - Agent started"),
338-
Err(_) => log::error!("PyroscopeAgent - Error starting agent"),
339+
Ok(_) => log::trace!(target: LOG_TAG, "Agent started"),
340+
Err(_) => log::error!(target: LOG_TAG, "Error starting agent"),
339341
}
340342
}
341343

342344
fn _stop(&mut self) -> Result<()> {
343-
log::debug!("PyroscopeAgent - Stopping");
345+
log::debug!(target: LOG_TAG, "Stopping");
344346
// get tx and send termination signal
345347
if let Some(sender) = self.tx.take() {
346348
sender.send(0)?;
@@ -371,8 +373,8 @@ impl PyroscopeAgent {
371373
/// ```
372374
pub fn stop(&mut self) {
373375
match self._stop() {
374-
Ok(_) => log::trace!("PyroscopeAgent - Agent stopped"),
375-
Err(_) => log::error!("PyroscopeAgent - Error stopping agent"),
376+
Ok(_) => log::trace!(target: LOG_TAG, "Agent stopped"),
377+
Err(_) => log::error!(target: LOG_TAG, "Error stopping agent"),
376378
}
377379
}
378380

@@ -387,7 +389,7 @@ impl PyroscopeAgent {
387389
/// agent.stop()?;
388390
/// ```
389391
pub fn add_tags(&mut self, tags: &[(&str, &str)]) -> Result<()> {
390-
log::debug!("PyroscopeAgent - Adding tags");
392+
log::debug!(target: LOG_TAG, "Adding tags");
391393
// Check that tags are not empty
392394
if tags.is_empty() {
393395
return Ok(());
@@ -430,7 +432,7 @@ impl PyroscopeAgent {
430432
/// # }
431433
/// ```
432434
pub fn remove_tags(&mut self, tags: &[&str]) -> Result<()> {
433-
log::debug!("PyroscopeAgent - Removing tags");
435+
log::debug!(target: LOG_TAG, "Removing tags");
434436

435437
// Check that tags are not empty
436438
if tags.is_empty() {

src/session.rs

Lines changed: 8 additions & 7 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
}
@@ -97,7 +98,7 @@ impl Session {
9798
/// let session = Session::new(until, config, report)?;
9899
/// ```
99100
pub fn new(until: u64, config: PyroscopeConfig, report: Vec<u8>) -> Result<Self> {
100-
log::info!("Session - Creating Session");
101+
log::info!(target: LOG_TAG, "Creating Session");
101102

102103
// get_time_range should be used with "from". We balance this by reducing
103104
// 10s from the returned range.
@@ -121,7 +122,7 @@ impl Session {
121122
/// session.send()?;
122123
/// ```
123124
pub fn send(self) -> Result<()> {
124-
log::info!("Session - Sending Session {} - {}", self.from, self.until);
125+
log::info!(target: LOG_TAG, "Sending Session: {} - {}", self.from, self.until);
125126

126127
// Check if the report is empty
127128
if self.report.is_empty() {

0 commit comments

Comments
 (0)