2021-02-18 11:29:14 +00:00
|
|
|
package steward
|
|
|
|
|
|
|
|
import (
|
2021-02-22 08:33:37 +00:00
|
|
|
"log"
|
|
|
|
"net"
|
2021-02-18 11:29:14 +00:00
|
|
|
"net/http"
|
2021-02-26 14:11:20 +00:00
|
|
|
"os"
|
2021-02-18 11:29:14 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
|
|
|
|
2021-02-19 15:58:16 +00:00
|
|
|
// metrics are generally used to hold the structure around metrics
|
|
|
|
// handling
|
2021-02-18 11:29:14 +00:00
|
|
|
type metrics struct {
|
2021-02-19 15:58:16 +00:00
|
|
|
// The channel to pass metrics that should be processed
|
|
|
|
metricsCh chan metricType
|
2021-02-22 08:33:37 +00:00
|
|
|
// 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
|
2021-02-22 08:33:37 +00:00
|
|
|
func newMetrics(hostAndPort string) *metrics {
|
2021-02-18 11:29:14 +00:00
|
|
|
m := metrics{
|
2021-02-24 09:58:02 +00:00
|
|
|
metricsCh: make(chan metricType),
|
|
|
|
hostAndPort: hostAndPort,
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
2021-02-19 15:58:16 +00:00
|
|
|
type Metricer interface {
|
|
|
|
Set(float64)
|
|
|
|
}
|
|
|
|
|
|
|
|
type metricType struct {
|
|
|
|
metric prometheus.Collector
|
|
|
|
value float64
|
|
|
|
}
|
|
|
|
|
2021-02-18 11:29:14 +00:00
|
|
|
func (s *server) startMetrics() {
|
|
|
|
|
2021-02-19 15:58:16 +00:00
|
|
|
// Receive and process all metrics
|
2021-02-18 11:29:14 +00:00
|
|
|
go func() {
|
|
|
|
for {
|
2021-02-19 15:58:16 +00:00
|
|
|
for f := range s.metrics.metricsCh {
|
|
|
|
// // Try to register the metric of the interface type prometheus.Collector
|
|
|
|
// prometheus.Register(f.metric)
|
|
|
|
|
|
|
|
// Check the real type of the interface type
|
|
|
|
switch ff := f.metric.(type) {
|
|
|
|
case prometheus.Gauge:
|
|
|
|
|
|
|
|
// Try to register. If it is already registered we need to check the error
|
|
|
|
// to get the previously registered collector, and update it's value. If it
|
|
|
|
// is not registered we register the new collector, and sets it's value.
|
|
|
|
err := prometheus.Register(ff)
|
|
|
|
if err != nil {
|
|
|
|
are, ok := err.(prometheus.AlreadyRegisteredError)
|
|
|
|
if ok {
|
|
|
|
// already registered, use the one we have and put it into ff
|
|
|
|
ff = are.ExistingCollector.(prometheus.Gauge)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ff.Set(f.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2021-02-22 08:33:37 +00:00
|
|
|
//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)
|
2021-02-26 14:11:20 +00:00
|
|
|
os.Exit(1)
|
2021-02-22 08:33:37 +00:00
|
|
|
}
|
|
|
|
m := http.NewServeMux()
|
|
|
|
m.Handle("/metrics", promhttp.Handler())
|
|
|
|
http.Serve(n, m)
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|