ceph-csi/internal/util/httpserver.go
Niels de Vos 6d00b39886 cleanup: move log functions to new internal/util/log package
Moving the log functions into its own internal/util/log package makes it
possible to split out the humongous internal/util packages in further
smaller pieces. This reduces the inter-dependencies between utility
functions and components, preventing circular dependencies which are not
allowed in Go.

Updates: #852
Signed-off-by: Niels de Vos <ndevos@redhat.com>
2021-08-26 09:34:05 +00:00

52 lines
1.3 KiB
Go

package util
import (
"net"
"net/http"
"net/http/pprof"
"net/url"
runtime_pprof "runtime/pprof"
"strconv"
"github.com/ceph/ceph-csi/internal/util/log"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// ValidateURL validates the url.
func ValidateURL(c *Config) error {
_, err := url.Parse(c.MetricsPath)
return err
}
// StartMetricsServer starts http server.
func StartMetricsServer(c *Config) {
addr := net.JoinHostPort(c.MetricsIP, strconv.Itoa(c.MetricsPort))
http.Handle(c.MetricsPath, promhttp.Handler())
err := http.ListenAndServe(addr, nil)
if err != nil {
log.FatalLogMsg("failed to listen on address %v: %s", addr, err)
}
}
func addPath(name string, handler http.Handler) {
http.Handle(name, handler)
log.DebugLogMsg("DEBUG: registered profiling handler on /debug/pprof/%s\n", name)
}
// 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))
}