metrics.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. S3TimeToFirstByteHistogram = prometheus.NewHistogramVec(
  201. prometheus.HistogramOpts{
  202. Namespace: Namespace,
  203. Subsystem: "s3",
  204. Name: "time_to_first_byte_millisecond",
  205. Help: "Bucketed histogram of s3 time to first byte request processing time.",
  206. Buckets: prometheus.ExponentialBuckets(0.001, 2, 27),
  207. }, []string{"type", "bucket"})
  208. )
  209. func init() {
  210. Gather.MustRegister(MasterClientConnectCounter)
  211. Gather.MustRegister(MasterRaftIsleader)
  212. Gather.MustRegister(MasterAdminLock)
  213. Gather.MustRegister(MasterReceivedHeartbeatCounter)
  214. Gather.MustRegister(MasterLeaderChangeCounter)
  215. Gather.MustRegister(MasterReplicaPlacementMismatch)
  216. Gather.MustRegister(FilerRequestCounter)
  217. Gather.MustRegister(FilerRequestHistogram)
  218. Gather.MustRegister(FilerStoreCounter)
  219. Gather.MustRegister(FilerStoreHistogram)
  220. Gather.MustRegister(FilerSyncOffsetGauge)
  221. Gather.MustRegister(FilerServerLastSendTsOfSubscribeGauge)
  222. Gather.MustRegister(collectors.NewGoCollector())
  223. Gather.MustRegister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
  224. Gather.MustRegister(VolumeServerRequestCounter)
  225. Gather.MustRegister(VolumeServerRequestHistogram)
  226. Gather.MustRegister(VolumeServerVacuumingCompactCounter)
  227. Gather.MustRegister(VolumeServerVacuumingCommitCounter)
  228. Gather.MustRegister(VolumeServerVacuumingHistogram)
  229. Gather.MustRegister(VolumeServerVolumeCounter)
  230. Gather.MustRegister(VolumeServerMaxVolumeCounter)
  231. Gather.MustRegister(VolumeServerReadOnlyVolumeGauge)
  232. Gather.MustRegister(VolumeServerDiskSizeGauge)
  233. Gather.MustRegister(VolumeServerResourceGauge)
  234. Gather.MustRegister(S3RequestCounter)
  235. Gather.MustRegister(S3RequestHistogram)
  236. Gather.MustRegister(S3TimeToFirstByteHistogram)
  237. }
  238. func LoopPushingMetric(name, instance, addr string, intervalSeconds int) {
  239. if addr == "" || intervalSeconds == 0 {
  240. return
  241. }
  242. glog.V(0).Infof("%s server sends metrics to %s every %d seconds", name, addr, intervalSeconds)
  243. pusher := push.New(addr, name).Gatherer(Gather).Grouping("instance", instance)
  244. for {
  245. err := pusher.Push()
  246. if err != nil && !strings.HasPrefix(err.Error(), "unexpected status code 200") {
  247. glog.V(0).Infof("could not push metrics to prometheus push gateway %s: %v", addr, err)
  248. }
  249. if intervalSeconds <= 0 {
  250. intervalSeconds = 15
  251. }
  252. time.Sleep(time.Duration(intervalSeconds) * time.Second)
  253. }
  254. }
  255. func JoinHostPort(host string, port int) string {
  256. portStr := strconv.Itoa(port)
  257. if strings.HasPrefix(host, "[") && strings.HasSuffix(host, "]") {
  258. return host + ":" + portStr
  259. }
  260. return net.JoinHostPort(host, portStr)
  261. }
  262. func StartMetricsServer(ip string, port int) {
  263. if port == 0 {
  264. return
  265. }
  266. http.Handle("/metrics", promhttp.HandlerFor(Gather, promhttp.HandlerOpts{}))
  267. log.Fatal(http.ListenAndServe(JoinHostPort(ip, port), nil))
  268. }
  269. func SourceName(port uint32) string {
  270. hostname, err := os.Hostname()
  271. if err != nil {
  272. return "unknown"
  273. }
  274. return net.JoinHostPort(hostname, strconv.Itoa(int(port)))
  275. }
  276. // todo - can be changed to DeletePartialMatch when https://github.com/prometheus/client_golang/pull/1013 gets released
  277. func DeleteCollectionMetrics(collection string) {
  278. VolumeServerDiskSizeGauge.DeleteLabelValues(collection, "normal")
  279. for _, volume_type := range readOnlyVolumeTypes {
  280. VolumeServerReadOnlyVolumeGauge.DeleteLabelValues(collection, volume_type)
  281. }
  282. VolumeServerVolumeCounter.DeleteLabelValues(collection, "volume")
  283. }