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.

351 lines
11 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. MasterVolumeLayout = prometheus.NewGaugeVec(
  63. prometheus.GaugeOpts{
  64. Namespace: Namespace,
  65. Subsystem: "master",
  66. Name: "volume_layout_total",
  67. Help: "Number of volumes in volume layouts",
  68. }, []string{"collection", "replica", "type"})
  69. MasterLeaderChangeCounter = prometheus.NewCounterVec(
  70. prometheus.CounterOpts{
  71. Namespace: Namespace,
  72. Subsystem: "master",
  73. Name: "leader_changes",
  74. Help: "Counter of master leader changes.",
  75. }, []string{"type"})
  76. FilerRequestCounter = prometheus.NewCounterVec(
  77. prometheus.CounterOpts{
  78. Namespace: Namespace,
  79. Subsystem: "filer",
  80. Name: "request_total",
  81. Help: "Counter of filer requests.",
  82. }, []string{"type", "code"})
  83. FilerHandlerCounter = prometheus.NewCounterVec(
  84. prometheus.CounterOpts{
  85. Namespace: Namespace,
  86. Subsystem: "filer",
  87. Name: "handler_total",
  88. Help: "Counter of filer handlers.",
  89. }, []string{"type"})
  90. FilerRequestHistogram = prometheus.NewHistogramVec(
  91. prometheus.HistogramOpts{
  92. Namespace: Namespace,
  93. Subsystem: "filer",
  94. Name: "request_seconds",
  95. Help: "Bucketed histogram of filer request processing time.",
  96. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  97. }, []string{"type"})
  98. FilerServerLastSendTsOfSubscribeGauge = prometheus.NewGaugeVec(
  99. prometheus.GaugeOpts{
  100. Namespace: Namespace,
  101. Subsystem: "filer",
  102. Name: "last_send_timestamp_of_subscribe",
  103. Help: "The last send timestamp of the filer subscription.",
  104. }, []string{"sourceFiler", "clientName", "path"})
  105. FilerStoreCounter = prometheus.NewCounterVec(
  106. prometheus.CounterOpts{
  107. Namespace: Namespace,
  108. Subsystem: "filerStore",
  109. Name: "request_total",
  110. Help: "Counter of filer store requests.",
  111. }, []string{"store", "type"})
  112. FilerStoreHistogram = prometheus.NewHistogramVec(
  113. prometheus.HistogramOpts{
  114. Namespace: Namespace,
  115. Subsystem: "filerStore",
  116. Name: "request_seconds",
  117. Help: "Bucketed histogram of filer store request processing time.",
  118. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  119. }, []string{"store", "type"})
  120. FilerSyncOffsetGauge = prometheus.NewGaugeVec(
  121. prometheus.GaugeOpts{
  122. Namespace: Namespace,
  123. Subsystem: "filerSync",
  124. Name: "sync_offset",
  125. Help: "The offset of the filer synchronization service.",
  126. }, []string{"sourceFiler", "targetFiler", "clientName", "path"})
  127. VolumeServerRequestCounter = prometheus.NewCounterVec(
  128. prometheus.CounterOpts{
  129. Namespace: Namespace,
  130. Subsystem: "volumeServer",
  131. Name: "request_total",
  132. Help: "Counter of volume server requests.",
  133. }, []string{"type", "code"})
  134. VolumeServerHandlerCounter = prometheus.NewCounterVec(
  135. prometheus.CounterOpts{
  136. Namespace: Namespace,
  137. Subsystem: "volumeServer",
  138. Name: "handler_total",
  139. Help: "Counter of volume server handlers.",
  140. }, []string{"type"})
  141. VolumeServerVacuumingCompactCounter = prometheus.NewCounterVec(
  142. prometheus.CounterOpts{
  143. Namespace: Namespace,
  144. Subsystem: "volumeServer",
  145. Name: "vacuuming_compact_count",
  146. Help: "Counter of volume vacuuming Compact counter",
  147. }, []string{"success"})
  148. VolumeServerVacuumingCommitCounter = prometheus.NewCounterVec(
  149. prometheus.CounterOpts{
  150. Namespace: Namespace,
  151. Subsystem: "volumeServer",
  152. Name: "vacuuming_commit_count",
  153. Help: "Counter of volume vacuuming commit counter",
  154. }, []string{"success"})
  155. VolumeServerVacuumingHistogram = prometheus.NewHistogramVec(
  156. prometheus.HistogramOpts{
  157. Namespace: Namespace,
  158. Subsystem: "volumeServer",
  159. Name: "vacuuming_seconds",
  160. Help: "Bucketed histogram of volume server vacuuming processing time.",
  161. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  162. }, []string{"type"})
  163. VolumeServerRequestHistogram = prometheus.NewHistogramVec(
  164. prometheus.HistogramOpts{
  165. Namespace: Namespace,
  166. Subsystem: "volumeServer",
  167. Name: "request_seconds",
  168. Help: "Bucketed histogram of volume server request processing time.",
  169. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  170. }, []string{"type"})
  171. VolumeServerVolumeCounter = prometheus.NewGaugeVec(
  172. prometheus.GaugeOpts{
  173. Namespace: Namespace,
  174. Subsystem: "volumeServer",
  175. Name: "volumes",
  176. Help: "Number of volumes or shards.",
  177. }, []string{"collection", "type"})
  178. VolumeServerReadOnlyVolumeGauge = prometheus.NewGaugeVec(
  179. prometheus.GaugeOpts{
  180. Namespace: Namespace,
  181. Subsystem: "volumeServer",
  182. Name: "read_only_volumes",
  183. Help: "Number of read only volumes.",
  184. }, []string{"collection", "type"})
  185. VolumeServerMaxVolumeCounter = prometheus.NewGauge(
  186. prometheus.GaugeOpts{
  187. Namespace: Namespace,
  188. Subsystem: "volumeServer",
  189. Name: "max_volumes",
  190. Help: "Maximum number of volumes.",
  191. })
  192. VolumeServerDiskSizeGauge = prometheus.NewGaugeVec(
  193. prometheus.GaugeOpts{
  194. Namespace: Namespace,
  195. Subsystem: "volumeServer",
  196. Name: "total_disk_size",
  197. Help: "Actual disk size used by volumes.",
  198. }, []string{"collection", "type"})
  199. VolumeServerResourceGauge = prometheus.NewGaugeVec(
  200. prometheus.GaugeOpts{
  201. Namespace: Namespace,
  202. Subsystem: "volumeServer",
  203. Name: "resource",
  204. Help: "Resource usage",
  205. }, []string{"name", "type"})
  206. S3RequestCounter = prometheus.NewCounterVec(
  207. prometheus.CounterOpts{
  208. Namespace: Namespace,
  209. Subsystem: "s3",
  210. Name: "request_total",
  211. Help: "Counter of s3 requests.",
  212. }, []string{"type", "code", "bucket"})
  213. S3RequestHistogram = prometheus.NewHistogramVec(
  214. prometheus.HistogramOpts{
  215. Namespace: Namespace,
  216. Subsystem: "s3",
  217. Name: "request_seconds",
  218. Help: "Bucketed histogram of s3 request processing time.",
  219. Buckets: prometheus.ExponentialBuckets(0.0001, 2, 24),
  220. }, []string{"type", "bucket"})
  221. S3TimeToFirstByteHistogram = prometheus.NewHistogramVec(
  222. prometheus.HistogramOpts{
  223. Namespace: Namespace,
  224. Subsystem: "s3",
  225. Name: "time_to_first_byte_millisecond",
  226. Help: "Bucketed histogram of s3 time to first byte request processing time.",
  227. Buckets: prometheus.ExponentialBuckets(0.001, 2, 27),
  228. }, []string{"type", "bucket"})
  229. )
  230. func init() {
  231. Gather.MustRegister(MasterClientConnectCounter)
  232. Gather.MustRegister(MasterRaftIsleader)
  233. Gather.MustRegister(MasterAdminLock)
  234. Gather.MustRegister(MasterReceivedHeartbeatCounter)
  235. Gather.MustRegister(MasterLeaderChangeCounter)
  236. Gather.MustRegister(MasterReplicaPlacementMismatch)
  237. Gather.MustRegister(MasterVolumeLayout)
  238. Gather.MustRegister(FilerRequestCounter)
  239. Gather.MustRegister(FilerHandlerCounter)
  240. Gather.MustRegister(FilerRequestHistogram)
  241. Gather.MustRegister(FilerStoreCounter)
  242. Gather.MustRegister(FilerStoreHistogram)
  243. Gather.MustRegister(FilerSyncOffsetGauge)
  244. Gather.MustRegister(FilerServerLastSendTsOfSubscribeGauge)
  245. Gather.MustRegister(collectors.NewGoCollector())
  246. Gather.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
  247. Gather.MustRegister(VolumeServerRequestCounter)
  248. Gather.MustRegister(VolumeServerHandlerCounter)
  249. Gather.MustRegister(VolumeServerRequestHistogram)
  250. Gather.MustRegister(VolumeServerVacuumingCompactCounter)
  251. Gather.MustRegister(VolumeServerVacuumingCommitCounter)
  252. Gather.MustRegister(VolumeServerVacuumingHistogram)
  253. Gather.MustRegister(VolumeServerVolumeCounter)
  254. Gather.MustRegister(VolumeServerMaxVolumeCounter)
  255. Gather.MustRegister(VolumeServerReadOnlyVolumeGauge)
  256. Gather.MustRegister(VolumeServerDiskSizeGauge)
  257. Gather.MustRegister(VolumeServerResourceGauge)
  258. Gather.MustRegister(S3RequestCounter)
  259. Gather.MustRegister(S3RequestHistogram)
  260. Gather.MustRegister(S3TimeToFirstByteHistogram)
  261. }
  262. func LoopPushingMetric(name, instance, addr string, intervalSeconds int) {
  263. if addr == "" || intervalSeconds == 0 {
  264. return
  265. }
  266. glog.V(0).Infof("%s server sends metrics to %s every %d seconds", name, addr, intervalSeconds)
  267. pusher := push.New(addr, name).Gatherer(Gather).Grouping("instance", instance)
  268. for {
  269. err := pusher.Push()
  270. if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
  271. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  272. }
  273. if intervalSeconds <= 0 {
  274. intervalSeconds = 15
  275. }
  276. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  277. }
  278. }
  279. func JoinHostPort(host string, port int) string {
  280. portStr := strconv.Itoa(port)
  281. if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
  282. return host + ":" + portStr
  283. }
  284. return net.JoinHostPort(host, portStr)
  285. }
  286. func StartMetricsServer(ip string, port int) {
  287. if port == 0 {
  288. return
  289. }
  290. http.Handle("/metrics", promhttp.HandlerFor(Gather, promhttp.HandlerOpts{}))
  291. log.Fatal(http.ListenAndServe(JoinHostPort(ip, port), nil))
  292. }
  293. func SourceName(port uint32) string {
  294. hostname, err := os.Hostname()
  295. if err != nil {
  296. return "unknown"
  297. }
  298. return net.JoinHostPort(hostname, strconv.Itoa(int(port)))
  299. }
  300. // todo - can be changed to DeletePartialMatch when https://github.com/prometheus/client_golang/pull/1013 gets released
  301. func DeleteCollectionMetrics(collection string) {
  302. VolumeServerDiskSizeGauge.DeleteLabelValues(collection, "normal")
  303. for _, volume_type := range readOnlyVolumeTypes {
  304. VolumeServerReadOnlyVolumeGauge.DeleteLabelValues(collection, volume_type)
  305. }
  306. VolumeServerVolumeCounter.DeleteLabelValues(collection, "volume")
  307. }