metrics.go 5.5 KB

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