Linux下监控Rust应用性能的可落地方案
一 系统与应用指标的可观测性栈
二 深入剖析 CPU 与内核事件
三 内存与资源瓶颈的定位与优化
四 生产落地与持续观测实践
[dependencies]
pyroscope = "0.5"
pyroscope_pprofrs = "0.2"
tokio = { version = "1", features = ["full"] }
warp = "0.3"
pretty_env_logger = "0.5"
chrono = "0.4"
use pyroscope::PyroscopeAgent;
use pyroscope_pprofrs::{pprof_backend, PprofConfig};
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
std::env::set_var("RUST_LOG", "info");
pretty_env_logger::init();
let agent = PyroscopeAgent::builder("http://localhost:4040", "my-rust-app")
.backend(pprof_backend(PprofConfig::new().sample_rate(100))) // 100Hz
.tags(vec![("env", "prod"), ("version", "1.0.0")])
.build()?;
let agent_running = agent.start()?;
// TODO: 启动你的服务
run_application().await?;
let agent_ready = agent_running.stop()?;
agent_ready.shutdown();
Ok(())
}
async fn run_application() -> Result<(), Box<dyn std::error::Error>> {
Ok(())
}