Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cpu profile pyroscope #317

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
320 changes: 303 additions & 17 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ shared-entity = { path = "libs/shared-entity", features = ["cloud"] }
workspace-template = { workspace = true }
realtime-entity.workspace = true

# Pyroscope for profiling
pyroscope = "0.5.7"
pyroscope_pprofrs = "0.2.7"

# profiling
#puffin = "0.16.0"
Expand Down
4 changes: 3 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ impl Application {
}

pub async fn run_until_stopped(self) -> Result<(), std::io::Error> {
self.server.await
let stopped = self.server.await;
tracing::error!("AppFlowy Cloud Shutdown: {:?}", stopped);
Ok(())
}

pub fn port(&self) -> u16 {
Expand Down
2 changes: 1 addition & 1 deletion src/biz/collab/access_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
}

#[instrument(level = "debug", skip_all, err)]
#[allow(clippy::blocks_in_if_conditions)]
#[allow(clippy::blocks_in_conditions)]
async fn check_collab_permission(
&self,
oid: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/biz/collab/storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ where
}

#[instrument(level = "trace", skip(self, params), oid = %params.oid, err)]
#[allow(clippy::blocks_in_if_conditions)]
#[allow(clippy::blocks_in_conditions)]
async fn upsert_collab_with_transaction(
&self,
workspace_id: &str,
Expand Down
2 changes: 1 addition & 1 deletion src/biz/workspace/access_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ where
}

#[instrument(level = "trace", skip_all, err)]
#[allow(clippy::blocks_in_if_conditions)]
#[allow(clippy::blocks_in_conditions)]
async fn check_workspace_permission(
&self,
workspace_id: &Uuid,
Expand Down
2 changes: 2 additions & 0 deletions src/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub struct Config {
pub redis_uri: Secret<String>,
pub s3: S3Setting,
pub casbin: CasbinSetting,
pub pyroscope_url: String,
}

#[derive(serde::Deserialize, Clone, Debug)]
Expand Down Expand Up @@ -132,6 +133,7 @@ pub fn get_configuration() -> Result<Config, anyhow::Error> {
casbin: CasbinSetting {
pool_size: get_env_var("APPFLOWY_CASBIN_POOL_SIZE", "8").parse()?,
},
pyroscope_url: get_env_var("APPFLOWY_PYROSCOPE_URL", "http://localhost:4040"),
};
Ok(config)
}
Expand Down
25 changes: 24 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use appflowy_cloud::application::{init_state, Application};
use appflowy_cloud::config::config::get_configuration;
use appflowy_cloud::config::config::{get_configuration, Config};
use appflowy_cloud::telemetry::init_subscriber;
use pyroscope::pyroscope::PyroscopeAgentRunning;
use pyroscope::PyroscopeAgent;
use pyroscope_pprofrs::{pprof_backend, PprofConfig};
use tracing::info;

#[actix_web::main]
Expand Down Expand Up @@ -50,6 +53,8 @@ async fn main() -> anyhow::Result<()> {
dotenvy::dotenv().ok();
}

let _pyro_agent_running = init_pyroscope(&conf)?;

let state = init_state(&conf)
.await
.map_err(|e| anyhow::anyhow!("Failed to initialize application state: {}", e))?;
Expand All @@ -58,3 +63,21 @@ async fn main() -> anyhow::Result<()> {

Ok(())
}

// https://grafana.com/docs/pyroscope/latest/configure-client/language-sdks/rust/
fn init_pyroscope(conf: &Config) -> anyhow::Result<PyroscopeAgent<PyroscopeAgentRunning>> {
let pyroscope_url = conf.pyroscope_url.as_str();
info!("Pyroscope URL: {}", pyroscope_url);

// Configure profiling backend
let pprof_config = PprofConfig::new().sample_rate(100);
let backend_impl = pprof_backend(pprof_config);

// Configure Pyroscope Agent
// let agent = PyroscopeAgent::builder(conf.pyroscope_url.as_str(), "appflowy-cloud")
let agent = PyroscopeAgent::builder(pyroscope_url, "appflowy-cloud")
.backend(backend_impl)
.build()?;
let pyro_agent_running = agent.start()?;
Ok(pyro_agent_running)
}
Loading