|
| 1 | +use pyroscope::PyroscopeAgent; |
| 2 | +use pyroscope_pprofrs::{Pprof, PprofConfig}; |
| 3 | +use pyroscope_pyspy::{Pyspy, PyspyConfig}; |
| 4 | +use pyroscope_rbspy::{Rbspy, RbspyConfig}; |
| 5 | +use utils::app_config::AppConfig; |
| 6 | +use utils::error::Result; |
| 7 | +use utils::types::Spy; |
| 8 | + |
| 9 | +#[derive(Debug, Default)] |
| 10 | +pub struct Profiler { |
| 11 | + agent: Option<PyroscopeAgent>, |
| 12 | +} |
| 13 | + |
| 14 | +impl Profiler { |
| 15 | + pub fn init(&mut self) -> Result<()> { |
| 16 | + let pid: i32 = AppConfig::get::<i32>("pid")?; |
| 17 | + |
| 18 | + let app_name: String = get_application_name()?; |
| 19 | + |
| 20 | + let server_address: String = AppConfig::get::<String>("server_address")?; |
| 21 | + |
| 22 | + let sample_rate: u32 = AppConfig::get::<u32>("sample_rate")?; |
| 23 | + |
| 24 | + // TODO: CLI should probably unify this into a single argument |
| 25 | + let pyspy_blocking: bool = AppConfig::get::<bool>("pyspy_blocking")?; |
| 26 | + let rbspy_blocking: bool = AppConfig::get::<bool>("rbspy_blocking")?; |
| 27 | + |
| 28 | + let detect_subprocesses: bool = AppConfig::get::<bool>("detect_subprocesses")?; |
| 29 | + |
| 30 | + let mut agent = match AppConfig::get::<Spy>("spy_name")? { |
| 31 | + Spy::Rustspy => { |
| 32 | + println!("here"); |
| 33 | + let config = PprofConfig::new(100); |
| 34 | + let backend = Pprof::new(config); |
| 35 | + PyroscopeAgent::builder(server_address, app_name) |
| 36 | + .backend(backend) |
| 37 | + .build()? |
| 38 | + } |
| 39 | + Spy::Pyspy => { |
| 40 | + let config = PyspyConfig::new(pid) |
| 41 | + .sample_rate(sample_rate) |
| 42 | + .lock_process(pyspy_blocking) |
| 43 | + .with_subprocesses(detect_subprocesses); |
| 44 | + let backend = Pyspy::new(config); |
| 45 | + PyroscopeAgent::builder(server_address, app_name) |
| 46 | + .backend(backend) |
| 47 | + .build()? |
| 48 | + } |
| 49 | + Spy::Rbspy => { |
| 50 | + let config = RbspyConfig::new(pid) |
| 51 | + .sample_rate(sample_rate) |
| 52 | + .lock_process(rbspy_blocking) |
| 53 | + .with_subprocesses(detect_subprocesses); |
| 54 | + let backend = Rbspy::new(config); |
| 55 | + PyroscopeAgent::builder(server_address, app_name) |
| 56 | + .backend(backend) |
| 57 | + .build()? |
| 58 | + } |
| 59 | + _ => return Ok(()), |
| 60 | + }; |
| 61 | + |
| 62 | + agent.start()?; |
| 63 | + |
| 64 | + self.agent = Some(agent); |
| 65 | + |
| 66 | + Ok(()) |
| 67 | + } |
| 68 | + |
| 69 | + pub fn stop(self) -> Result<()> { |
| 70 | + if let Some(mut agent) = self.agent { |
| 71 | + agent.stop()?; |
| 72 | + } |
| 73 | + |
| 74 | + Ok(()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fn get_application_name() -> Result<String> { |
| 79 | + let pre_app_name: String = AppConfig::get::<String>("application_name").unwrap_or_else(|_| { |
| 80 | + names::Generator::default() |
| 81 | + .next() |
| 82 | + .unwrap_or("unassigned.app".to_string()) |
| 83 | + .replace("-", ".") |
| 84 | + }); |
| 85 | + |
| 86 | + let pre = match AppConfig::get::<Spy>("spy_name")? { |
| 87 | + Spy::Pyspy => "pyspy", |
| 88 | + Spy::Rbspy => "rbspy", |
| 89 | + _ => "none", |
| 90 | + }; |
| 91 | + |
| 92 | + // add pre to pre_app_name |
| 93 | + let app_name = format!("{}.{}", pre, pre_app_name); |
| 94 | + |
| 95 | + Ok(app_name) |
| 96 | +} |
0 commit comments