Rust
In March 2023, Grafana Labs acquired Pyroscope, the company behind the eponymous open source continuous profiling project. As a result, the Pyroscope and Grafana Phlare projects will be merged under the new name Grafana Pyroscope. To learn more, read our recent blog post about the news.
How to add Rust profiling to your application
Add the pyroscope
and pyroscope_pprofrs
crates to your Cargo.toml:
cargo add pyroscope
cargo add pyroscope_pprofrs
Rust client configuration
At a minimum, you need to provide the URL of the Pyroscope Server and the name of your application. You also need to configure a profiling backend. For Rust, you can use pprof-rs.
// 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("http://localhost:4040", "myapp")
.backend(pprof_backend)
.build()?;
You can start profiling by invoking the following code:
let agent_running = agent.start().unwrap();
The agent can be stopped at any point, and it’ll send a last report to the server. The agent can be restarted at a later point.
let agent_ready = agent.stop().unwrap();
It’s recommended to shutdown the agent before exiting the application. A last request to the server might be missed if the agent is not shutdown properly.
agent_ready.shutdown();
How to add profiling labels to Rust applications
Tags can be added or removed after the agent is started. As of 0.5.0, the Pyroscope Agent supports tagging within threads. Check the labels and Multi-Thread examples for detailed usage.
After the agent is started (Running state), the tag_wrapper
function becomes
available. tag_wrapper
returns a tuple of functions to add and remove tags
to the agent across thread boundaries. This function is available as long as
the agent is running and can be called multiple times.
// 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 client configuration options
The agent accepts additional initial parameters:
- Backend: Profiling backend. For Rust, it’s pprof-rs
- Sample Rate: Sampling Frequency in Hertz. Default is 100.
- Tags: Initial tags.
// 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("http://localhost:4040", "myapp")
// Profiling backend
.backend(pprof_backend)
// Sample rate
.sample_rate(100)
// Tags
.tags(vec![("env", "dev")])
// Create the agent
.build()?;
Technical Details
- Backend: The Pyroscope Agent uses pprof-rs as a backend. As a result, the limitations for pprof-rs also applies. As of 0.5.0, the Pyroscope Agent supports tagging within threads. Check the Tags and Multi-Thread examples for usage.
- Timer: epoll (for Linux) and kqueue (for macOS) are required for a more precise timer.
- Shutdown: The Pyroscope Agent might take some time (usually less than 10 seconds) to shutdown properly and drop its threads. For a proper shutdown, it’s recommended that you run the
shutdown
function before dropping the Agent. - Relevant Links
Examples
Usage Examples
- basic: Minimal configuration example.
- tags: Example using Tags.
- async: Example using Async code with Tokio.
- multi-thread: Example using multiple threads.
- with-logger: Example with logging to stdout.
- error: Example with an invalid server address.