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.

223 lines
6.6 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
  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. MasterClientConnectCounter = prometheus.NewCounterVec(
  20. prometheus.CounterOpts{
  21. Namespace: "SeaweedFS",
  22. Subsystem: "wdclient",
  23. Name: "connect_updates",
  24. Help: "Counter of master client leader updates.",
  25. }, []string{"type"})
  26. MasterRaftIsleader = prometheus.NewGauge(
  27. prometheus.GaugeOpts{
  28. Namespace: "SeaweedFS",
  29. Subsystem: "master",
  30. Name: "is_leader",
  31. Help: "is leader",
  32. })
  33. MasterReceivedHeartbeatCounter = prometheus.NewCounterVec(
  34. prometheus.CounterOpts{
  35. Namespace: "SeaweedFS",
  36. Subsystem: "master",
  37. Name: "received_heartbeats",
  38. Help: "Counter of master received heartbeat.",
  39. }, []string{"type"})
  40. MasterLeaderChangeCounter = prometheus.NewCounterVec(
  41. prometheus.CounterOpts{
  42. Namespace: "SeaweedFS",
  43. Subsystem: "master",
  44. Name: "leader_changes",
  45. Help: "Counter of master leader changes.",
  46. }, []string{"type"})
  47. FilerRequestCounter = prometheus.NewCounterVec(
  48. prometheus.CounterOpts{
  49. Namespace: "SeaweedFS",
  50. Subsystem: "filer",
  51. Name: "request_total",
  52. Help: "Counter of filer requests.",
  53. }, []string{"type"})
  54. FilerRequestHistogram = prometheus.NewHistogramVec(
  55. prometheus.HistogramOpts{
  56. Namespace: "SeaweedFS",
  57. Subsystem: "filer",
  58. Name: "request_seconds",
  59. Help: "Bucketed histogram of filer request processing time.",
  60. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  61. }, []string{"type"})
  62. FilerStoreCounter = prometheus.NewCounterVec(
  63. prometheus.CounterOpts{
  64. Namespace: "SeaweedFS",
  65. Subsystem: "filerStore",
  66. Name: "request_total",
  67. Help: "Counter of filer store requests.",
  68. }, []string{"store", "type"})
  69. FilerStoreHistogram = prometheus.NewHistogramVec(
  70. prometheus.HistogramOpts{
  71. Namespace: "SeaweedFS",
  72. Subsystem: "filerStore",
  73. Name: "request_seconds",
  74. Help: "Bucketed histogram of filer store request processing time.",
  75. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  76. }, []string{"store", "type"})
  77. VolumeServerRequestCounter = prometheus.NewCounterVec(
  78. prometheus.CounterOpts{
  79. Namespace: "SeaweedFS",
  80. Subsystem: "volumeServer",
  81. Name: "request_total",
  82. Help: "Counter of volume server requests.",
  83. }, []string{"type"})
  84. VolumeServerRequestHistogram = prometheus.NewHistogramVec(
  85. prometheus.HistogramOpts{
  86. Namespace: "SeaweedFS",
  87. Subsystem: "volumeServer",
  88. Name: "request_seconds",
  89. Help: "Bucketed histogram of volume server request processing time.",
  90. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  91. }, []string{"type"})
  92. VolumeServerVolumeCounter = prometheus.NewGaugeVec(
  93. prometheus.GaugeOpts{
  94. Namespace: "SeaweedFS",
  95. Subsystem: "volumeServer",
  96. Name: "volumes",
  97. Help: "Number of volumes or shards.",
  98. }, []string{"collection", "type"})
  99. VolumeServerReadOnlyVolumeGauge = prometheus.NewGaugeVec(
  100. prometheus.GaugeOpts{
  101. Namespace: "SeaweedFS",
  102. Subsystem: "volumeServer",
  103. Name: "read_only_volumes",
  104. Help: "Number of read only volumes.",
  105. }, []string{"collection", "type"})
  106. VolumeServerMaxVolumeCounter = prometheus.NewGauge(
  107. prometheus.GaugeOpts{
  108. Namespace: "SeaweedFS",
  109. Subsystem: "volumeServer",
  110. Name: "max_volumes",
  111. Help: "Maximum number of volumes.",
  112. })
  113. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  114. prometheus.GaugeOpts{
  115. Namespace: "SeaweedFS",
  116. Subsystem: "volumeServer",
  117. Name: "total_disk_size",
  118. Help: "Actual disk size used by volumes.",
  119. }, []string{"collection", "type"})
  120. VolumeServerResourceGauge = prometheus.NewGaugeVec(
  121. prometheus.GaugeOpts{
  122. Namespace: "SeaweedFS",
  123. Subsystem: "volumeServer",
  124. Name: "resource",
  125. Help: "Resource usage",
  126. }, []string{"name", "type"})
  127. S3RequestCounter = prometheus.NewCounterVec(
  128. prometheus.CounterOpts{
  129. Namespace: "SeaweedFS",
  130. Subsystem: "s3",
  131. Name: "request_total",
  132. Help: "Counter of s3 requests.",
  133. }, []string{"type", "code"})
  134. S3RequestHistogram = prometheus.NewHistogramVec(
  135. prometheus.HistogramOpts{
  136. Namespace: "SeaweedFS",
  137. Subsystem: "s3",
  138. Name: "request_seconds",
  139. Help: "Bucketed histogram of s3 request processing time.",
  140. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  141. }, []string{"type"})
  142. )
  143. func init() {
  144. Gather.MustRegister(MasterClientConnectCounter)
  145. Gather.MustRegister(MasterRaftIsleader)
  146. Gather.MustRegister(MasterReceivedHeartbeatCounter)
  147. Gather.MustRegister(MasterLeaderChangeCounter)
  148. Gather.MustRegister(FilerRequestCounter)
  149. Gather.MustRegister(FilerRequestHistogram)
  150. Gather.MustRegister(FilerStoreCounter)
  151. Gather.MustRegister(FilerStoreHistogram)
  152. Gather.MustRegister(collectors.NewGoCollector())
  153. Gather.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
  154. Gather.MustRegister(VolumeServerRequestCounter)
  155. Gather.MustRegister(VolumeServerRequestHistogram)
  156. Gather.MustRegister(VolumeServerVolumeCounter)
  157. Gather.MustRegister(VolumeServerMaxVolumeCounter)
  158. Gather.MustRegister(VolumeServerReadOnlyVolumeGauge)
  159. Gather.MustRegister(VolumeServerDiskSizeGauge)
  160. Gather.MustRegister(VolumeServerResourceGauge)
  161. Gather.MustRegister(S3RequestCounter)
  162. Gather.MustRegister(S3RequestHistogram)
  163. }
  164. func LoopPushingMetric(name, instance, addr string, intervalSeconds int) {
  165. if addr == "" || intervalSeconds == 0 {
  166. return
  167. }
  168. glog.V(0).Infof("%s server sends metrics to %s every %d seconds", name, addr, intervalSeconds)
  169. pusher := push.New(addr, name).Gatherer(Gather).Grouping("instance", instance)
  170. for {
  171. err := pusher.Push()
  172. if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
  173. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  174. }
  175. if intervalSeconds <= 0 {
  176. intervalSeconds = 15
  177. }
  178. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  179. }
  180. }
  181. func StartMetricsServer(port int) {
  182. if port == 0 {
  183. return
  184. }
  185. http.Handle("/metrics", promhttp.HandlerFor(Gather, promhttp.HandlerOpts{}))
  186. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  187. }
  188. func SourceName(port uint32) string {
  189. hostname, err := os.Hostname()
  190. if err != nil {
  191. return "unknown"
  192. }
  193. return net.JoinHostPort(hostname, strconv.Itoa(int(port)))
  194. }