stats.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. )
  17. // startStats initializes stats from config. On success, if any futher work
  18. // is needed to serve stats, it returns a func to handle that work. If no
  19. // work is needed, it'll return nil. On failure, it returns nil, error.
  20. func startStats(l *logrus.Logger, c *Config, buildVersion string, configTest bool) (func(), error) {
  21. mType := c.GetString("stats.type", "")
  22. if mType == "" || mType == "none" {
  23. return nil, nil
  24. }
  25. interval := c.GetDuration("stats.interval", 0)
  26. if interval == 0 {
  27. return nil, fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
  28. }
  29. var startFn func()
  30. switch mType {
  31. case "graphite":
  32. err := startGraphiteStats(l, interval, c, configTest)
  33. if err != nil {
  34. return nil, err
  35. }
  36. case "prometheus":
  37. var err error
  38. startFn, err = startPrometheusStats(l, interval, c, 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 startFn, nil
  50. }
  51. func startGraphiteStats(l *logrus.Logger, i time.Duration, c *Config, 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, buildVersion string, configTest bool) (func(), error) {
  69. namespace := c.GetString("stats.namespace", "")
  70. subsystem := c.GetString("stats.subsystem", "")
  71. listen := c.GetString("stats.listen", "")
  72. if listen == "" {
  73. return nil, fmt.Errorf("stats.listen should not be empty")
  74. }
  75. path := c.GetString("stats.path", "")
  76. if path == "" {
  77. return nil, fmt.Errorf("stats.path should not be empty")
  78. }
  79. pr := prometheus.NewRegistry()
  80. pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
  81. go pClient.UpdatePrometheusMetrics()
  82. // Export our version information as labels on a static gauge
  83. g := prometheus.NewGauge(prometheus.GaugeOpts{
  84. Namespace: namespace,
  85. Subsystem: subsystem,
  86. Name: "info",
  87. Help: "Version information for the Nebula binary",
  88. ConstLabels: prometheus.Labels{
  89. "version": buildVersion,
  90. "goversion": runtime.Version(),
  91. },
  92. })
  93. pr.MustRegister(g)
  94. g.Set(1)
  95. var startFn func()
  96. if !configTest {
  97. startFn = func() {
  98. l.Infof("Prometheus stats listening on %s at %s", listen, path)
  99. http.Handle(path, promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}))
  100. log.Fatal(http.ListenAndServe(listen, nil))
  101. }
  102. }
  103. return startFn, nil
  104. }