stats.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package nebula
  2. import (
  3. "errors"
  4. "fmt"
  5. "net"
  6. "net/http"
  7. "runtime"
  8. "strconv"
  9. "time"
  10. graphite "github.com/cyberdelia/go-metrics-graphite"
  11. mp "github.com/nbrownus/go-metrics-prometheus"
  12. "github.com/prometheus/client_golang/prometheus"
  13. "github.com/prometheus/client_golang/prometheus/promhttp"
  14. "github.com/rcrowley/go-metrics"
  15. "github.com/sirupsen/logrus"
  16. "github.com/slackhq/nebula/config"
  17. )
  18. type statsHandlerFunc func(listen, path string) http.Handler
  19. // startStats initializes stats from config. On success, if any further work
  20. // is needed to serve stats, it returns a statsHandlerFunc for that work. If
  21. // no work is needed, it'll return nil. On failure, it returns nil, error.
  22. func startStats(l *logrus.Logger, c *config.C, listen, buildVersion string, configTest bool) (f statsHandlerFunc, err error) {
  23. mType := c.GetString("stats.type", "")
  24. if mType == "" || mType == "none" {
  25. return nil, nil
  26. }
  27. interval := c.GetDuration("stats.interval", 0)
  28. if interval == 0 {
  29. return nil, fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
  30. }
  31. switch mType {
  32. case "graphite":
  33. err := startGraphiteStats(l, interval, c, configTest)
  34. if err != nil {
  35. return nil, err
  36. }
  37. case "prometheus":
  38. f, err = startPrometheusStats(l, interval, c, listen, buildVersion, configTest)
  39. if err != nil {
  40. return nil, err
  41. }
  42. default:
  43. return nil, fmt.Errorf("stats.type was not understood: %s", mType)
  44. }
  45. metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
  46. metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
  47. go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval)
  48. go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval)
  49. return f, nil
  50. }
  51. func startGraphiteStats(l *logrus.Logger, i time.Duration, c *config.C, configTest bool) error {
  52. proto := c.GetString("stats.protocol", "tcp")
  53. host := c.GetString("stats.host", "")
  54. if host == "" {
  55. return errors.New("stats.host can not be empty")
  56. }
  57. prefix := c.GetString("stats.prefix", "nebula")
  58. addr, err := net.ResolveTCPAddr(proto, host)
  59. if err != nil {
  60. return fmt.Errorf("error while setting up graphite sink: %s", err)
  61. }
  62. if !configTest {
  63. l.Infof("Starting graphite. Interval: %s, prefix: %s, addr: %s", i, prefix, addr)
  64. go graphite.Graphite(metrics.DefaultRegistry, i, prefix, addr)
  65. }
  66. return nil
  67. }
  68. func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, listen, buildVersion string, configTest bool) (statsHandlerFunc, error) {
  69. namespace := c.GetString("stats.namespace", "")
  70. subsystem := c.GetString("stats.subsystem", "")
  71. if listen == "" {
  72. return nil, fmt.Errorf("http.listen or stats.listen must be defined to use Prometheus stats")
  73. }
  74. pr := prometheus.NewRegistry()
  75. pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
  76. if !configTest {
  77. go pClient.UpdatePrometheusMetrics()
  78. }
  79. // Export our version information as labels on a static gauge
  80. g := prometheus.NewGauge(prometheus.GaugeOpts{
  81. Namespace: namespace,
  82. Subsystem: subsystem,
  83. Name: "info",
  84. Help: "Version information for the Nebula binary",
  85. ConstLabels: prometheus.Labels{
  86. "version": buildVersion,
  87. "goversion": runtime.Version(),
  88. "boringcrypto": strconv.FormatBool(boringEnabled()),
  89. },
  90. })
  91. pr.MustRegister(g)
  92. g.Set(1)
  93. var startHandler statsHandlerFunc
  94. if !configTest {
  95. startHandler = func(listen, path string) http.Handler {
  96. l.Infof("Prometheus stats listening on %s at %s", listen, path)
  97. return promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l})
  98. }
  99. }
  100. return startHandler, nil
  101. }