Skip to content

Commit c7f5c85

Browse files
authored
Add pprof server for steve binary (#886)
* Add pprof server for steve binary * Add link to graphviz
1 parent 5b38a4b commit c7f5c85

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,6 +452,22 @@ go run main.go
452452

453453
The API can be accessed by navigating to https://localhost:9443/v1.
454454

455+
### Running the pprof server
456+
457+
You can enable the `pprof` http server when running steve as a binary by
458+
enabling pprof with `--enable-pprof`.
459+
460+
```
461+
go run . --enable-pprof
462+
```
463+
464+
It is then possible to use `go tool pprof` to view profiles. (You might need
465+
[graphviz](https://www.graphviz.org/)) For example:
466+
467+
```
468+
go tool pprof -no_browser -http localhost:31000 http://localhost:6060/debug/pprof/goroutine
469+
```
470+
455471
Steve Features
456472
--------------
457473

main.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"net/http"
5+
_ "net/http/pprof"
46
"os"
57

68
"github.com/rancher/dynamiclistener/server"
@@ -39,5 +41,12 @@ func run(_ *cli.Context) error {
3941
if err != nil {
4042
return err
4143
}
44+
if config.PprofEnabled {
45+
go func() {
46+
if err := http.ListenAndServe(config.PprofListenAddr, nil); err != nil {
47+
logrus.Fatalf("pprof server: %v\n", err)
48+
}
49+
}()
50+
}
4251
return s.ListenAndServe(ctx, config.HTTPSListenPort, config.HTTPListenPort, &server.ListenOpts{DisplayServerLogs: true})
4352
}

pkg/server/cli/clicontext.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ type Config struct {
2121
HTTPListenPort int
2222
UIPath string
2323

24+
PprofEnabled bool
25+
PprofListenAddr string
26+
2427
WebhookConfig authcli.WebhookConfig
2528
}
2629

@@ -87,6 +90,16 @@ func Flags(config *Config) []cli.Flag {
8790
Value: 9080,
8891
Destination: &config.HTTPListenPort,
8992
},
93+
&cli.BoolFlag{
94+
Name: "enable-pprof",
95+
Value: false,
96+
Destination: &config.PprofEnabled,
97+
},
98+
&cli.StringFlag{
99+
Name: "pprof-listen-addr",
100+
Value: "localhost:6060",
101+
Destination: &config.PprofListenAddr,
102+
},
90103
}
91104

92105
return append(flags, authcli.Flags(&config.WebhookConfig)...)

0 commit comments

Comments
 (0)