This README provides a comprehensive guide on migrating from the standard pprof
library to Pyroscope in a Go application. The example demonstrates the transition within a detective-themed Go application, showcasing how to enhance the profiling process with Pyroscope's advanced capabilities. The actual changes needed to migrate from the standard pprof
to using the Pyroscope SDK are very simple. See the source PR.
The Pyroscope Go SDK extends the standard pprof
library with extra functionality and performance improvements. If you would like to use the standard pprof
alongside the Pyroscope Go SDK simultaneously, see the example here.
![image](https://private-user-images.githubusercontent.com/23323466/289687191-f094399a-4a4d-4b47-9f03-5a15b4085fab.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzkzNDE4MjcsIm5iZiI6MTczOTM0MTUyNywicGF0aCI6Ii8yMzMyMzQ2Ni8yODk2ODcxOTEtZjA5NDM5OWEtNGE0ZC00YjQ3LTlmMDMtNWExNWI0MDg1ZmFiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNTAyMTIlMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjUwMjEyVDA2MjUyN1omWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTFjNzEyYTUyMmI2YTZlMzg0NDZkOGY5YWI4NmU3NmQyOWI1NGIwODQxMWMxZjgzMGFiZGEwMTAwZTUyMTM0YjcmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0In0.LH5ojONzNDGRvp4YfBgQXMxrT17hH-LKiB46b15pakk)
Originally in the pre-pyroscope code, the main.go
file used the standard net/http/pprof
package for profiling. This setup is common and straightforward but lacks continuous profiling and real-time analysis capabilities.
In the post-pyroscope code, to leverage the advanced features of Pyroscope, we made the following changes:
-
Removed Standard pprof Import: The
_ "net/http/pprof"
import was removed, as Pyroscope replaces its functionality. -
Added Pyroscope SDK: We installed the Pyroscope module using the following command and imported it in our
main.go
:
go get github.com/grafana/pyroscope-go
- Enabled block and mutex profilers: This step is only required if you're using mutex or block profiling. Inside the
main()
function, we enabled the block and mutex profilers using the runtime functions:
runtime.SetMutexProfileFraction(5)
runtime.SetBlockProfileRate(5)
-
Configured Pyroscope: Inside the
main()
function, we set up Pyroscope using thepyroscope.Start()
method with the following configuration:- Application name and server address.
- Logger configuration.
- Tags for additional metadata.
- Profile types to be captured.
-
Consider using godeltaprof: It provides an optimized way to perform memory, mutex, and block profiling more efficiently.
- Continuous Profiling: Pyroscope offers continuous, always-on profiling, allowing real-time performance analysis.
- Advanced support: Support for advanced features such as trace-span profiling, tags/labels, and controlling profiling with code.
- Higher efficiency: Less ressource consumed by sending delta instead of cumulative profiles.
- Enhanced Insights: With Pyroscope, you gain deeper insights into your application's performance, helping to identify and resolve issues more effectively.
- Easy Integration: Migrating to Pyroscope requires minimal changes and provides a more robust profiling solution with little overhead.
- Customizable Profiling: Pyroscope enables more granular control over what gets profiled, offering a range of profiling types.
To view the exact changes made during the migration, refer to our pull request. This PR clearly illustrates the differences and the necessary steps to transition from the standard pprof
library to Pyroscope.
Migrating to the Pyroscope SDK in a Go application is a straightforward process that significantly enhances profiling capabilities. By following the steps outlined in this guide and reviewing the provided PR, developers can easily transition from the standard pprof
library to Pyroscope, benefiting from real-time, continuous profiling and advanced performance insights.