菜单
开源

Rust

使用我们先进的 Rust Profiler 优化您的 Rust 应用程序。它与 Pyroscope 协作,提供实时的性能分析功能,深入了解您的 Rust 代码库的复杂性。对于希望提高性能、减少资源使用并实现 Rust 应用程序高效代码执行的开发人员来说,这种集成非常宝贵。

注意

有关 Rust 支持的性能分析类型列表,请参阅 可用的性能分析类型

开始之前

要捕获和分析性能分析数据,您需要托管的 Pyroscope OSS 服务器或托管的 带有 Grafana Cloud Profiles 的 Pyroscope 实例(需要免费的 Grafana Cloud 帐户)。

Pyroscope 服务器可以是用于开发的本地服务器,也可以是用于生产的远程服务器。

将 Rust 性能分析添加到您的应用程序

pyroscopepyroscope_pprofrs crates 添加到您的 Cargo.toml

bash
cargo add pyroscope
cargo add pyroscope_pprofrs

配置 Rust 客户端

至少,您需要提供 Pyroscope 服务器的 URL 和您的应用程序的名称。您还需要配置性能分析后端。对于 Rust,您可以使用 pprof-rs

rust
// 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://127.0.0.1:4040", "myapp")
    .backend(backend_impl)
    .build()?;

安全后端的用户需要提供身份验证详细信息。Grafana Cloud 使用基本身份验证。您的用户名是一个数值,您可以从 grafana.com 上您的堆栈的 Pyroscope 的“详细信息页面”获取。在此同一页面上,创建一个令牌并将其用作基本身份验证密码。然后配置将类似于

rust
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()?;

您可以通过调用以下代码开始性能分析

rust
let agent_running = agent.start().unwrap();

代理可以在任何时候停止,它将向服务器发送最后一份报告。代理可以在稍后重新启动。

rust
let agent_ready = agent.stop().unwrap();

建议在退出应用程序之前关闭代理。如果代理未正确关闭,则可能会错过向服务器发送的最后请求。

rust
agent_ready.shutdown();

将性能分析标签添加到 Rust 应用程序

标签可以在代理启动后添加或删除。从 0.5.0 版本开始,Pyroscope Agent 支持线程内标记。查看 tagsmulti-thread 示例以获取详细用法。

代理启动后,tag_wrapper 函数变为可用。tag_wrapper 返回一个函数元组,用于跨线程边界向代理添加和删除标签。只要代理正在运行,此函数就可用,并且可以多次调用。

rust
// 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
  • 采样率:采样频率,单位为赫兹。默认为 100。
  • 标签:初始标签。
rust
// 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://127.0.0.1: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 支持线程内标记。查看 tagsmulti-thread 示例以获取用法。

  • 定时器:epoll (对于 Linux) 和 kqueue (对于 macOS) 是更精确的定时器所必需的。

  • 关闭:Pyroscope Agent 可能需要一些时间(通常少于 10 秒)才能正确关闭并释放其线程。为了正确关闭,建议您在释放代理之前运行 shutdown 函数。

  • 相关链接

示例

使用示例

  • basic:最小配置示例。
  • tags:使用标签的示例。
  • async:使用带有 Tokio 的 Async 代码的示例。
  • multi-thread:使用多线程的示例。
  • with-logger:带有 stdout 日志记录的示例。
  • error:带有无效服务器地址的示例。

独立示例

  • basic:使用 Pyroscope 库的简单 Rust 应用程序。
  • rideshare:在 Docker 上运行的多实例 Web 服务。