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.

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