Skip to content

Commit e1a13ca

Browse files
Migrate from Standard pprof to Pyroscope for Continuous Profiling (#2830)
* Add migration from standard pprof to pyroscope * Update examples/golang-push/migrating-from-standard-pprof/README.md Co-authored-by: Kim Nylander <[email protected]> * Update examples/golang-push/migrating-from-standard-pprof/README.md Co-authored-by: Kim Nylander <[email protected]> * Update examples/golang-push/migrating-from-standard-pprof/README.md Co-authored-by: Kim Nylander <[email protected]> * Update examples/golang-push/migrating-from-standard-pprof/README.md Co-authored-by: Kim Nylander <[email protected]> * Update examples/golang-push/migrating-from-standard-pprof/README.md Co-authored-by: Kim Nylander <[email protected]> * Update README.md * Update main.go * Update README.md * Update examples/golang-push/migrating-from-standard-pprof/README.md Co-authored-by: Kim Nylander <[email protected]> * Update examples/golang-push/migrating-from-standard-pprof/README.md Co-authored-by: Kim Nylander <[email protected]> * Update README.md * Update formatting * Add more benefits to README.md --------- Co-authored-by: Kim Nylander <[email protected]>
1 parent 80959ae commit e1a13ca

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Migrating from standard pprof to Pyroscope in a Go application
2+
3+
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, enhancing the process of profiling with Pyroscope's advanced capabilities. The actual changes needed to migrate from standard `pprof` to using the Pyroscope SDK is very simple (it extends the standard pprof library with extra functionality and performance improvements).
4+
5+
See link to [source PR here](https://github.com/grafana/pyroscope/pull/2830)
6+
<img width="1426" alt="image" src="https://github.com/grafana/pyroscope/assets/23323466/f094399a-4a4d-4b47-9f03-5a15b4085fab">
7+
8+
## Changes made
9+
10+
### Pre-Pyroscope setup
11+
12+
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.
13+
14+
### Post-Pyroscope migration
15+
16+
In the post-pyroscope code, to leverage the advanced features of Pyroscope, we made the following changes:
17+
18+
1. **Removed Standard pprof Import:** The `_ "net/http/pprof"` import was removed, as Pyroscope replaces its functionality.
19+
2. **Added Pyroscope SDK:** We installed the Pyroscope module using `go get github.com/grafana/pyroscope-go` and imported it in our `main.go`.
20+
3. **Configured Pyroscope:** Inside the `main()` function, we set up Pyroscope using the `pyroscope.Start()` method with the following configuration:
21+
- Application name and server address.
22+
- Logger configuration.
23+
- Tags for additional metadata.
24+
- Profile types to be captured.
25+
4. Consider using [godeltaprof](https://pkg.go.dev/github.com/grafana/pyroscope-go/godeltaprof) -- which is an optimized way to do memory profiling more efficiently
26+
27+
## Benefits of using Pyroscope
28+
29+
- **Continuous Profiling:** Pyroscope offers continuous, always-on profiling, allowing real-time performance analysis.
30+
- **Advanced support:** Support for advanced features such as trace-span profiling, tags/labels, and controlling profiling with code.
31+
- **Higher efficiency:** Less ressource consumed by sending delta instead of cumulative profiles.
32+
- **Enhanced Insights:** With Pyroscope, you gain deeper insights into your application's performance, helping to identify and resolve issues more effectively.
33+
- **Easy Integration:** Migrating to Pyroscope requires minimal changes and provides a more robust profiling solution with little overhead.
34+
- **Customizable Profiling:** Pyroscope enables more granular control over what gets profiled, offering a range of profiling types.
35+
36+
## Migration guide
37+
38+
To view the exact changes made during the migration, refer to our [pull request](https://github.com/grafana/pyroscope/pull/2830). This PR clearly illustrates the differences and necessary steps to transition from standard pprof to Pyroscope.
39+
40+
## Conclusion
41+
42+
Migrating to 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 switch from standard pprof to Pyroscope, benefiting from real-time, continuous profiling and advanced performance insights.
43+
44+

examples/golang-push/migrating-from-standard-pprof/main.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"fmt"
55
"net/http"
6+
"os"
7+
"runtime"
68
"sync"
79
"time"
810

9-
_ "net/http/pprof" // Standard way of adding pprof to your server
11+
"github.com/grafana/pyroscope-go" // replace "net/http/pprof" with Pyroscope SDK
1012
)
1113

1214
func busyWork(d time.Duration) {
@@ -47,6 +49,32 @@ func solveMystery(wg *sync.WaitGroup) {
4749
}
4850

4951
func main() {
52+
// Pyroscope configuration
53+
runtime.SetMutexProfileFraction(5)
54+
runtime.SetBlockProfileRate(5)
55+
56+
pyroscope.Start(pyroscope.Config{
57+
ApplicationName: "detective.mystery.app",
58+
ServerAddress: "https://profiles-prod-001.grafana.net", // if OSS then http://localhost:4040
59+
// Optional HTTP Basic authentication
60+
BasicAuthUser: "<User>", // 900009
61+
BasicAuthPassword: "<Password>", // glc_SAMPLEAPIKEY0000000000==
62+
Logger: pyroscope.StandardLogger,
63+
Tags: map[string]string{"hostname": os.Getenv("HOSTNAME")},
64+
ProfileTypes: []pyroscope.ProfileType{
65+
pyroscope.ProfileCPU,
66+
pyroscope.ProfileAllocObjects,
67+
pyroscope.ProfileAllocSpace,
68+
pyroscope.ProfileInuseObjects,
69+
pyroscope.ProfileInuseSpace,
70+
pyroscope.ProfileGoroutines,
71+
pyroscope.ProfileMutexCount,
72+
pyroscope.ProfileMutexDuration,
73+
pyroscope.ProfileBlockCount,
74+
pyroscope.ProfileBlockDuration,
75+
},
76+
})
77+
5078
var wg sync.WaitGroup
5179

5280
// Server for pprof

0 commit comments

Comments
 (0)