You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package stats
  2. import (
  3. "fmt"
  4. "os"
  5. "time"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/prometheus/client_golang/prometheus"
  8. "github.com/prometheus/client_golang/prometheus/push"
  9. )
  10. var (
  11. FilerGather = prometheus.NewRegistry()
  12. VolumeServerGather = prometheus.NewRegistry()
  13. FilerRequestCounter = prometheus.NewCounterVec(
  14. prometheus.CounterOpts{
  15. Namespace: "SeaweedFS",
  16. Subsystem: "filer",
  17. Name: "request_total",
  18. Help: "Counter of filer requests.",
  19. }, []string{"type"})
  20. FilerRequestHistogram = prometheus.NewHistogramVec(
  21. prometheus.HistogramOpts{
  22. Namespace: "SeaweedFS",
  23. Subsystem: "filer",
  24. Name: "request_seconds",
  25. Help: "Bucketed histogram of filer request processing time.",
  26. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  27. }, []string{"type"})
  28. VolumeServerRequestCounter = prometheus.NewCounterVec(
  29. prometheus.CounterOpts{
  30. Namespace: "SeaweedFS",
  31. Subsystem: "volumeServer",
  32. Name: "request_total",
  33. Help: "Counter of filer requests.",
  34. }, []string{"type"})
  35. VolumeServerRequestHistogram = prometheus.NewHistogramVec(
  36. prometheus.HistogramOpts{
  37. Namespace: "SeaweedFS",
  38. Subsystem: "volumeServer",
  39. Name: "request_seconds",
  40. Help: "Bucketed histogram of filer request processing time.",
  41. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  42. }, []string{"type"})
  43. VolumeServerVolumeCounter = prometheus.NewGauge(
  44. prometheus.GaugeOpts{
  45. Namespace: "SeaweedFS",
  46. Subsystem: "volumeServer",
  47. Name: "volumes",
  48. Help: "Number of volumes.",
  49. })
  50. VolumeServerEcShardCounter = prometheus.NewGauge(
  51. prometheus.GaugeOpts{
  52. Namespace: "SeaweedFS",
  53. Subsystem: "volumeServer",
  54. Name: "ec_shards",
  55. Help: "Number of EC shards.",
  56. })
  57. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  58. prometheus.GaugeOpts{
  59. Namespace: "SeaweedFS",
  60. Subsystem: "volumeServer",
  61. Name: "total_disk_size",
  62. Help: "Actual disk size used by volumes.",
  63. }, []string{"collection", "type"})
  64. )
  65. func init() {
  66. FilerGather.MustRegister(FilerRequestCounter)
  67. FilerGather.MustRegister(FilerRequestHistogram)
  68. VolumeServerGather.MustRegister(VolumeServerRequestCounter)
  69. VolumeServerGather.MustRegister(VolumeServerRequestHistogram)
  70. VolumeServerGather.MustRegister(VolumeServerVolumeCounter)
  71. VolumeServerGather.MustRegister(VolumeServerEcShardCounter)
  72. VolumeServerGather.MustRegister(VolumeServerDiskSizeGauge)
  73. }
  74. func StartPushingMetric(name, instance string, gatherer *prometheus.Registry, addr string, intervalSeconds int) {
  75. if intervalSeconds == 0 || addr == "" {
  76. glog.V(0).Info("disable metrics reporting")
  77. return
  78. }
  79. glog.V(0).Infof("push metrics to %s every %d seconds", addr, intervalSeconds)
  80. go loopPushMetrics(name, instance, gatherer, addr, intervalSeconds)
  81. }
  82. func loopPushMetrics(name, instance string, gatherer *prometheus.Registry, addr string, intervalSeconds int) {
  83. pusher := push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance)
  84. for {
  85. err := pusher.Push()
  86. if err != nil {
  87. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  88. }
  89. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  90. }
  91. }
  92. func SourceName(port int) string {
  93. hostname, err := os.Hostname()
  94. if err != nil {
  95. return "unknown"
  96. }
  97. return fmt.Sprintf("%s:%d", hostname, port)
  98. }