Skip to content

Commit 662f78f

Browse files
authored
Merge pull request #4160 from TAM360/feat/pprof-docs
📖 Add new documentation explaining the usage of Pprof
2 parents ec15b00 + 61a83b7 commit 662f78f

File tree

4 files changed

+64
-0
lines changed

4 files changed

+64
-0
lines changed

docs/book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
- [completion](./reference/completion.md)
9696
- [Artifacts](./reference/artifacts.md)
9797
- [Platform Support](./reference/platform.md)
98+
- [Monitoring with Pprof](./reference/pprof-tutorial.md)
9899

99100
- [Manager and CRDs Scope](./reference/scopes.md)
100101

Loading
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
![pprof-result-visualization](./images/pprof-result-visualization.png)
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

docs/book/src/reference/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- [Object/DeepCopy](markers/object.md)
2828
- [RBAC](markers/rbac.md)
2929

30+
- [Monitoring with Pprof](pprof-tutorial.md)
3031
- [controller-gen CLI](controller-gen.md)
3132
- [completion](completion.md)
3233
- [Artifacts](artifacts.md)

0 commit comments

Comments
 (0)