stats.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package nebula
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "net"
  7. "net/http"
  8. "runtime"
  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. // startStats initializes stats from config. On success, if any futher work
  19. // is needed to serve stats, it returns a func to handle that work. If no
  20. // work is needed, it'll return nil. On failure, it returns nil, error.
  21. func startStats(l *logrus.Logger, c *config.C, buildVersion string, configTest bool) (func(), error) {
  22. mType := c.GetString("stats.type", "")
  23. if mType == "" || mType == "none" {
  24. return nil, nil
  25. }
  26. interval := c.GetDuration("stats.interval", 0)
  27. if interval == 0 {
  28. return nil, fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
  29. }
  30. var startFn func()
  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. var err error
  39. startFn, err = startPrometheusStats(l, interval, c, buildVersion, configTest)
  40. if err != nil {
  41. return nil, err
  42. }
  43. default:
  44. return nil, fmt.Errorf("stats.type was not understood: %s", mType)
  45. }
  46. metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
  47. metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
  48. go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval)
  49. go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval)
  50. return startFn, nil
  51. }
  52. func startGraphiteStats(l *logrus.Logger, i time.Duration, c *config.C, configTest bool) error {
  53. proto := c.GetString("stats.protocol", "tcp")
  54. host := c.GetString("stats.host", "")
  55. if host == "" {
  56. return errors.New("stats.host can not be empty")
  57. }
  58. prefix := c.GetString("stats.prefix", "nebula")
  59. addr, err := net.ResolveTCPAddr(proto, host)
  60. if err != nil {
  61. return fmt.Errorf("error while setting up graphite sink: %s", err)
  62. }
  63. if !configTest {
  64. l.Infof("Starting graphite. Interval: %s, prefix: %s, addr: %s", i, prefix, addr)
  65. go graphite.Graphite(metrics.DefaultRegistry, i, prefix, addr)
  66. }
  67. return nil
  68. }
  69. func startPrometheusStats(l *logrus.Logger, i time.Duration, c *config.C, buildVersion string, configTest bool) (func(), error) {
  70. namespace := c.GetString("stats.namespace", "")
  71. subsystem := c.GetString("stats.subsystem", "")
  72. listen := c.GetString("stats.listen", "")
  73. if listen == "" {
  74. return nil, fmt.Errorf("stats.listen should not be empty")
  75. }
  76. path := c.GetString("stats.path", "")
  77. if path == "" {
  78. return nil, fmt.Errorf("stats.path should not be empty")
  79. }
  80. pr := prometheus.NewRegistry()
  81. pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
  82. if !configTest {
  83. go pClient.UpdatePrometheusMetrics()
  84. }
  85. // Export our version information as labels on a static gauge
  86. g := prometheus.NewGauge(prometheus.GaugeOpts{
  87. Namespace: namespace,
  88. Subsystem: subsystem,
  89. Name: "info",
  90. Help: "Version information for the Nebula binary",
  91. ConstLabels: prometheus.Labels{
  92. "version": buildVersion,
  93. "goversion": runtime.Version(),
  94. },
  95. })
  96. pr.MustRegister(g)
  97. g.Set(1)
  98. var startFn func()
  99. if !configTest {
  100. startFn = func() {
  101. l.Infof("Prometheus stats listening on %s at %s", listen, path)
  102. http.Handle(path, promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}))
  103. log.Fatal(http.ListenAndServe(listen, nil))
  104. }
  105. }
  106. return startFn, nil
  107. }