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.

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