metrics.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. FilerStoreCounter = prometheus.NewCounterVec(
  29. prometheus.CounterOpts{
  30. Namespace: "SeaweedFS",
  31. Subsystem: "filerStore",
  32. Name: "request_total",
  33. Help: "Counter of filer store requests.",
  34. }, []string{"store", "type"})
  35. FilerStoreHistogram = prometheus.NewHistogramVec(
  36. prometheus.HistogramOpts{
  37. Namespace: "SeaweedFS",
  38. Subsystem: "filerStore",
  39. Name: "request_seconds",
  40. Help: "Bucketed histogram of filer store request processing time.",
  41. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  42. }, []string{"store", "type"})
  43. VolumeServerRequestCounter = prometheus.NewCounterVec(
  44. prometheus.CounterOpts{
  45. Namespace: "SeaweedFS",
  46. Subsystem: "volumeServer",
  47. Name: "request_total",
  48. Help: "Counter of volume server requests.",
  49. }, []string{"type"})
  50. VolumeServerRequestHistogram = prometheus.NewHistogramVec(
  51. prometheus.HistogramOpts{
  52. Namespace: "SeaweedFS",
  53. Subsystem: "volumeServer",
  54. Name: "request_seconds",
  55. Help: "Bucketed histogram of volume server request processing time.",
  56. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  57. }, []string{"type"})
  58. VolumeServerVolumeCounter = prometheus.NewGaugeVec(
  59. prometheus.GaugeOpts{
  60. Namespace: "SeaweedFS",
  61. Subsystem: "volumeServer",
  62. Name: "volumes",
  63. Help: "Number of volumes or shards.",
  64. }, []string{"collection", "type"})
  65. VolumeServerMaxVolumeCounter = prometheus.NewGauge(
  66. prometheus.GaugeOpts{
  67. Namespace: "SeaweedFS",
  68. Subsystem: "volumeServer",
  69. Name: "max_volumes",
  70. Help: "Maximum number of volumes.",
  71. })
  72. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  73. prometheus.GaugeOpts{
  74. Namespace: "SeaweedFS",
  75. Subsystem: "volumeServer",
  76. Name: "total_disk_size",
  77. Help: "Actual disk size used by volumes.",
  78. }, []string{"collection", "type"})
  79. )
  80. func init() {
  81. FilerGather.MustRegister(FilerRequestCounter)
  82. FilerGather.MustRegister(FilerRequestHistogram)
  83. FilerGather.MustRegister(FilerStoreCounter)
  84. FilerGather.MustRegister(FilerStoreHistogram)
  85. FilerGather.MustRegister(prometheus.NewGoCollector())
  86. VolumeServerGather.MustRegister(VolumeServerRequestCounter)
  87. VolumeServerGather.MustRegister(VolumeServerRequestHistogram)
  88. VolumeServerGather.MustRegister(VolumeServerVolumeCounter)
  89. VolumeServerGather.MustRegister(VolumeServerMaxVolumeCounter)
  90. VolumeServerGather.MustRegister(VolumeServerDiskSizeGauge)
  91. }
  92. func LoopPushingMetric(name, instance string, gatherer *prometheus.Registry, fnGetMetricsDest func() (addr string, intervalSeconds int)) {
  93. if fnGetMetricsDest == nil {
  94. return
  95. }
  96. addr, intervalSeconds := fnGetMetricsDest()
  97. pusher := push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance)
  98. currentAddr := addr
  99. for {
  100. if currentAddr != "" {
  101. err := pusher.Push()
  102. if err != nil {
  103. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  104. }
  105. }
  106. if intervalSeconds <= 0 {
  107. intervalSeconds = 15
  108. }
  109. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  110. addr, intervalSeconds = fnGetMetricsDest()
  111. if currentAddr != addr {
  112. pusher = push.New(addr, name).Gatherer(gatherer).Grouping("instance", instance)
  113. currentAddr = addr
  114. }
  115. }
  116. }
  117. func SourceName(port uint32) string {
  118. hostname, err := os.Hostname()
  119. if err != nil {
  120. return "unknown"
  121. }
  122. return fmt.Sprintf("%s:%d", hostname, port)
  123. }