1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2024-12-14 12:37:31 +00:00
ctrl/prometheus.go

45 lines
1,010 B
Go
Raw Normal View History

2021-02-18 11:29:14 +00:00
package steward
import (
"log"
"net"
2021-02-18 11:29:14 +00:00
"net/http"
"os"
2021-02-18 11:29:14 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// metrics are generally used to hold the structure around metrics
// handling
2021-02-18 11:29:14 +00:00
type metrics struct {
// The channel to pass metrics that should be processed
2021-04-12 08:51:26 +00:00
promRegistry *prometheus.Registry
// host and port where prometheus metrics will be exported
hostAndPort string
2021-02-18 11:29:14 +00:00
}
2021-02-24 09:58:02 +00:00
// newMetrics will prepare and return a *metrics
func newMetrics(hostAndPort string) *metrics {
2021-02-18 11:29:14 +00:00
m := metrics{
2021-04-12 08:51:26 +00:00
promRegistry: prometheus.NewRegistry(),
hostAndPort: hostAndPort,
2021-02-18 11:29:14 +00:00
}
return &m
}
func (s *server) startMetrics() {
//http.Handle("/metrics", promhttp.Handler())
//http.ListenAndServe(":2112", nil)
n, err := net.Listen("tcp", s.metrics.hostAndPort)
if err != nil {
log.Printf("error: failed to open prometheus listen port: %v\n", err)
os.Exit(1)
}
m := http.NewServeMux()
m.Handle("/metrics", promhttp.Handler())
http.Serve(n, m)
2021-02-18 11:29:14 +00:00
}