-
Notifications
You must be signed in to change notification settings - Fork 2
/
profiler.go
49 lines (40 loc) · 1.24 KB
/
profiler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package util
import (
"fmt"
"net/http"
_ "net/http/pprof"
"github.com/go-logr/logr"
"github.com/kelseyhightower/envconfig"
)
type ProfilerConfig struct {
EnableProfiler bool `envconfig:"PROFILER_ENABLE" default:"false"`
ProfilerBindAddress string `envconfig:"PROFILER_BIND_ADDRESS" default:"0.0.0.0"`
ProfilerBinbPort string `envconfig:"PROFILER_BIND_ORT" default:"6060"`
Log logr.Logger
}
func (cfg ProfilerConfig) Setup() error {
if err := envconfig.Process("profiler", &cfg); err != nil {
cfg.Log.Error(err, "unable to fetch profile env config")
return err
}
if !cfg.EnableProfiler {
cfg.Log.Info("profiler not enabled")
return nil
}
return cfg.enable()
}
func (cfg ProfilerConfig) enable() error {
cfg.Log.Info("profiler enabled")
go func() {
pprofListenerAddr := fmt.Sprintf("%s:%s", cfg.ProfilerBindAddress, cfg.ProfilerBinbPort)
cfg.Log.Info("starting pprof server", "pprofListenerAddr", pprofListenerAddr)
if err := http.ListenAndServe(pprofListenerAddr, nil); err != nil {
cfg.Log.Error(err, "unable to start the pprof server")
return
}
cfg.Log.Info("pprof server successfully started",
"pprofUrl", fmt.Sprintf("http://%s/debug/pprof/", pprofListenerAddr),
)
}()
return nil
}