github.com/grafana/pyroscope@v1.18.0/examples/language-sdk-instrumentation/rust/rideshare/server/src/main.rs (about)

     1  use std::sync::Arc;
     2  
     3  use chrono::prelude::*;
     4  use pyroscope::PyroscopeAgent;
     5  use pyroscope_pprofrs::{pprof_backend, PprofConfig};
     6  use warp::Filter;
     7  
     8  // Vehicle enum
     9  #[derive(Debug, PartialEq)]
    10  enum Vehicle {
    11      Car,
    12      Bike,
    13      Scooter,
    14  }
    15  
    16  #[tokio::main]
    17  async fn main() -> Result<(), Box<dyn std::error::Error>> {
    18      std::env::set_var("RUST_LOG", "trace");
    19  
    20      pretty_env_logger::init_timed();
    21  
    22      let server_address = std::env::var("PYROSCOPE_SERVER_ADDRESS")
    23          .unwrap_or_else(|_| "http://localhost:4040".to_string());
    24      let region = std::env::var("REGION").unwrap_or_else(|_| "us-east".to_string());
    25  
    26      let app_name = std::env::var("PYROSCOPE_APPLICATION_NAME")
    27          .unwrap_or_else(|_| "rust-ride-sharing-app".to_string());
    28  
    29      let auth_user = std::env::var("PYROSCOPE_BASIC_AUTH_USER")
    30          .unwrap_or_else(|_| "".to_string());
    31  
    32      let auth_password = std::env::var("PYROSCOPE_BASIC_AUTH_PASSWORD")
    33          .unwrap_or_else(|_| "".to_string());
    34  
    35      let agent = PyroscopeAgent::builder(server_address, app_name.to_owned())
    36          .basic_auth(auth_user, auth_password)
    37          .backend(pprof_backend(PprofConfig::new().sample_rate(100)))
    38          .tags(vec![("region", &region)])
    39          .build()
    40          .unwrap();
    41  
    42      let agent_running = agent.start()?;
    43  
    44      // Root Route
    45      let root = warp::path::end().map(|| {
    46          // iterate throuh all env vars
    47          let mut vars = String::new();
    48          for (key, value) in std::env::vars() {
    49              vars = format!("{} {} \n", vars, format!("{}={}", key, value));
    50          }
    51          vars
    52      });
    53  
    54      let (add_tag, remove_tag) = agent_running.tag_wrapper();
    55      let add = Arc::new(add_tag);
    56      let remove = Arc::new(remove_tag);
    57  
    58      let bike = warp::path("bike").map(move || {
    59          add("vehicle".to_string(), "bike".to_string());
    60          order_bike(1);
    61          remove("vehicle".to_string(), "bike".to_string());
    62  
    63          "Bike ordered"
    64      });
    65  
    66      let (add_tag, remove_tag) = agent_running.tag_wrapper();
    67      let add = Arc::new(add_tag);
    68      let remove = Arc::new(remove_tag);
    69  
    70      let scooter = warp::path("scooter").map(move || {
    71          add("vehicle".to_string(), "scooter".to_string());
    72          order_scooter(2);
    73          remove("vehicle".to_string(), "scooter".to_string());
    74  
    75          "Scooter ordered"
    76      });
    77  
    78      let (add_tag, remove_tag) = agent_running.tag_wrapper();
    79      let add = Arc::new(add_tag);
    80      let remove = Arc::new(remove_tag);
    81  
    82      let car = warp::path("car").map(move || {
    83          add("vehicle".to_string(), "car".to_string());
    84          order_car(3);
    85          remove("vehicle".to_string(), "car".to_string());
    86  
    87          "Car ordered"
    88      });
    89  
    90      let routes = warp::get().and(root).or(bike).or(scooter).or(car);
    91  
    92      warp::serve(routes).run(([0, 0, 0, 0], 5000)).await;
    93  
    94      let agent_ready = agent_running.stop()?;
    95  
    96      agent_ready.shutdown();
    97  
    98      Ok(())
    99  }
   100  
   101  fn order_bike(n: u64) {
   102      find_nearest_vehicle(n, Vehicle::Bike);
   103  }
   104  
   105  fn order_scooter(n: u64) {
   106      find_nearest_vehicle(n, Vehicle::Scooter);
   107  }
   108  
   109  fn order_car(n: u64) {
   110      find_nearest_vehicle(n, Vehicle::Car);
   111  }
   112  
   113  fn find_nearest_vehicle(search_radius: u64, vehicle: Vehicle) {
   114      let mut _i: u64 = 0;
   115  
   116      let start_time = std::time::Instant::now();
   117      while start_time.elapsed().as_millis() < u128::from(search_radius * 200) {
   118          _i += 1;
   119      }
   120  
   121      if vehicle == Vehicle::Car {
   122          check_driver_availability(search_radius);
   123      }
   124  }
   125  
   126  fn check_driver_availability(search_radius: u64) {
   127      let mut _i: u64 = 0;
   128  
   129      let start_time = std::time::Instant::now();
   130      while start_time.elapsed().as_millis() < u128::from(search_radius * 200) {
   131          _i += 1;
   132      }
   133      // Every 4 minutes this will artificially create make requests in eu-north region slow
   134      // this is just for demonstration purposes to show how performance impacts show up in the
   135      // flamegraph
   136      let time_minutes = Local::now().minute();
   137      if std::env::var("REGION").unwrap_or_else(|_| "eu-north".to_owned()) == "eu-north"
   138          && (time_minutes % 4 == 0)
   139      {
   140          mutex_lock(search_radius);
   141      }
   142  }
   143  
   144  fn mutex_lock(search_radius: u64) {
   145      let mut _i: u64 = 0;
   146  
   147      let start_time = std::time::Instant::now();
   148      while start_time.elapsed().as_millis() < u128::from(search_radius * 4000) {
   149          _i += 1;
   150      }
   151  }