metrics.go 4.2 KB

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