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.

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