Rust
使用我们先进的 Rust Profiler 优化您的 Rust 应用程序。它与 Pyroscope 协作,提供实时性能剖析功能,深入了解您的 Rust 代码库的复杂性。对于希望增强性能、减少资源使用并实现高效代码执行的 Rust 开发者而言,此集成非常宝贵。
注意
请参阅支持的性能剖析类型了解 Rust 支持的性能剖析类型列表。
开始之前
要捕获和分析性能剖析数据,您需要一个托管的 Pyroscope OSS 服务器,或者一个托管的带有 Grafana Cloud Profiles 的 Pyroscope 实例(需要一个免费的 Grafana Cloud 账户)。
Pyroscope 服务器可以是用于开发的本地服务器,也可以是用于生产的远程服务器。
将 Rust 性能剖析添加到您的应用程序
将 pyroscope
和 pyroscope_pprofrs
crate 添加到您的 Cargo.toml
cargo add pyroscope
cargo add pyroscope_pprofrs
配置 Rust 客户端
您至少需要提供 Pyroscope 服务器的 URL 和您的应用程序名称。您还需要配置一个性能剖析后端。对于 Rust,您可以使用pprof-rs。
// 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("https://:4040", "myapp")
.backend(backend_impl)
.build()?;
使用安全后端的用户需要提供认证详细信息。Grafana Cloud 使用 Basic 认证。您的用户名是一个数值,可以从您在 grafana.com 上技术栈的 Pyroscope“详细信息页面”获取。在同一页面上创建一个令牌,并将其用作 Basic 认证密码。配置将类似于
fn main() -> Result<()> {
std::env::set_var("RUST_LOG", "debug");
pretty_env_logger::init_timed();
let user = std::env::var("USER").unwrap();
let password = std::env::var("PASSWORD").unwrap();
let url = std::env::var("PYROSCOPE_URL").unwrap();
let samplerate = std::env::var("SAMPLE_RATE").unwrap().to_string().parse().unwrap();
let application_name = "example.basic";
let agent = PyroscopeAgent::builder(url, application_name.to_string())
.basic_auth(user, password).backend(pprof_backend(PprofConfig::new().sample_rate(samplerate)))
.tags([("app", "Rust"), ("TagB", "ValueB")].to_vec())
.build()?;
您可以调用以下代码开始性能剖析
let agent_running = agent.start().unwrap();
代理可以在任何时候停止,它将向服务器发送最后一份报告。代理可以在稍后重新启动。
let agent_ready = agent.stop().unwrap();
建议在退出应用程序之前关闭代理。如果代理没有正确关闭,最后一次发送到服务器的请求可能会丢失。
agent_ready.shutdown();
向 Rust 应用程序添加性能剖析标签
代理启动后可以添加或移除标签。从 0.5.0 版本开始,Pyroscope Agent 支持在线程内部添加标签。请查看tags和multi-thread示例以了解详细用法。
代理启动后,tag_wrapper
函数即可使用。tag_wrapper
返回一个函数元组,用于跨线程边界向代理添加和移除标签。只要代理正在运行,此函数即可使用,并且可以多次调用。
// Start Profiling
let agent_running = agent.start().unwrap();
// Generate Tag Wrapper functions
let (add_tag, remove_tag) = agent_running.tag_wrapper();
// Profiled code (with no tags)
// Add tags to the agent
add_tag("key".to_string(), "value".to_string());
// This portion will be profiled with the specified tag.
// Remove tags from the agent
remove_tag("key".to_string(), "value".to_string());
// Stop the agent
let agent_ready = running_agent.stop();
Rust 客户端配置选项
代理接受额外的初始参数
- 后端:性能剖析后端。对于 Rust,它是pprof-rs
- 采样率:以赫兹 (Hz) 为单位的采样频率。默认值为 100。
- 标签:初始标签。
// Configure Profiling backend
let pprof_config = PprofConfig::new().sample_rate(100);
let pprof_backend = Pprof::new(pprof_config);
// Configure Pyroscope Agent
let agent =
PyroscopeAgent::builder("https://:4040", "myapp")
// Profiling backend
.backend(pprof_backend)
// Sample rate
.sample_rate(100)
// Tags
.tags(vec![("env", "dev")])
// Create the agent
.build()?;
技术细节
后端:Pyroscope Agent 使用pprof-rs作为后端。因此,pprof-rs 的限制也适用。从 0.5.0 版本开始,Pyroscope Agent 支持在线程内部添加标签。请查看tags和multi-thread示例以了解用法。
定时器:需要 epoll(适用于 Linux)和 kqueue(适用于 macOS)以实现更精确的定时器。
关闭:Pyroscope Agent 可能需要一些时间(通常少于 10 秒)才能正常关闭并停止其线程。为了正确关闭,建议在释放代理之前运行
shutdown
函数。相关链接
示例
用法示例
- basic:最小配置示例。
- tags:使用标签的示例。
- async:使用异步代码和 Tokio 的示例。
- multi-thread:使用多线程的示例。
- with-logger:带有日志输出到 stdout 的示例。
- error:带有无效服务器地址的示例。