2021-02-18 11:29:14 +00:00
|
|
|
package steward
|
|
|
|
|
|
|
|
import (
|
2021-08-03 11:57:29 +00:00
|
|
|
"fmt"
|
2021-02-22 08:33:37 +00:00
|
|
|
"net"
|
2021-02-18 11:29:14 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-08-18 10:16:21 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
2021-02-18 11:29:14 +00:00
|
|
|
"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
|
2021-04-12 08:51:26 +00:00
|
|
|
promRegistry *prometheus.Registry
|
2021-02-22 08:33:37 +00:00
|
|
|
// host and port where prometheus metrics will be exported
|
|
|
|
hostAndPort string
|
2021-08-18 10:21:02 +00:00
|
|
|
//
|
|
|
|
promTotalProcesses prometheus.Gauge
|
2021-08-18 10:28:26 +00:00
|
|
|
//
|
|
|
|
promProcessesVec *prometheus.GaugeVec
|
2021-08-18 10:38:39 +00:00
|
|
|
//
|
|
|
|
promHelloNodes prometheus.Gauge
|
|
|
|
//
|
|
|
|
promHelloNodesNameVec *prometheus.GaugeVec
|
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-08-18 10:16:21 +00:00
|
|
|
reg := prometheus.NewRegistry()
|
|
|
|
//prometheus.Unregister(prometheus.NewGoCollector())
|
|
|
|
reg.MustRegister(collectors.NewGoCollector())
|
|
|
|
// prometheus.MustRegister(collectors.NewGoCollector())
|
2021-02-18 11:29:14 +00:00
|
|
|
m := metrics{
|
2021-08-18 10:16:21 +00:00
|
|
|
promRegistry: reg,
|
2021-04-12 08:51:26 +00:00
|
|
|
hostAndPort: hostAndPort,
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
|
2021-08-16 11:01:12 +00:00
|
|
|
// Start the http interface for Prometheus metrics.
|
2021-08-04 08:37:24 +00:00
|
|
|
func (m *metrics) start() error {
|
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)
|
2021-08-04 08:37:24 +00:00
|
|
|
n, err := net.Listen("tcp", m.hostAndPort)
|
2021-02-22 08:33:37 +00:00
|
|
|
if err != nil {
|
2021-08-03 11:57:29 +00:00
|
|
|
return fmt.Errorf("error: startMetrics: failed to open prometheus listen port: %v", err)
|
2021-02-22 08:33:37 +00:00
|
|
|
}
|
2021-08-18 10:16:21 +00:00
|
|
|
//mux := http.NewServeMux()
|
|
|
|
//mux.Handle("/metrics", promhttp.Handler())
|
2021-08-03 11:57:29 +00:00
|
|
|
|
2021-08-18 10:16:21 +00:00
|
|
|
http.Handle("/metrics", promhttp.HandlerFor(m.promRegistry, promhttp.HandlerOpts{}))
|
|
|
|
|
|
|
|
err = http.Serve(n, nil)
|
2021-08-03 11:57:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error: startMetrics: failed to start http.Serve: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-02-18 11:29:14 +00:00
|
|
|
}
|