2019-08-21 14:58:02 +05:30
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-03-25 16:44:46 +05:30
|
|
|
"net/http/pprof"
|
2019-08-21 14:58:02 +05:30
|
|
|
"net/url"
|
2021-03-25 16:44:46 +05:30
|
|
|
runtime_pprof "runtime/pprof"
|
2019-08-21 14:58:02 +05:30
|
|
|
"strconv"
|
|
|
|
|
2021-08-24 17:03:25 +02:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
|
|
|
|
2019-08-21 14:58:02 +05:30
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
2020-07-19 17:51:03 +05:30
|
|
|
// ValidateURL validates the url.
|
2019-08-21 14:58:02 +05:30
|
|
|
func ValidateURL(c *Config) error {
|
|
|
|
_, err := url.Parse(c.MetricsPath)
|
2021-07-22 11:15:17 +05:30
|
|
|
|
2019-08-21 14:58:02 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-19 17:51:03 +05:30
|
|
|
// StartMetricsServer starts http server.
|
2019-08-21 14:58:02 +05:30
|
|
|
func StartMetricsServer(c *Config) {
|
|
|
|
addr := net.JoinHostPort(c.MetricsIP, strconv.Itoa(c.MetricsPort))
|
|
|
|
http.Handle(c.MetricsPath, promhttp.Handler())
|
2023-06-02 14:27:12 +02:00
|
|
|
|
|
|
|
//nolint:gosec // TODO: add support for passing timeouts
|
2019-08-21 14:58:02 +05:30
|
|
|
err := http.ListenAndServe(addr, nil)
|
|
|
|
if err != nil {
|
2021-08-24 17:03:25 +02:00
|
|
|
log.FatalLogMsg("failed to listen on address %v: %s", addr, err)
|
2019-08-21 14:58:02 +05:30
|
|
|
}
|
|
|
|
}
|
2021-07-13 17:51:05 +05:30
|
|
|
|
2021-03-25 16:44:46 +05:30
|
|
|
func addPath(name string, handler http.Handler) {
|
|
|
|
http.Handle(name, handler)
|
2021-08-24 17:03:25 +02:00
|
|
|
log.DebugLogMsg("DEBUG: registered profiling handler on /debug/pprof/%s\n", name)
|
2021-03-25 16:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// 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))
|
|
|
|
}
|