|
| 1 | +# Monitoring Performance with Pprof |
| 2 | + |
| 3 | +[Pprof][github], a Go profiling tool, helps identify performance bottlenecks in areas like CPU and memory usage. It's integrated with the controller-runtime library's HTTP server, enabling profiling via HTTP endpoints. You can visualize the data using go tool pprof. Since [Pprof][github] is built into controller-runtime, no separate installation is needed. [Manager options][manager-options-doc] make it easy to enable pprof and gather runtime metrics to optimize controller performance. |
| 4 | + |
| 5 | +<aside class="note warning"> |
| 6 | +<h1>Not Recommended for Production</h1> |
| 7 | + |
| 8 | +While [Pprof][github] is an excellent tool for profiling and debugging, it is not recommended to leave it enabled in production environments. The primary reasons are: |
| 9 | + |
| 10 | +1. **Security Risk**: The profiling endpoints expose detailed information about your application's performance and resource usage, which could be exploited if accessed by unauthorized users. |
| 11 | +2. **Overhead**: Running profiling can introduce performance overhead, mainly CPU usage, especially under heavy load, potentially impacting production workloads. |
| 12 | + |
| 13 | +</aside> |
| 14 | + |
| 15 | +## How to use Pprof? |
| 16 | + |
| 17 | +1. **Enabling Pprof** |
| 18 | + |
| 19 | + In your `cmd/main.go` file, add the field: |
| 20 | + |
| 21 | + ```golang |
| 22 | + mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ |
| 23 | + ... |
| 24 | + // PprofBindAddress is the TCP address that the controller should bind to |
| 25 | + // for serving pprof. Specify the manager address and the port that should be bind. |
| 26 | + PprofBindAddress: ":8082", |
| 27 | + ... |
| 28 | + }) |
| 29 | + ``` |
| 30 | + |
| 31 | +2. **Test It Out** |
| 32 | + |
| 33 | + After enabling [Pprof][github], you need to build and deploy your controller to test it out. Follow the steps in the [Quick Start guide][quick-start-run-it] to run your project locally or on a cluster. |
| 34 | + |
| 35 | + Then, you can apply your CRs/samples in order to monitor the performance of its controllers. |
| 36 | + |
| 37 | +3. **Exporting the data** |
| 38 | + |
| 39 | + Using `curl`, export the profiling statistics to a file like this: |
| 40 | + |
| 41 | + ```bash |
| 42 | + # Note that we are using the bind host and port configured via the |
| 43 | + # Manager Options in the cmd/main.go |
| 44 | + curl -s "http://127.0.0.1:8082/debug/pprof/profile" > ./cpu-profile.out |
| 45 | + ``` |
| 46 | + |
| 47 | +4. **Visualizing the results on Browser** |
| 48 | + |
| 49 | + ```bash |
| 50 | + # Go tool will open a session on port 8080. |
| 51 | + # You can change this as per your own need. |
| 52 | + go tool pprof -http=:8080 ./cpu-profile.out |
| 53 | + ``` |
| 54 | + |
| 55 | + Visualizaion results will vary depending on the deployed workload, and the Controller's behavior. |
| 56 | + However, you'll see the result on your browser similar to this one: |
| 57 | + |
| 58 | +  |
| 59 | + |
| 60 | +[manager-options-doc]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/manager |
| 61 | +[quick-start-run-it]: ../quick-start.md#test-it-out |
| 62 | +[github]: https://github.com/google/pprof |
0 commit comments