stats.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 an http.Handler for that work. If no
  21. // 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 promtheus stats")
  73. }
  74. path := c.GetString("stats.path", "")
  75. if path == "" {
  76. return nil, fmt.Errorf("stats.path should not be empty")
  77. }
  78. pr := prometheus.NewRegistry()
  79. pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
  80. if !configTest {
  81. go pClient.UpdatePrometheusMetrics()
  82. }
  83. // Export our version information as labels on a static gauge
  84. g := prometheus.NewGauge(prometheus.GaugeOpts{
  85. Namespace: namespace,
  86. Subsystem: subsystem,
  87. Name: "info",
  88. Help: "Version information for the Nebula binary",
  89. ConstLabels: prometheus.Labels{
  90. "version": buildVersion,
  91. "goversion": runtime.Version(),
  92. "boringcrypto": strconv.FormatBool(boringEnabled()),
  93. },
  94. })
  95. pr.MustRegister(g)
  96. g.Set(1)
  97. var f statsHandlerFunc
  98. if !configTest {
  99. f = func(listen, path string) http.Handler {
  100. l.Infof("Prometheus stats listening on %s at %s", listen, path)
  101. return promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l})
  102. }
  103. }
  104. return f, nil
  105. }