From aeb234fbbf478c69b66d4df4416294cb54c94542 Mon Sep 17 00:00:00 2001 From: Vivek Revankar Date: Wed, 30 Oct 2024 19:07:16 -0700 Subject: [PATCH] refactor --- metrics/metrics.go | 45 +++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/metrics/metrics.go b/metrics/metrics.go index 9df9d91..c977dfa 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -91,29 +91,38 @@ func InitPrometheus(prometheusHost, prometheusPort, prometheusEntry string) { go func() { if strings.HasPrefix(prometheusHost, "unix:") { - socketPath := prometheusHost[5:] + socketPath := prometheusHost[5:] // trim the "unix:" prefix glog.Infof("Starting Prometheus on Unix socket %v, entry point is /%v", socketPath, prometheusEntry) - _ := os.Remove(socketPath) // allow failure - unixListener, err := net.Listen("unix", socketPath) - if err != nil { - glog.Errorf("Error starting Prometheus on socket %v: %v", socketPath, err) - os.Exit(1) - } - if err := http.Serve(unixListener, nil); err != nil { - glog.Errorf("Error starting Prometheus at socket %v: %v", socketPath, err) - os.Exit(1) - } - return - } - - glog.Infof("Starting Prometheus on IP port %v:%v, entry point is /%v", prometheusHost, prometheusPort, prometheusEntry) - if err := http.ListenAndServe(prometheusHost+":"+prometheusPort, nil); err != nil { - glog.Errorf("Error starting Prometheus at port %v:%v: %v", prometheusHost, prometheusPort, err) - os.Exit(1) + serveOnUnixSocket(socketPath) + } else { + ipPort := prometheusHost+":"+prometheusPort + glog.Infof("Starting Prometheus on IP port %v:%v, entry point is /%v", ipPort, prometheusEntry) + serveOnIpPort(ipPort) } + }() } +func serveOnUnixSocket(socketPath string) { + _ = os.Remove(socketPath) // allow failure + unixListener, err := net.Listen("unix", socketPath) + if err != nil { + glog.Errorf("Error starting Prometheus on socket %v: %v", socketPath, err) + os.Exit(1) + } + if err := http.Serve(unixListener, nil); err != nil { + glog.Errorf("Error starting Prometheus at socket %v: %v", socketPath, err) + os.Exit(1) + } +} + +func serveOnIpPort(ipPort string) { + if err := http.ListenAndServe(ipPort, nil); err != nil { + glog.Errorf("Error starting Prometheus at ip port %v: %v", ipPort, err) + os.Exit(1) + } +} + const ( RecordEntryTypeStart = iota RecordEntryTypeSend = iota