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.

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