2019-11-19 10:00:20 -07:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2021-03-26 12:16:35 -06:00
|
|
|
"runtime"
|
2023-05-04 13:42:45 -06:00
|
|
|
"strconv"
|
2019-11-19 10:00:20 -07:00
|
|
|
"time"
|
2020-06-30 16:53:30 -06:00
|
|
|
|
|
|
|
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 08:46:30 -06:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
2019-11-19 10:00:20 -07:00
|
|
|
)
|
|
|
|
|
2023-07-06 17:35:19 -06:00
|
|
|
type statsHandlerFunc func(listen, path string) http.Handler
|
|
|
|
|
2022-12-19 10:28:27 -07:00
|
|
|
// startStats initializes stats from config. On success, if any further work
|
2023-07-07 09:06:19 -06:00
|
|
|
// is needed to serve stats, it returns a statsHandlerFunc for that work. If
|
2023-07-06 18:07:33 -06:00
|
|
|
// no work is needed, it'll return nil. On failure, it returns nil, error.
|
2023-07-06 17:35:19 -06:00
|
|
|
func startStats(l *logrus.Logger, c *config.C, listen, buildVersion string, configTest bool) (f statsHandlerFunc, err error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
mType := c.GetString("stats.type", "")
|
|
|
|
if mType == "" || mType == "none" {
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
interval := c.GetDuration("stats.interval", 0)
|
|
|
|
if interval == 0 {
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
switch mType {
|
|
|
|
case "graphite":
|
2021-04-16 09:34:28 -06:00
|
|
|
err := startGraphiteStats(l, interval, c, configTest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
case "prometheus":
|
2023-07-06 17:35:19 -06:00
|
|
|
f, err = startPrometheusStats(l, interval, c, listen, buildVersion, configTest)
|
2021-04-16 09:34:28 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
default:
|
2021-04-16 09:34:28 -06:00
|
|
|
return nil, fmt.Errorf("stats.type was not understood: %s", mType)
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
|
|
|
|
metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
|
|
|
|
|
|
|
|
go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval)
|
|
|
|
go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval)
|
|
|
|
|
2023-07-06 17:35:19 -06:00
|
|
|
return f, nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func startGraphiteStats(l *logrus.Logger, i time.Duration, c *config.C, configTest bool) error {
|
2019-11-19 10:00:20 -07: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)
|
|
|
|
}
|
|
|
|
|
2020-04-06 12:35:32 -06:00
|
|
|
if !configTest {
|
2021-04-16 09:34:28 -06:00
|
|
|
l.Infof("Starting graphite. Interval: %s, prefix: %s, addr: %s", i, prefix, addr)
|
2020-04-06 12:35:32 -06:00
|
|
|
go graphite.Graphite(metrics.DefaultRegistry, i, prefix, addr)
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-07-06 17:35:19 -06:00
|
|
|
func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, listen, buildVersion string, configTest bool) (statsHandlerFunc, error) {
|
2019-11-19 10:00:20 -07:00
|
|
|
namespace := c.GetString("stats.namespace", "")
|
|
|
|
subsystem := c.GetString("stats.subsystem", "")
|
|
|
|
|
|
|
|
if listen == "" {
|
2023-07-06 17:43:42 -06:00
|
|
|
return nil, fmt.Errorf("http.listen or stats.listen must be defined to use Prometheus stats")
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
pr := prometheus.NewRegistry()
|
|
|
|
pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
|
2021-11-02 12:14:26 -06:00
|
|
|
if !configTest {
|
|
|
|
go pClient.UpdatePrometheusMetrics()
|
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2021-03-26 12:16:35 -06:00
|
|
|
// Export our version information as labels on a static gauge
|
|
|
|
g := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: namespace,
|
|
|
|
Subsystem: subsystem,
|
|
|
|
Name: "info",
|
|
|
|
Help: "Version information for the Nebula binary",
|
|
|
|
ConstLabels: prometheus.Labels{
|
2023-05-04 13:42:45 -06:00
|
|
|
"version": buildVersion,
|
|
|
|
"goversion": runtime.Version(),
|
|
|
|
"boringcrypto": strconv.FormatBool(boringEnabled()),
|
2021-03-26 12:16:35 -06:00
|
|
|
},
|
|
|
|
})
|
|
|
|
pr.MustRegister(g)
|
|
|
|
g.Set(1)
|
|
|
|
|
2023-07-06 18:00:30 -06:00
|
|
|
var startHandler statsHandlerFunc
|
2020-04-06 12:35:32 -06:00
|
|
|
if !configTest {
|
2023-07-06 18:00:30 -06:00
|
|
|
startHandler = func(listen, path string) http.Handler {
|
2020-04-06 12:35:32 -06:00
|
|
|
l.Infof("Prometheus stats listening on %s at %s", listen, path)
|
2023-07-06 17:35:19 -06:00
|
|
|
return promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l})
|
2021-04-16 09:34:28 -06:00
|
|
|
}
|
2020-04-06 12:35:32 -06:00
|
|
|
}
|
2019-11-19 10:00:20 -07:00
|
|
|
|
2023-07-06 18:00:30 -06:00
|
|
|
return startHandler, nil
|
2019-11-19 10:00:20 -07:00
|
|
|
}
|