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.

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