nebula/stats.go

96 lines
2.6 KiB
Go
Raw Normal View History

2019-11-19 18:00:20 +01:00
package nebula
import (
"errors"
"fmt"
"log"
"net"
"net/http"
"time"
graphite "github.com/cyberdelia/go-metrics-graphite"
mp "github.com/nbrownus/go-metrics-prometheus"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/rcrowley/go-metrics"
2021-03-26 15:46:30 +01:00
"github.com/sirupsen/logrus"
2019-11-19 18:00:20 +01:00
)
2021-03-26 15:46:30 +01:00
func startStats(l *logrus.Logger, c *Config, configTest bool) error {
2019-11-19 18:00:20 +01:00
mType := c.GetString("stats.type", "")
if mType == "" || mType == "none" {
return nil
}
interval := c.GetDuration("stats.interval", 0)
if interval == 0 {
return fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
}
switch mType {
case "graphite":
2021-03-26 15:46:30 +01:00
startGraphiteStats(l, interval, c, configTest)
2019-11-19 18:00:20 +01:00
case "prometheus":
2021-03-26 15:46:30 +01:00
startPrometheusStats(l, interval, c, configTest)
2019-11-19 18:00:20 +01:00
default:
return fmt.Errorf("stats.type was not understood: %s", mType)
}
metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval)
go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval)
return nil
}
2021-03-26 15:46:30 +01:00
func startGraphiteStats(l *logrus.Logger, i time.Duration, c *Config, configTest bool) error {
2019-11-19 18:00:20 +01:00
proto := c.GetString("stats.protocol", "tcp")
host := c.GetString("stats.host", "")
if host == "" {
return errors.New("stats.host can not be empty")
}
prefix := c.GetString("stats.prefix", "nebula")
addr, err := net.ResolveTCPAddr(proto, host)
if err != nil {
return fmt.Errorf("error while setting up graphite sink: %s", err)
}
l.Infof("Starting graphite. Interval: %s, prefix: %s, addr: %s", i, prefix, addr)
if !configTest {
go graphite.Graphite(metrics.DefaultRegistry, i, prefix, addr)
}
2019-11-19 18:00:20 +01:00
return nil
}
2021-03-26 15:46:30 +01:00
func startPrometheusStats(l *logrus.Logger, i time.Duration, c *Config, configTest bool) error {
2019-11-19 18:00:20 +01:00
namespace := c.GetString("stats.namespace", "")
subsystem := c.GetString("stats.subsystem", "")
listen := c.GetString("stats.listen", "")
if listen == "" {
2020-01-06 18:14:11 +01:00
return fmt.Errorf("stats.listen should not be empty")
2019-11-19 18:00:20 +01:00
}
path := c.GetString("stats.path", "")
if path == "" {
2020-01-06 18:14:11 +01:00
return fmt.Errorf("stats.path should not be empty")
2019-11-19 18:00:20 +01:00
}
pr := prometheus.NewRegistry()
pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
go pClient.UpdatePrometheusMetrics()
if !configTest {
go func() {
l.Infof("Prometheus stats listening on %s at %s", listen, path)
http.Handle(path, promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}))
log.Fatal(http.ListenAndServe(listen, nil))
}()
}
2019-11-19 18:00:20 +01:00
return nil
}