Skip to content

Commit 5b04a00

Browse files
committed
refactor(agent): remove intermediary start/stop functions
1 parent 04c2643 commit 5b04a00

File tree

8 files changed

+39
-48
lines changed

8 files changed

+39
-48
lines changed

examples/async.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ async fn main() -> Result<()> {
2323
.build()?;
2424

2525
// Start Agent
26-
agent.start();
26+
agent.start()?;
2727

2828
tokio::task::spawn(async {
2929
let n = fibonacci1(45);
@@ -40,7 +40,7 @@ async fn main() -> Result<()> {
4040
.unwrap();
4141

4242
// Stop Agent
43-
agent.stop();
43+
agent.stop()?;
4444

4545
Ok(())
4646
}

examples/backend.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ fn main() -> Result<()> {
1717
.tags(&[("TagA", "ValueA"), ("TagB", "ValueB")])
1818
.build()?;
1919

20-
agent.start();
20+
agent.start()?;
2121
let _result = fibonacci(45);
22-
agent.stop();
22+
agent.stop()?;
2323

2424
Ok(())
2525
}

examples/basic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() -> Result<()> {
2222
println!("Start Time: {}", start);
2323

2424
// Start Agent
25-
agent.start();
25+
agent.start()?;
2626

2727
let _result = fibonacci(47);
2828

@@ -34,7 +34,7 @@ fn main() -> Result<()> {
3434
println!("Stop Time: {}", stop);
3535

3636
// Stop Agent
37-
agent.stop();
37+
agent.stop()?;
3838

3939
drop(agent);
4040

examples/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ fn main() -> Result<()> {
2020
.build()
2121
.unwrap();
2222
// Start Agent
23-
agent.start();
23+
agent.start()?;
2424

2525
let _result = fibonacci(47);
2626

2727
// Stop Agent
28-
agent.stop();
28+
agent.stop()?;
2929

3030
drop(agent);
3131

examples/multi-thread.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ fn main() -> Result<()> {
2424
.build()?;
2525

2626
// Start Agent
27-
agent.start();
27+
agent.start()?;
2828

2929
let handle_1 = thread::spawn(|| {
3030
fibonacci1(45);
@@ -39,7 +39,7 @@ fn main() -> Result<()> {
3939
handle_2.join().unwrap();
4040

4141
// Stop Agent
42-
agent.stop();
42+
agent.stop()?;
4343

4444
Ok(())
4545
}

examples/tags.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn main() -> Result<()> {
1616
.build()?;
1717

1818
// Start Agent
19-
agent.start();
19+
agent.start()?;
2020

2121
// Make some calculation
2222
let _result = fibonacci(47);
@@ -28,7 +28,7 @@ fn main() -> Result<()> {
2828
let _result = fibonacci(47);
2929

3030
// Stop Agent
31-
agent.stop();
31+
agent.stop()?;
3232

3333
Ok(())
3434
}

examples/with-logger.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate pyroscope;
22

3-
use log::{debug, error, info, trace, warn};
3+
use log::info;
44

55
use pyroscope::{PyroscopeAgent, Result};
66

@@ -24,12 +24,12 @@ fn main() -> Result<()> {
2424
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "example.logger").build()?;
2525

2626
// Start Agent
27-
agent.start();
27+
agent.start()?;
2828

2929
let _result = fibonacci(47);
3030

3131
// Stop Agent
32-
agent.stop();
32+
agent.stop()?;
3333

3434
Ok(())
3535
}

src/pyroscope.rs

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ impl PyroscopeAgentBuilder {
134134
/// ?;
135135
/// ```
136136
pub fn backend<T>(self, backend: T) -> Self
137-
where T: 'static + Backend {
137+
where
138+
T: 'static + Backend,
139+
{
138140
Self {
139141
backend: Arc::new(Mutex::new(backend)),
140142
..self
@@ -237,7 +239,10 @@ impl Drop for PyroscopeAgent {
237239
// Stop the SessionManager
238240
match self.session_manager.push(SessionSignal::Kill) {
239241
Ok(_) => log::trace!(target: LOG_TAG, "Sent kill signal to SessionManager"),
240-
Err(_) => log::error!(target: LOG_TAG, "Error sending kill signal to SessionManager"),
242+
Err(_) => log::error!(
243+
target: LOG_TAG,
244+
"Error sending kill signal to SessionManager"
245+
),
241246
}
242247

243248
if let Some(handle) = self.session_manager.handle.take() {
@@ -256,7 +261,7 @@ impl Drop for PyroscopeAgent {
256261
}
257262
}
258263

259-
log::debug!(target: LOG_TAG, "Agent Dropped");
264+
log::debug!(target: LOG_TAG, "Agent Dropped");
260265
}
261266
}
262267

@@ -272,7 +277,13 @@ impl PyroscopeAgent {
272277
PyroscopeAgentBuilder::new(url, application_name)
273278
}
274279

275-
fn _start(&mut self) -> Result<()> {
280+
/// Start profiling and sending data. The agent will keep running until stopped. The agent will send data to the server every 10s secondy.
281+
/// # Example
282+
/// ```ignore
283+
/// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?;
284+
/// agent.start()?;
285+
/// ```
286+
pub fn start(&mut self) -> Result<()> {
276287
log::debug!(target: LOG_TAG, "Starting");
277288

278289
// Create a clone of Backend
@@ -328,20 +339,15 @@ impl PyroscopeAgent {
328339
Ok(())
329340
}
330341

331-
/// Start profiling and sending data. The agent will keep running until stopped. The agent will send data to the server every 10s secondy.
342+
/// Stop the agent. The agent will stop profiling and send a last report to the server.
332343
/// # Example
333344
/// ```ignore
334345
/// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?;
335-
/// agent.start();
346+
/// agent.start()?;
347+
/// // Expensive operation
348+
/// agent.stop();
336349
/// ```
337-
pub fn start(&mut self) {
338-
match self._start() {
339-
Ok(_) => log::trace!(target: LOG_TAG, "Agent started"),
340-
Err(_) => log::error!(target: LOG_TAG, "Error starting agent"),
341-
}
342-
}
343-
344-
fn _stop(&mut self) -> Result<()> {
350+
pub fn stop(&mut self) -> Result<()> {
345351
log::debug!(target: LOG_TAG, "Stopping");
346352
// get tx and send termination signal
347353
if let Some(sender) = self.tx.take() {
@@ -363,21 +369,6 @@ impl PyroscopeAgent {
363369
Ok(())
364370
}
365371

366-
/// Stop the agent. The agent will stop profiling and send a last report to the server.
367-
/// # Example
368-
/// ```ignore
369-
/// let agent = PyroscopeAgent::builder("http://localhost:8080", "my-app").build()?;
370-
/// agent.start()?;
371-
/// // Expensive operation
372-
/// agent.stop();
373-
/// ```
374-
pub fn stop(&mut self) {
375-
match self._stop() {
376-
Ok(_) => log::trace!(target: LOG_TAG, "Agent stopped"),
377-
Err(_) => log::error!(target: LOG_TAG, "Error stopping agent"),
378-
}
379-
}
380-
381372
/// Add tags. This will restart the agent.
382373
/// # Example
383374
/// ```ignore
@@ -396,7 +387,7 @@ impl PyroscopeAgent {
396387
}
397388

398389
// Stop Agent
399-
self.stop();
390+
self.stop()?;
400391

401392
// Convert &[(&str, &str)] to HashMap(String, String)
402393
let tags_hashmap: HashMap<String, String> = tags
@@ -409,7 +400,7 @@ impl PyroscopeAgent {
409400
self.config.tags.extend(tags_hashmap);
410401

411402
// Restart Agent
412-
self.start();
403+
self.start()?;
413404

414405
Ok(())
415406
}
@@ -440,7 +431,7 @@ impl PyroscopeAgent {
440431
}
441432

442433
// Stop Agent
443-
self.stop();
434+
self.stop()?;
444435

445436
// Iterate through every tag
446437
tags.iter().for_each(|key| {
@@ -449,7 +440,7 @@ impl PyroscopeAgent {
449440
});
450441

451442
// Restart Agent
452-
self.start();
443+
self.start()?;
453444

454445
Ok(())
455446
}

0 commit comments

Comments
 (0)