-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Add new file for Pprof tutorial.
- Loading branch information
Showing
4 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# Monitoring Performance with Pprof | ||
|
||
`pprof` is a Go profiling tool integrated within the `controller-runtime` library, designed to help identify performance bottlenecks in areas such as CPU usage, memory allocation, and more. This profiling tool, from the Go package [`net/http/pprof`][pprof-go-docs], is especially useful for diagnosing performance issues by providing detailed insights into resource usage during the runtime of your application. | ||
|
||
`pprof` is integrated into the HTTP server used by the `controller-runtime` manager, allowing you to collect profiling data via HTTP endpoints. This data can then be visualized using `go tool pprof`. Pprof feature is built into the `controller-runtime` library, so there is no need to install it separately. | ||
|
||
The `controller-runtime` [Manager options][manager-options-doc] provide an easy way to enable `pprof`, allowing you to gather detailed runtime metrics for improving the performance of your controllers. | ||
|
||
For further information about the Pprof, check out the official [Github repository][github-repo]. | ||
|
||
<aside class="warning"> | ||
<h1>Pprof Not Recommended for Production</h1> | ||
|
||
While [Pprof][github-repo] is an excellent tool for profiling and debugging, it is not recommended to leave it enabled in production environments. The primary reasons are: | ||
|
||
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. | ||
2. **Overhead**: Continuous profiling can introduce performance overhead, especially under heavy load, potentially impacting production workloads. | ||
|
||
</aside> | ||
|
||
## How to use Pprof? | ||
|
||
1. **Enabling Pprof** | ||
|
||
In your `cmd/main.go` file, add the following `controller-runtime` manager field to enable the `pprof` endpoints: | ||
|
||
```golang | ||
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{ | ||
... | ||
// PprofBindAddress is the TCP address that the controller should bind to | ||
// for serving pprof. Specify the manager address and the port that should be bind. | ||
PprofBindAddress: ":8082", | ||
... | ||
}) | ||
``` | ||
|
||
2. **Test It Out** | ||
|
||
After enabling [Pprof][pprof-go-docs], 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. | ||
|
||
Then, you can apply your CRs/samples in order to monitor the performance of its controllers. | ||
|
||
3. **Exporting the data** | ||
|
||
By using `curl`, we can export the profiling statistics to a file like this: | ||
|
||
```bash | ||
# Note that we are using the bind host and port configured via the | ||
# Manager Options in the cmd/main.go | ||
curl -s "http://127.0.0.1:8082/debug/pprof/profile" > ./cpu-profile.out | ||
``` | ||
|
||
4. **Visualizing the results on Browser** | ||
|
||
```bash | ||
# Go tool will open a session on port 8080. | ||
# You can change this as per your own need. | ||
go tool pprof -http=:8080 ./cpu-profile.out | ||
``` | ||
|
||
Visualizaion results will vary depending on the deployed workload, and the Controller's behavior. | ||
However, you'll see a result on your browser similar to this one: | ||
|
||
![pprof-result-visualization](./images/pprof-result-visualization.png) | ||
|
||
[pprof-go-docs]: https://pkg.go.dev/net/http/pprof | ||
[manager-options-doc]: https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/manager | ||
[github-repo]: https://github.com/google/pprof | ||
[quick-start-run-it]: ../quick-start.md#test-it-out. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters