Skip to content

Commit

Permalink
Merge pull request #128 from master-hax/unix-socket
Browse files Browse the repository at this point in the history
add unix socket support for prometheus
  • Loading branch information
shizunge authored Nov 4, 2024
2 parents fb85362 + a6390a1 commit 258491b
Showing 1 changed file with 32 additions and 4 deletions.
36 changes: 32 additions & 4 deletions metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"net/http"
"os"
"time"
"strings"
"net"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -87,14 +89,40 @@ func InitPrometheus(prometheusHost, prometheusPort, prometheusEntry string) {
handler := promhttp.HandlerFor(promReg, promhttp.HandlerOpts{EnableOpenMetrics: true})
http.Handle("/"+prometheusEntry, handler)
go func() {
glog.Infof("Starting Prometheus on %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)

if strings.HasPrefix(prometheusHost, "unix:") {
socketPath := prometheusHost[5:] // trim the "unix:" prefix
glog.Infof("Starting Prometheus on Unix socket %v, entry point is /%v", socketPath, prometheusEntry)
serveOnUnixSocket(socketPath)
} else {
ipPort := prometheusHost+":"+prometheusPort
glog.Infof("Starting Prometheus on IP port %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
Expand Down

0 comments on commit 258491b

Please sign in to comment.