Skip to content

Commit d7d0a05

Browse files
committed
refactor(examples): update examples
1 parent 673926e commit d7d0a05

File tree

7 files changed

+112
-33
lines changed

7 files changed

+112
-33
lines changed

examples/async.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,20 @@ fn hash_rounds2(n: u64) -> u64 {
3434

3535
#[tokio::main]
3636
async fn main() -> Result<()> {
37-
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "example.async")
37+
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.async")
3838
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
3939
.tags([("TagA", "ValueA"), ("TagB", "ValueB")].to_vec())
4040
.build()?;
4141

42+
// Show start time
43+
let start = std::time::SystemTime::now()
44+
.duration_since(std::time::UNIX_EPOCH)
45+
.unwrap()
46+
.as_secs();
47+
println!("Start Time: {}", start);
48+
4249
// Start Agent
43-
agent.start()?;
50+
let agent_running = agent.start()?;
4451

4552
tokio::task::spawn(async {
4653
let n = hash_rounds1(300_000);
@@ -57,7 +64,17 @@ async fn main() -> Result<()> {
5764
.unwrap();
5865

5966
// Stop Agent
60-
agent.stop()?;
67+
let agent_ready = agent_running.stop()?;
68+
69+
// Shutdown the Agent
70+
agent_ready.shutdown();
71+
72+
// Show program exit time
73+
let exit = std::time::SystemTime::now()
74+
.duration_since(std::time::UNIX_EPOCH)
75+
.unwrap()
76+
.as_secs();
77+
println!("Exit Time: {}", exit);
6178

6279
Ok(())
6380
}

examples/basic.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn hash_rounds(n: u64) -> u64 {
1919
}
2020

2121
fn main() -> Result<()> {
22-
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "example.basic")
22+
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.basic")
2323
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
2424
.tags([("TagA", "ValueA"), ("TagB", "ValueB")].to_vec())
2525
.build()?;
@@ -32,7 +32,7 @@ fn main() -> Result<()> {
3232
println!("Start Time: {}", start);
3333

3434
// Start Agent
35-
agent.start()?;
35+
let agent_running = agent.start()?;
3636

3737
let _result = hash_rounds(300_000);
3838

@@ -44,9 +44,10 @@ fn main() -> Result<()> {
4444
println!("Stop Time: {}", stop);
4545

4646
// Stop Agent
47-
agent.stop()?;
47+
let agent_ready = agent_running.stop()?;
4848

49-
drop(agent);
49+
// Shutdown the Agent
50+
agent_ready.shutdown();
5051

5152
// Show program exit time
5253
let exit = std::time::SystemTime::now()

examples/error.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ fn main() -> Result<()> {
1717
// Initialize the logger.
1818
pretty_env_logger::init_timed();
1919

20-
let mut agent = PyroscopeAgent::builder("http://invalid_url", "example.error")
20+
let agent = PyroscopeAgent::builder("http://invalid_url", "example.error")
2121
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
2222
.build()
2323
.unwrap();
2424
// Start Agent
25-
agent.start()?;
25+
let agent_running = agent.start()?;
2626

2727
let _result = fibonacci(47);
2828

2929
// Stop Agent
30-
agent.stop()?;
30+
let agent_ready = agent_running.stop()?;
3131

32-
drop(agent);
32+
agent_ready.shutdown();
3333

3434
Ok(())
3535
}

examples/internal/backend-void-run.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,43 @@ fn main() -> Result<()> {
2121
let backend = void_backend(backend_config);
2222

2323
// Create a new agent.
24-
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "void.backend")
24+
let agent = PyroscopeAgent::builder("http://localhost:4040", "void.backend")
2525
.tags([("TagA", "ValueA"), ("TagB", "ValueB")].to_vec())
2626
.backend(backend)
2727
.build()?;
2828

29+
// Show start time
30+
let start = std::time::SystemTime::now()
31+
.duration_since(std::time::UNIX_EPOCH)
32+
.unwrap()
33+
.as_secs();
34+
println!("Start Time: {}", start);
35+
2936
// Start Agent
30-
agent.start()?;
37+
let agent_running = agent.start()?;
3138

3239
// Sleep for 1 minute
3340
std::thread::sleep(std::time::Duration::from_secs(60));
3441

42+
// Show stop time
43+
let stop = std::time::SystemTime::now()
44+
.duration_since(std::time::UNIX_EPOCH)
45+
.unwrap()
46+
.as_secs();
47+
println!("Stop Time: {}", stop);
48+
3549
// Stop Agent
36-
agent.stop()?;
50+
let agent_ready = agent_running.stop()?;
51+
52+
// Shutdown the Agent
53+
agent_ready.shutdown();
54+
55+
// Show program exit time
56+
let exit = std::time::SystemTime::now()
57+
.duration_since(std::time::UNIX_EPOCH)
58+
.unwrap()
59+
.as_secs();
60+
println!("Exit Time: {}", exit);
3761

3862
Ok(())
3963
}

examples/multi-thread.rs

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

3-
use pyroscope::{backend::Tag, PyroscopeAgent, Result};
3+
use pyroscope::{PyroscopeAgent, Result};
44
use pyroscope_pprofrs::{pprof_backend, PprofConfig};
55
use std::{
66
collections::hash_map::DefaultHasher,
@@ -65,7 +65,7 @@ fn extra_rounds2(n: u64) -> u64 {
6565
}
6666

6767
fn main() -> Result<()> {
68-
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "example.multithread")
68+
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.multithread")
6969
.tags([("Host", "Rust")].to_vec())
7070
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
7171
.build()?;
@@ -78,9 +78,9 @@ fn main() -> Result<()> {
7878
println!("Start Time: {}", start);
7979

8080
// Start Agent
81-
agent.start()?;
81+
let agent_running = agent.start()?;
8282

83-
let (add_tag, remove_tag) = agent.tag_wrapper();
83+
let (add_tag, remove_tag) = agent_running.tag_wrapper();
8484

8585
let handle_1 = thread::Builder::new()
8686
.name("thread-1".to_string())
@@ -91,7 +91,7 @@ fn main() -> Result<()> {
9191
remove_tag("extra".to_string(), "round-1".to_string()).unwrap();
9292
})?;
9393

94-
let (add_tag, remove_tag) = agent.tag_wrapper();
94+
let (add_tag, remove_tag) = agent_running.tag_wrapper();
9595

9696
let handle_2 = thread::Builder::new()
9797
.name("thread-2".to_string())
@@ -107,9 +107,10 @@ fn main() -> Result<()> {
107107
handle_2.join().unwrap();
108108

109109
// Stop Agent
110-
agent.stop()?;
110+
let agent_ready = agent_running.stop()?;
111111

112-
drop(agent);
112+
// Shutdown the Agent
113+
agent_ready.shutdown();
113114

114115
// Show program exit time
115116
let exit = std::time::SystemTime::now()

examples/tags.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,32 @@ fn hash_rounds(n: u64) -> u64 {
1919
}
2020

2121
fn main() -> Result<()> {
22-
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "example.tags")
22+
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.tags")
2323
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
2424
.tags([("Hostname", "pyroscope")].to_vec())
2525
.build()?;
2626

27-
// Start Agent
28-
agent.start()?;
29-
3027
// Show start time
3128
let start = std::time::SystemTime::now()
3229
.duration_since(std::time::UNIX_EPOCH)
3330
.unwrap()
3431
.as_secs();
3532
println!("Start Time: {}", start);
3633

34+
// Start Agent
35+
let agent_running = agent.start()?;
36+
37+
let (add_tag, remove_tag) = agent_running.tag_wrapper();
38+
3739
// Add Tags
38-
agent.add_global_tag(Tag::new("series".to_string(), "Number 1".to_string()))?;
40+
add_tag("series".to_string(), "Number 1".to_string())?;
3941

4042
// Make some calculation
4143
let _result = hash_rounds(300_000);
4244

43-
// Add Tags
44-
agent.remove_global_tag(Tag::new("series".to_string(), "Number 1".to_string()))?;
45-
agent.add_global_tag(Tag::new("series".to_string(), "Number 2".to_string()))?;
45+
// Changing Tags
46+
remove_tag("series".to_string(), "Number 1".to_string())?;
47+
add_tag("series".to_string(), "Number 2".to_string())?;
4648

4749
// Do more calculation
4850
let _result = hash_rounds(500_000);
@@ -55,7 +57,17 @@ fn main() -> Result<()> {
5557
println!("Stop Time: {}", stop);
5658

5759
// Stop Agent
58-
agent.stop()?;
60+
let agent_ready = agent_running.stop()?;
61+
62+
// Shutdown the Agent
63+
agent_ready.shutdown();
64+
65+
// Show program exit time
66+
let exit = std::time::SystemTime::now()
67+
.duration_since(std::time::UNIX_EPOCH)
68+
.unwrap()
69+
.as_secs();
70+
println!("Exit Time: {}", exit);
5971

6072
Ok(())
6173
}

examples/with-logger.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,41 @@ fn main() -> Result<()> {
3030
info!("With Logger example");
3131

3232
// Create a new agent.
33-
let mut agent = PyroscopeAgent::builder("http://localhost:4040", "example.logger")
33+
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.logger")
3434
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))
3535
.build()?;
3636

37+
// Show start time
38+
let start = std::time::SystemTime::now()
39+
.duration_since(std::time::UNIX_EPOCH)
40+
.unwrap()
41+
.as_secs();
42+
println!("Start Time: {}", start);
43+
3744
// Start Agent
38-
agent.start()?;
45+
let agent_running = agent.start()?;
3946

4047
let _result = hash_rounds(300_000);
4148

49+
// Show stop time
50+
let stop = std::time::SystemTime::now()
51+
.duration_since(std::time::UNIX_EPOCH)
52+
.unwrap()
53+
.as_secs();
54+
println!("Stop Time: {}", stop);
55+
4256
// Stop Agent
43-
agent.stop()?;
57+
let agent_ready = agent_running.stop()?;
58+
59+
// Shutdown the Agent
60+
agent_ready.shutdown();
61+
62+
// Show program exit time
63+
let exit = std::time::SystemTime::now()
64+
.duration_since(std::time::UNIX_EPOCH)
65+
.unwrap()
66+
.as_secs();
67+
println!("Exit Time: {}", exit);
4468

4569
Ok(())
4670
}

0 commit comments

Comments
 (0)