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.

280 lines
8.5 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
  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/prometheus/client_golang/prometheus"
  12. "github.com/prometheus/client_golang/prometheus/collectors"
  13. "github.com/prometheus/client_golang/prometheus/promhttp"
  14. "github.com/prometheus/client_golang/prometheus/push"
  15. "github.com/seaweedfs/seaweedfs/weed/glog"
  16. )
  17. // Readonly volume types
  18. const (
  19. Namespace = "SeaweedFS"
  20. IsReadOnly = "IsReadOnly"
  21. NoWriteOrDelete = "noWriteOrDelete"
  22. NoWriteCanDelete = "noWriteCanDelete"
  23. IsDiskSpaceLow = "isDiskSpaceLow"
  24. )
  25. var readOnlyVolumeTypes = [4]string{IsReadOnly, NoWriteOrDelete, NoWriteCanDelete, IsDiskSpaceLow}
  26. var (
  27. Gather = prometheus.NewRegistry()
  28. MasterClientConnectCounter = prometheus.NewCounterVec(
  29. prometheus.CounterOpts{
  30. Namespace: Namespace,
  31. Subsystem: "wdclient",
  32. Name: "connect_updates",
  33. Help: "Counter of master client leader updates.",
  34. }, []string{"type"})
  35. MasterRaftIsleader = prometheus.NewGauge(
  36. prometheus.GaugeOpts{
  37. Namespace: Namespace,
  38. Subsystem: "master",
  39. Name: "is_leader",
  40. Help: "is leader",
  41. })
  42. MasterAdminLock = prometheus.NewGaugeVec(
  43. prometheus.GaugeOpts{
  44. Namespace: Namespace,
  45. Subsystem: "master",
  46. Name: "admin_lock",
  47. Help: "admin lock",
  48. }, []string{"client"})
  49. MasterReceivedHeartbeatCounter = prometheus.NewCounterVec(
  50. prometheus.CounterOpts{
  51. Namespace: Namespace,
  52. Subsystem: "master",
  53. Name: "received_heartbeats",
  54. Help: "Counter of master received heartbeat.",
  55. }, []string{"type"})
  56. MasterReplicaPlacementMismatch = prometheus.NewGaugeVec(
  57. prometheus.GaugeOpts{
  58. Namespace: Namespace,
  59. Subsystem: "master",
  60. Name: "replica_placement_mismatch",
  61. Help: "replica placement mismatch",
  62. }, []string{"collection", "id"})
  63. MasterLeaderChangeCounter = prometheus.NewCounterVec(
  64. prometheus.CounterOpts{
  65. Namespace: Namespace,
  66. Subsystem: "master",
  67. Name: "leader_changes",
  68. Help: "Counter of master leader changes.",
  69. }, []string{"type"})
  70. FilerRequestCounter = prometheus.NewCounterVec(
  71. prometheus.CounterOpts{
  72. Namespace: Namespace,
  73. Subsystem: "filer",
  74. Name: "request_total",
  75. Help: "Counter of filer requests.",
  76. }, []string{"type"})
  77. FilerRequestHistogram = prometheus.NewHistogramVec(
  78. prometheus.HistogramOpts{
  79. Namespace: Namespace,
  80. Subsystem: "filer",
  81. Name: "request_seconds",
  82. Help: "Bucketed histogram of filer request processing time.",
  83. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  84. }, []string{"type"})
  85. FilerServerLastSendTsOfSubscribeGauge = prometheus.NewGaugeVec(
  86. prometheus.GaugeOpts{
  87. Namespace: Namespace,
  88. Subsystem: "filer",
  89. Name: "last_send_timestamp_of_subscribe",
  90. Help: "The last send timestamp of the filer subscription.",
  91. }, []string{"sourceFiler", "clientName", "path"})
  92. FilerStoreCounter = prometheus.NewCounterVec(
  93. prometheus.CounterOpts{
  94. Namespace: Namespace,
  95. Subsystem: "filerStore",
  96. Name: "request_total",
  97. Help: "Counter of filer store requests.",
  98. }, []string{"store", "type"})
  99. FilerStoreHistogram = prometheus.NewHistogramVec(
  100. prometheus.HistogramOpts{
  101. Namespace: Namespace,
  102. Subsystem: "filerStore",
  103. Name: "request_seconds",
  104. Help: "Bucketed histogram of filer store request processing time.",
  105. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  106. }, []string{"store", "type"})
  107. FilerSyncOffsetGauge = prometheus.NewGaugeVec(
  108. prometheus.GaugeOpts{
  109. Namespace: Namespace,
  110. Subsystem: "filerSync",
  111. Name: "sync_offset",
  112. Help: "The offset of the filer synchronization service.",
  113. }, []string{"sourceFiler", "targetFiler", "clientName", "path"})
  114. VolumeServerRequestCounter = prometheus.NewCounterVec(
  115. prometheus.CounterOpts{
  116. Namespace: Namespace,
  117. Subsystem: "volumeServer",
  118. Name: "request_total",
  119. Help: "Counter of volume server requests.",
  120. }, []string{"type"})
  121. VolumeServerRequestHistogram = prometheus.NewHistogramVec(
  122. prometheus.HistogramOpts{
  123. Namespace: Namespace,
  124. Subsystem: "volumeServer",
  125. Name: "request_seconds",
  126. Help: "Bucketed histogram of volume server request processing time.",
  127. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  128. }, []string{"type"})
  129. VolumeServerVolumeCounter = prometheus.NewGaugeVec(
  130. prometheus.GaugeOpts{
  131. Namespace: Namespace,
  132. Subsystem: "volumeServer",
  133. Name: "volumes",
  134. Help: "Number of volumes or shards.",
  135. }, []string{"collection", "type"})
  136. VolumeServerReadOnlyVolumeGauge = prometheus.NewGaugeVec(
  137. prometheus.GaugeOpts{
  138. Namespace: Namespace,
  139. Subsystem: "volumeServer",
  140. Name: "read_only_volumes",
  141. Help: "Number of read only volumes.",
  142. }, []string{"collection", "type"})
  143. VolumeServerMaxVolumeCounter = prometheus.NewGauge(
  144. prometheus.GaugeOpts{
  145. Namespace: Namespace,
  146. Subsystem: "volumeServer",
  147. Name: "max_volumes",
  148. Help: "Maximum number of volumes.",
  149. })
  150. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  151. prometheus.GaugeOpts{
  152. Namespace: Namespace,
  153. Subsystem: "volumeServer",
  154. Name: "total_disk_size",
  155. Help: "Actual disk size used by volumes.",
  156. }, []string{"collection", "type"})
  157. VolumeServerResourceGauge = prometheus.NewGaugeVec(
  158. prometheus.GaugeOpts{
  159. Namespace: Namespace,
  160. Subsystem: "volumeServer",
  161. Name: "resource",
  162. Help: "Resource usage",
  163. }, []string{"name", "type"})
  164. S3RequestCounter = prometheus.NewCounterVec(
  165. prometheus.CounterOpts{
  166. Namespace: Namespace,
  167. Subsystem: "s3",
  168. Name: "request_total",
  169. Help: "Counter of s3 requests.",
  170. }, []string{"type", "code", "bucket"})
  171. S3RequestHistogram = prometheus.NewHistogramVec(
  172. prometheus.HistogramOpts{
  173. Namespace: Namespace,
  174. Subsystem: "s3",
  175. Name: "request_seconds",
  176. Help: "Bucketed histogram of s3 request processing time.",
  177. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  178. }, []string{"type", "bucket"})
  179. )
  180. func init() {
  181. Gather.MustRegister(MasterClientConnectCounter)
  182. Gather.MustRegister(MasterRaftIsleader)
  183. Gather.MustRegister(MasterAdminLock)
  184. Gather.MustRegister(MasterReceivedHeartbeatCounter)
  185. Gather.MustRegister(MasterLeaderChangeCounter)
  186. Gather.MustRegister(MasterReplicaPlacementMismatch)
  187. Gather.MustRegister(FilerRequestCounter)
  188. Gather.MustRegister(FilerRequestHistogram)
  189. Gather.MustRegister(FilerStoreCounter)
  190. Gather.MustRegister(FilerStoreHistogram)
  191. Gather.MustRegister(FilerSyncOffsetGauge)
  192. Gather.MustRegister(FilerServerLastSendTsOfSubscribeGauge)
  193. Gather.MustRegister(collectors.NewGoCollector())
  194. Gather.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
  195. Gather.MustRegister(VolumeServerRequestCounter)
  196. Gather.MustRegister(VolumeServerRequestHistogram)
  197. Gather.MustRegister(VolumeServerVolumeCounter)
  198. Gather.MustRegister(VolumeServerMaxVolumeCounter)
  199. Gather.MustRegister(VolumeServerReadOnlyVolumeGauge)
  200. Gather.MustRegister(VolumeServerDiskSizeGauge)
  201. Gather.MustRegister(VolumeServerResourceGauge)
  202. Gather.MustRegister(S3RequestCounter)
  203. Gather.MustRegister(S3RequestHistogram)
  204. }
  205. func LoopPushingMetric(name, instance, addr string, intervalSeconds int) {
  206. if addr == "" || intervalSeconds == 0 {
  207. return
  208. }
  209. glog.V(0).Infof("%s server sends metrics to %s every %d seconds", name, addr, intervalSeconds)
  210. pusher := push.New(addr, name).Gatherer(Gather).Grouping("instance", instance)
  211. for {
  212. err := pusher.Push()
  213. if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
  214. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  215. }
  216. if intervalSeconds <= 0 {
  217. intervalSeconds = 15
  218. }
  219. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  220. }
  221. }
  222. func StartMetricsServer(port int) {
  223. if port == 0 {
  224. return
  225. }
  226. http.Handle("/metrics", promhttp.HandlerFor(Gather, promhttp.HandlerOpts{}))
  227. log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), nil))
  228. }
  229. func SourceName(port uint32) string {
  230. hostname, err := os.Hostname()
  231. if err != nil {
  232. return "unknown"
  233. }
  234. return net.JoinHostPort(hostname, strconv.Itoa(int(port)))
  235. }
  236. // todo - can be changed to DeletePartialMatch when https://github.com/prometheus/client_golang/pull/1013 gets released
  237. func DeleteCollectionMetrics(collection string) {
  238. VolumeServerDiskSizeGauge.DeleteLabelValues(collection, "normal")
  239. for _, volume_type := range readOnlyVolumeTypes {
  240. VolumeServerReadOnlyVolumeGauge.DeleteLabelValues(collection, volume_type)
  241. }
  242. VolumeServerVolumeCounter.DeleteLabelValues(collection, "volume")
  243. }