forked from openshift/library-go
/
profiler.go
34 lines (29 loc) · 922 Bytes
/
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
package serviceability
import (
"fmt"
"net/http"
"os"
"runtime"
_ "net/http/pprof" // include the default Go profiler mux
"k8s.io/klog"
)
// StartProfiler starts the golang profiler on a port if `web` is specified. It uses the "standard" openshift env vars
func StartProfiler() {
if env("OPENSHIFT_PROFILE", "") == "web" {
go func() {
runtime.SetBlockProfileRate(1)
profilePort := env("OPENSHIFT_PROFILE_PORT", "6060")
profileHost := env("OPENSHIFT_PROFILE_HOST", "127.0.0.1")
klog.Infof(fmt.Sprintf("Starting profiling endpoint at http://%s:%s/debug/pprof/", profileHost, profilePort))
klog.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%s", profileHost, profilePort), nil))
}()
}
}
// env returns an environment variable or a default value if not specified.
func env(key string, defaultValue string) string {
val := os.Getenv(key)
if len(val) == 0 {
return defaultValue
}
return val
}