2019-08-21 09:28:02 +00:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-03-25 11:14:46 +00:00
|
|
|
"net/http/pprof"
|
2019-08-21 09:28:02 +00:00
|
|
|
"net/url"
|
2021-03-25 11:14:46 +00:00
|
|
|
runtime_pprof "runtime/pprof"
|
2019-08-21 09:28:02 +00:00
|
|
|
"strconv"
|
|
|
|
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
|
|
|
|
2019-08-21 09:28:02 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// ValidateURL validates the url.
|
2019-08-21 09:28:02 +00:00
|
|
|
func ValidateURL(c *Config) error {
|
|
|
|
_, err := url.Parse(c.MetricsPath)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-08-21 09:28:02 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-19 12:21:03 +00:00
|
|
|
// StartMetricsServer starts http server.
|
2019-08-21 09:28:02 +00:00
|
|
|
func StartMetricsServer(c *Config) {
|
|
|
|
addr := net.JoinHostPort(c.MetricsIP, strconv.Itoa(c.MetricsPort))
|
|
|
|
http.Handle(c.MetricsPath, promhttp.Handler())
|
2023-06-02 12:27:12 +00:00
|
|
|
|
|
|
|
//nolint:gosec // TODO: add support for passing timeouts
|
2019-08-21 09:28:02 +00:00
|
|
|
err := http.ListenAndServe(addr, nil)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.FatalLogMsg("failed to listen on address %v: %s", addr, err)
|
2019-08-21 09:28:02 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-13 12:21:05 +00:00
|
|
|
|
2021-03-25 11:14:46 +00:00
|
|
|
func addPath(name string, handler http.Handler) {
|
|
|
|
http.Handle(name, handler)
|
2021-08-24 15:03:25 +00:00
|
|
|
log.DebugLogMsg("DEBUG: registered profiling handler on /debug/pprof/%s\n", name)
|
2021-03-25 11:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EnableProfiling enables golang profiling.
|
|
|
|
func EnableProfiling() {
|
|
|
|
for _, profile := range runtime_pprof.Profiles() {
|
|
|
|
name := profile.Name()
|
|
|
|
handler := pprof.Handler(name)
|
|
|
|
addPath(name, handler)
|
|
|
|
}
|
|
|
|
|
|
|
|
// static profiles as listed in net/http/pprof/pprof.go:init()
|
|
|
|
addPath("cmdline", http.HandlerFunc(pprof.Cmdline))
|
|
|
|
addPath("profile", http.HandlerFunc(pprof.Profile))
|
|
|
|
addPath("symbol", http.HandlerFunc(pprof.Symbol))
|
|
|
|
addPath("trace", http.HandlerFunc(pprof.Trace))
|
|
|
|
}
|