Skip to content

Commit

Permalink
refactor(cli): upgrade CLI to 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
omarabid committed Apr 28, 2022
1 parent 6a972f5 commit 24518c5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 30 deletions.
83 changes: 64 additions & 19 deletions pyroscope_cli/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pyroscope_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ slog-term = "2.8.0"
slog-scope = "4.4.0"
slog-async = "2.7.0"
slog-stdlog = "4.1.0"
pyroscope = { version = "0.4", path = "../" }
pyroscope_pprofrs = {version = "0.1", path = "../pyroscope_backends/pyroscope_pprofrs" }
pyroscope_rbspy = {version = "0.1", path = "../pyroscope_backends/pyroscope_rbspy" }
pyroscope_pyspy = {version = "0.1", path = "../pyroscope_backends/pyroscope_pyspy" }
pyroscope = { version = "0.5", path = "../" }
pyroscope_pprofrs = {version = "0.2", path = "../pyroscope_backends/pyroscope_pprofrs" }
pyroscope_rbspy = {version = "0.2", path = "../pyroscope_backends/pyroscope_rbspy" }
pyroscope_pyspy = {version = "0.2", path = "../pyroscope_backends/pyroscope_pyspy" }

[dependencies.clap]
version = "=3.1.6"
Expand Down
18 changes: 11 additions & 7 deletions pyroscope_cli/src/core/profiler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use pyroscope::PyroscopeAgent;
use pyroscope::{
pyroscope::{PyroscopeAgentReady, PyroscopeAgentRunning, PyroscopeAgentState},
PyroscopeAgent,
};
use pyroscope_pyspy::{pyspy_backend, PyspyConfig};
use pyroscope_rbspy::{rbspy_backend, RbspyConfig};

Expand All @@ -11,7 +14,7 @@ use crate::utils::{
/// Wrapper for the `pyroscope` library and the `pyroscope_pyspy` and `pyroscope_rbspy` backends.
#[derive(Debug, Default)]
pub struct Profiler {
agent: Option<PyroscopeAgent>,
agent: Option<PyroscopeAgent<PyroscopeAgentRunning>>,
}

impl Profiler {
Expand All @@ -36,7 +39,7 @@ impl Profiler {
let tag_str = &AppConfig::get::<String>("tag")?;
let tags = tags_to_array(tag_str)?;

let mut agent = match AppConfig::get::<Spy>("spy_name")? {
let agent = match AppConfig::get::<Spy>("spy_name")? {
Spy::Pyspy => {
let config = PyspyConfig::new(pid)
.sample_rate(sample_rate)
Expand Down Expand Up @@ -64,17 +67,18 @@ impl Profiler {
}
};

agent.start()?;
let agent_running = agent.start()?;

self.agent = Some(agent);
self.agent = Some(agent_running);

Ok(())
}

/// Stops the `pyroscope` library agent and the backend.
pub fn stop(self) -> Result<()> {
if let Some(mut agent) = self.agent {
agent.stop()?;
if let Some(agent_running) = self.agent {
let agent_ready = agent_running.stop()?;
agent_ready.shutdown();
}

Ok(())
Expand Down
7 changes: 7 additions & 0 deletions src/pyroscope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,19 @@ impl PyroscopeAgentBuilder {

/// This trait is used to encode the state of the agent.
pub trait PyroscopeAgentState {}

/// Marker struct for an Uninitialized state.
#[derive(Debug)]
pub struct PyroscopeAgentBare;

/// Marker struct for a Ready state.
#[derive(Debug)]
pub struct PyroscopeAgentReady;

/// Marker struct for a Running state.
#[derive(Debug)]
pub struct PyroscopeAgentRunning;

impl PyroscopeAgentState for PyroscopeAgentBare {}
impl PyroscopeAgentState for PyroscopeAgentReady {}
impl PyroscopeAgentState for PyroscopeAgentRunning {}
Expand Down

0 comments on commit 24518c5

Please sign in to comment.