github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/examples/rust/basic/src/main.rs (about)

     1  use pyroscope::PyroscopeAgent;
     2  use pyroscope_pprofrs::{pprof_backend, PprofConfig};
     3  
     4  fn main() -> Result<(), Box<dyn std::error::Error>> {
     5      // Create Pyroscope Agent
     6      let agent = PyroscopeAgent::builder("http://localhost:4040", "rust-app")
     7          .backend(pprof_backend(PprofConfig::new().sample_rate(100)))
     8          .tags(vec![("Hostname", "pyroscope")])
     9          .build()?;
    10  
    11      // Start Agent
    12      let agent_running = agent.start()?;
    13  
    14      let (add_tag, _remove_tag) = agent_running.tag_wrapper();
    15  
    16      // Add tag
    17      add_tag("Batch".to_string(), "first".to_string())?;
    18  
    19      // Do some work for first batch.
    20      mutex_lock(2);
    21  
    22      // Change Tag
    23      add_tag("Batch".to_string(), "second".to_string())?;
    24  
    25      // Do some work for second batch.
    26      mutex_lock(5);
    27  
    28      // Change Tag
    29      add_tag("Batch".to_string(), "third".to_string())?;
    30  
    31      // Do some work for third batch.
    32      mutex_lock(12);
    33  
    34      // Stop Agent
    35      let agent_ready = agent_running.stop()?;
    36      agent_ready.shutdown();
    37  
    38      Ok(())
    39  }
    40  
    41  // Generate useless load
    42  fn mutex_lock(n: u64) {
    43      let mut _i: u64 = 0;
    44  
    45      let start_time = std::time::Instant::now();
    46      while start_time.elapsed().as_secs() < (n * 10) {
    47          _i += 1;
    48      }
    49  }