stats.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. func startStats(l *logrus.Logger, c *Config, buildVersion string, configTest bool) error {
  18. mType := c.GetString("stats.type", "")
  19. if mType == "" || mType == "none" {
  20. return nil
  21. }
  22. interval := c.GetDuration("stats.interval", 0)
  23. if interval == 0 {
  24. return fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
  25. }
  26. switch mType {
  27. case "graphite":
  28. startGraphiteStats(l, interval, c, configTest)
  29. case "prometheus":
  30. startPrometheusStats(l, interval, c, buildVersion, configTest)
  31. default:
  32. return fmt.Errorf("stats.type was not understood: %s", mType)
  33. }
  34. metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
  35. metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
  36. go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval)
  37. go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval)
  38. return nil
  39. }
  40. func startGraphiteStats(l *logrus.Logger, i time.Duration, c *Config, configTest bool) error {
  41. proto := c.GetString("stats.protocol", "tcp")
  42. host := c.GetString("stats.host", "")
  43. if host == "" {
  44. return errors.New("stats.host can not be empty")
  45. }
  46. prefix := c.GetString("stats.prefix", "nebula")
  47. addr, err := net.ResolveTCPAddr(proto, host)
  48. if err != nil {
  49. return fmt.Errorf("error while setting up graphite sink: %s", err)
  50. }
  51. l.Infof("Starting graphite. Interval: %s, prefix: %s, addr: %s", i, prefix, addr)
  52. if !configTest {
  53. go graphite.Graphite(metrics.DefaultRegistry, i, prefix, addr)
  54. }
  55. return nil
  56. }
  57. func startPrometheusStats(l *logrus.Logger, i time.Duration, c *Config, buildVersion string, configTest bool) error {
  58. namespace := c.GetString("stats.namespace", "")
  59. subsystem := c.GetString("stats.subsystem", "")
  60. listen := c.GetString("stats.listen", "")
  61. if listen == "" {
  62. return fmt.Errorf("stats.listen should not be empty")
  63. }
  64. path := c.GetString("stats.path", "")
  65. if path == "" {
  66. return fmt.Errorf("stats.path should not be empty")
  67. }
  68. pr := prometheus.NewRegistry()
  69. pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
  70. go pClient.UpdatePrometheusMetrics()
  71. // Export our version information as labels on a static gauge
  72. g := prometheus.NewGauge(prometheus.GaugeOpts{
  73. Namespace: namespace,
  74. Subsystem: subsystem,
  75. Name: "info",
  76. Help: "Version information for the Nebula binary",
  77. ConstLabels: prometheus.Labels{
  78. "version": buildVersion,
  79. "goversion": runtime.Version(),
  80. },
  81. })
  82. pr.MustRegister(g)
  83. g.Set(1)
  84. if !configTest {
  85. go func() {
  86. l.Infof("Prometheus stats listening on %s at %s", listen, path)
  87. http.Handle(path, promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}))
  88. log.Fatal(http.ListenAndServe(listen, nil))
  89. }()
  90. }
  91. return nil
  92. }