stats.go 3.6 KB

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