metrics.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package stats
  2. import (
  3. "fmt"
  4. "net/http"
  5. "os"
  6. "strings"
  7. "time"
  8. "github.com/prometheus/client_golang/prometheus"
  9. "github.com/prometheus/client_golang/prometheus/promhttp"
  10. "github.com/prometheus/client_golang/prometheus/push"
  11. "github.com/chrislusf/seaweedfs/weed/util/log"
  12. )
  13. var (
  14. Gather = prometheus.NewRegistry()
  15. FilerRequestCounter = prometheus.NewCounterVec(
  16. prometheus.CounterOpts{
  17. Namespace: "SeaweedFS",
  18. Subsystem: "filer",
  19. Name: "request_total",
  20. Help: "Counter of filer requests.",
  21. }, []string{"type"})
  22. FilerRequestHistogram = prometheus.NewHistogramVec(
  23. prometheus.HistogramOpts{
  24. Namespace: "SeaweedFS",
  25. Subsystem: "filer",
  26. Name: "request_seconds",
  27. Help: "Bucketed histogram of filer request processing time.",
  28. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  29. }, []string{"type"})
  30. FilerStoreCounter = prometheus.NewCounterVec(
  31. prometheus.CounterOpts{
  32. Namespace: "SeaweedFS",
  33. Subsystem: "filerStore",
  34. Name: "request_total",
  35. Help: "Counter of filer store requests.",
  36. }, []string{"store", "type"})
  37. FilerStoreHistogram = prometheus.NewHistogramVec(
  38. prometheus.HistogramOpts{
  39. Namespace: "SeaweedFS",
  40. Subsystem: "filerStore",
  41. Name: "request_seconds",
  42. Help: "Bucketed histogram of filer store request processing time.",
  43. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  44. }, []string{"store", "type"})
  45. VolumeServerRequestCounter = prometheus.NewCounterVec(
  46. prometheus.CounterOpts{
  47. Namespace: "SeaweedFS",
  48. Subsystem: "volumeServer",
  49. Name: "request_total",
  50. Help: "Counter of volume server requests.",
  51. }, []string{"type"})
  52. VolumeServerRequestHistogram = prometheus.NewHistogramVec(
  53. prometheus.HistogramOpts{
  54. Namespace: "SeaweedFS",
  55. Subsystem: "volumeServer",
  56. Name: "request_seconds",
  57. Help: "Bucketed histogram of volume server request processing time.",
  58. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  59. }, []string{"type"})
  60. VolumeServerVolumeCounter = prometheus.NewGaugeVec(
  61. prometheus.GaugeOpts{
  62. Namespace: "SeaweedFS",
  63. Subsystem: "volumeServer",
  64. Name: "volumes",
  65. Help: "Number of volumes or shards.",
  66. }, []string{"collection", "type"})
  67. VolumeServerReadOnlyVolumeGauge = prometheus.NewGaugeVec(
  68. prometheus.GaugeOpts{
  69. Namespace: "SeaweedFS",
  70. Subsystem: "volumeServer",
  71. Name: "read_only_volumes",
  72. Help: "Number of read only volumes.",
  73. }, []string{"collection", "type"})
  74. VolumeServerMaxVolumeCounter = prometheus.NewGauge(
  75. prometheus.GaugeOpts{
  76. Namespace: "SeaweedFS",
  77. Subsystem: "volumeServer",
  78. Name: "max_volumes",
  79. Help: "Maximum number of volumes.",
  80. })
  81. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  82. prometheus.GaugeOpts{
  83. Namespace: "SeaweedFS",
  84. Subsystem: "volumeServer",
  85. Name: "total_disk_size",
  86. Help: "Actual disk size used by volumes.",
  87. }, []string{"collection", "type"})
  88. VolumeServerResourceGauge = prometheus.NewGaugeVec(
  89. prometheus.GaugeOpts{
  90. Namespace: "SeaweedFS",
  91. Subsystem: "volumeServer",
  92. Name: "resource",
  93. Help: "Resource usage",
  94. }, []string{"name", "type"})
  95. S3RequestCounter = prometheus.NewCounterVec(
  96. prometheus.CounterOpts{
  97. Namespace: "SeaweedFS",
  98. Subsystem: "s3",
  99. Name: "request_total",
  100. Help: "Counter of s3 requests.",
  101. }, []string{"type", "code"})
  102. S3RequestHistogram = prometheus.NewHistogramVec(
  103. prometheus.HistogramOpts{
  104. Namespace: "SeaweedFS",
  105. Subsystem: "s3",
  106. Name: "request_seconds",
  107. Help: "Bucketed histogram of s3 request processing time.",
  108. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  109. }, []string{"type"})
  110. )
  111. func init() {
  112. Gather.MustRegister(FilerRequestCounter)
  113. Gather.MustRegister(FilerRequestHistogram)
  114. Gather.MustRegister(FilerStoreCounter)
  115. Gather.MustRegister(FilerStoreHistogram)
  116. Gather.MustRegister(prometheus.NewGoCollector())
  117. Gather.MustRegister(VolumeServerRequestCounter)
  118. Gather.MustRegister(VolumeServerRequestHistogram)
  119. Gather.MustRegister(VolumeServerVolumeCounter)
  120. Gather.MustRegister(VolumeServerMaxVolumeCounter)
  121. Gather.MustRegister(VolumeServerReadOnlyVolumeGauge)
  122. Gather.MustRegister(VolumeServerDiskSizeGauge)
  123. Gather.MustRegister(VolumeServerResourceGauge)
  124. Gather.MustRegister(S3RequestCounter)
  125. Gather.MustRegister(S3RequestHistogram)
  126. }
  127. func LoopPushingMetric(name, instance, addr string, intervalSeconds int) {
  128. if addr == "" || intervalSeconds == 0 {
  129. return
  130. }
  131. log.Infof("%s server sends metrics to %s every %d seconds", name, addr, intervalSeconds)
  132. pusher := push.New(addr, name).Gatherer(Gather).Grouping("instance", instance)
  133. for {
  134. err := pusher.Push()
  135. if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
  136. log.Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  137. }
  138. if intervalSeconds <= 0 {
  139. intervalSeconds = 15
  140. }
  141. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  142. }
  143. }
  144. func StartMetricsServer(port int) {
  145. if port == 0 {
  146. return
  147. }
  148. http.Handle("/metrics", promhttp.HandlerFor(Gather, promhttp.HandlerOpts{}))
  149. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  150. }
  151. func SourceName(port uint32) string {
  152. hostname, err := os.Hostname()
  153. if err != nil {
  154. return "unknown"
  155. }
  156. return fmt.Sprintf("%s:%d", hostname, port)
  157. }