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.

168 lines
4.7 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. VolumeServerMaxVolumeCounter = prometheus.NewGauge(
  69. prometheus.GaugeOpts{
  70. Namespace: "SeaweedFS",
  71. Subsystem: "volumeServer",
  72. Name: "max_volumes",
  73. Help: "Maximum number of volumes.",
  74. })
  75. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  76. prometheus.GaugeOpts{
  77. Namespace: "SeaweedFS",
  78. Subsystem: "volumeServer",
  79. Name: "total_disk_size",
  80. Help: "Actual disk size used by volumes.",
  81. }, []string{"collection", "type"})
  82. S3RequestCounter = prometheus.NewCounterVec(
  83. prometheus.CounterOpts{
  84. Namespace: "SeaweedFS",
  85. Subsystem: "s3",
  86. Name: "request_total",
  87. Help: "Counter of s3 requests.",
  88. }, []string{"type", "code"})
  89. S3RequestHistogram = prometheus.NewHistogramVec(
  90. prometheus.HistogramOpts{
  91. Namespace: "SeaweedFS",
  92. Subsystem: "s3",
  93. Name: "request_seconds",
  94. Help: "Bucketed histogram of s3 request processing time.",
  95. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  96. }, []string{"type"})
  97. )
  98. func init() {
  99. Gather.MustRegister(FilerRequestCounter)
  100. Gather.MustRegister(FilerRequestHistogram)
  101. Gather.MustRegister(FilerStoreCounter)
  102. Gather.MustRegister(FilerStoreHistogram)
  103. Gather.MustRegister(prometheus.NewGoCollector())
  104. Gather.MustRegister(VolumeServerRequestCounter)
  105. Gather.MustRegister(VolumeServerRequestHistogram)
  106. Gather.MustRegister(VolumeServerVolumeCounter)
  107. Gather.MustRegister(VolumeServerMaxVolumeCounter)
  108. Gather.MustRegister(VolumeServerDiskSizeGauge)
  109. Gather.MustRegister(S3RequestCounter)
  110. Gather.MustRegister(S3RequestHistogram)
  111. }
  112. func LoopPushingMetric(name, instance, addr string, intervalSeconds int) {
  113. if addr == "" || intervalSeconds == 0 {
  114. return
  115. }
  116. glog.V(0).Infof("%s server sends metrics to %s every %d seconds", name, addr, intervalSeconds)
  117. pusher := push.New(addr, name).Gatherer(Gather).Grouping("instance", instance)
  118. for {
  119. err := pusher.Push()
  120. if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
  121. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  122. }
  123. if intervalSeconds <= 0 {
  124. intervalSeconds = 15
  125. }
  126. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  127. }
  128. }
  129. func StartMetricsServer(port int) {
  130. if port == 0 {
  131. return
  132. }
  133. http.Handle("/metrics", promhttp.HandlerFor(Gather, promhttp.HandlerOpts{}))
  134. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  135. }
  136. func SourceName(port uint32) string {
  137. hostname, err := os.Hostname()
  138. if err != nil {
  139. return "unknown"
  140. }
  141. return fmt.Sprintf("%s:%d", hostname, port)
  142. }