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.

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