stats.go 4.0 KB

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