stats.go 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package nebula
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "net"
  7. "net/http"
  8. "time"
  9. graphite "github.com/cyberdelia/go-metrics-graphite"
  10. mp "github.com/nbrownus/go-metrics-prometheus"
  11. "github.com/prometheus/client_golang/prometheus"
  12. "github.com/prometheus/client_golang/prometheus/promhttp"
  13. "github.com/rcrowley/go-metrics"
  14. "github.com/sirupsen/logrus"
  15. )
  16. func startStats(l *logrus.Logger, c *Config, configTest bool) error {
  17. mType := c.GetString("stats.type", "")
  18. if mType == "" || mType == "none" {
  19. return nil
  20. }
  21. interval := c.GetDuration("stats.interval", 0)
  22. if interval == 0 {
  23. return fmt.Errorf("stats.interval was an invalid duration: %s", c.GetString("stats.interval", ""))
  24. }
  25. switch mType {
  26. case "graphite":
  27. startGraphiteStats(l, interval, c, configTest)
  28. case "prometheus":
  29. startPrometheusStats(l, interval, c, configTest)
  30. default:
  31. return fmt.Errorf("stats.type was not understood: %s", mType)
  32. }
  33. metrics.RegisterDebugGCStats(metrics.DefaultRegistry)
  34. metrics.RegisterRuntimeMemStats(metrics.DefaultRegistry)
  35. go metrics.CaptureDebugGCStats(metrics.DefaultRegistry, interval)
  36. go metrics.CaptureRuntimeMemStats(metrics.DefaultRegistry, interval)
  37. return nil
  38. }
  39. func startGraphiteStats(l *logrus.Logger, i time.Duration, c *Config, configTest bool) error {
  40. proto := c.GetString("stats.protocol", "tcp")
  41. host := c.GetString("stats.host", "")
  42. if host == "" {
  43. return errors.New("stats.host can not be empty")
  44. }
  45. prefix := c.GetString("stats.prefix", "nebula")
  46. addr, err := net.ResolveTCPAddr(proto, host)
  47. if err != nil {
  48. return fmt.Errorf("error while setting up graphite sink: %s", err)
  49. }
  50. l.Infof("Starting graphite. Interval: %s, prefix: %s, addr: %s", i, prefix, addr)
  51. if !configTest {
  52. go graphite.Graphite(metrics.DefaultRegistry, i, prefix, addr)
  53. }
  54. return nil
  55. }
  56. func startPrometheusStats(l *logrus.Logger, i time.Duration, c *Config, configTest bool) error {
  57. namespace := c.GetString("stats.namespace", "")
  58. subsystem := c.GetString("stats.subsystem", "")
  59. listen := c.GetString("stats.listen", "")
  60. if listen == "" {
  61. return fmt.Errorf("stats.listen should not be empty")
  62. }
  63. path := c.GetString("stats.path", "")
  64. if path == "" {
  65. return fmt.Errorf("stats.path should not be empty")
  66. }
  67. pr := prometheus.NewRegistry()
  68. pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
  69. go pClient.UpdatePrometheusMetrics()
  70. if !configTest {
  71. go func() {
  72. l.Infof("Prometheus stats listening on %s at %s", listen, path)
  73. http.Handle(path, promhttp.HandlerFor(pr, promhttp.HandlerOpts{ErrorLog: l}))
  74. log.Fatal(http.ListenAndServe(listen, nil))
  75. }()
  76. }
  77. return nil
  78. }