metrics.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package etcdserver
  15. import (
  16. goruntime "runtime"
  17. "time"
  18. "go.etcd.io/etcd/pkg/runtime"
  19. "go.etcd.io/etcd/version"
  20. "github.com/prometheus/client_golang/prometheus"
  21. "go.uber.org/zap"
  22. )
  23. var (
  24. hasLeader = prometheus.NewGauge(prometheus.GaugeOpts{
  25. Namespace: "etcd",
  26. Subsystem: "server",
  27. Name: "has_leader",
  28. Help: "Whether or not a leader exists. 1 is existence, 0 is not.",
  29. })
  30. isLeader = prometheus.NewGauge(prometheus.GaugeOpts{
  31. Namespace: "etcd",
  32. Subsystem: "server",
  33. Name: "is_leader",
  34. Help: "Whether or not this member is a leader. 1 if is, 0 otherwise.",
  35. })
  36. leaderChanges = prometheus.NewCounter(prometheus.CounterOpts{
  37. Namespace: "etcd",
  38. Subsystem: "server",
  39. Name: "leader_changes_seen_total",
  40. Help: "The number of leader changes seen.",
  41. })
  42. isLearner = prometheus.NewGauge(prometheus.GaugeOpts{
  43. Namespace: "etcd",
  44. Subsystem: "server",
  45. Name: "is_learner",
  46. Help: "Whether or not this member is a learner. 1 if is, 0 otherwise.",
  47. })
  48. learnerPromoteFailed = prometheus.NewCounterVec(prometheus.CounterOpts{
  49. Namespace: "etcd",
  50. Subsystem: "server",
  51. Name: "learner_promote_failures",
  52. Help: "The total number of failed learner promotions (likely learner not ready) while this member is leader.",
  53. },
  54. []string{"Reason"},
  55. )
  56. learnerPromoteSucceed = prometheus.NewCounter(prometheus.CounterOpts{
  57. Namespace: "etcd",
  58. Subsystem: "server",
  59. Name: "learner_promote_successes",
  60. Help: "The total number of successful learner promotions while this member is leader.",
  61. })
  62. heartbeatSendFailures = prometheus.NewCounter(prometheus.CounterOpts{
  63. Namespace: "etcd",
  64. Subsystem: "server",
  65. Name: "heartbeat_send_failures_total",
  66. Help: "The total number of leader heartbeat send failures (likely overloaded from slow disk).",
  67. })
  68. slowApplies = prometheus.NewCounter(prometheus.CounterOpts{
  69. Namespace: "etcd",
  70. Subsystem: "server",
  71. Name: "slow_apply_total",
  72. Help: "The total number of slow apply requests (likely overloaded from slow disk).",
  73. })
  74. applySnapshotInProgress = prometheus.NewGauge(prometheus.GaugeOpts{
  75. Namespace: "etcd",
  76. Subsystem: "server",
  77. Name: "snapshot_apply_in_progress_total",
  78. Help: "1 if the server is applying the incoming snapshot. 0 if none.",
  79. })
  80. proposalsCommitted = prometheus.NewGauge(prometheus.GaugeOpts{
  81. Namespace: "etcd",
  82. Subsystem: "server",
  83. Name: "proposals_committed_total",
  84. Help: "The total number of consensus proposals committed.",
  85. })
  86. proposalsApplied = prometheus.NewGauge(prometheus.GaugeOpts{
  87. Namespace: "etcd",
  88. Subsystem: "server",
  89. Name: "proposals_applied_total",
  90. Help: "The total number of consensus proposals applied.",
  91. })
  92. proposalsPending = prometheus.NewGauge(prometheus.GaugeOpts{
  93. Namespace: "etcd",
  94. Subsystem: "server",
  95. Name: "proposals_pending",
  96. Help: "The current number of pending proposals to commit.",
  97. })
  98. proposalsFailed = prometheus.NewCounter(prometheus.CounterOpts{
  99. Namespace: "etcd",
  100. Subsystem: "server",
  101. Name: "proposals_failed_total",
  102. Help: "The total number of failed proposals seen.",
  103. })
  104. slowReadIndex = prometheus.NewCounter(prometheus.CounterOpts{
  105. Namespace: "etcd",
  106. Subsystem: "server",
  107. Name: "slow_read_indexes_total",
  108. Help: "The total number of pending read indexes not in sync with leader's or timed out read index requests.",
  109. })
  110. readIndexFailed = prometheus.NewCounter(prometheus.CounterOpts{
  111. Namespace: "etcd",
  112. Subsystem: "server",
  113. Name: "read_indexes_failed_total",
  114. Help: "The total number of failed read indexes seen.",
  115. })
  116. leaseExpired = prometheus.NewCounter(prometheus.CounterOpts{
  117. Namespace: "etcd_debugging",
  118. Subsystem: "server",
  119. Name: "lease_expired_total",
  120. Help: "The total number of expired leases.",
  121. })
  122. quotaBackendBytes = prometheus.NewGauge(prometheus.GaugeOpts{
  123. Namespace: "etcd",
  124. Subsystem: "server",
  125. Name: "quota_backend_bytes",
  126. Help: "Current backend storage quota size in bytes.",
  127. })
  128. currentVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  129. Namespace: "etcd",
  130. Subsystem: "server",
  131. Name: "version",
  132. Help: "Which version is running. 1 for 'server_version' label with current version.",
  133. },
  134. []string{"server_version"})
  135. currentGoVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  136. Namespace: "etcd",
  137. Subsystem: "server",
  138. Name: "go_version",
  139. Help: "Which Go version server is running with. 1 for 'server_go_version' label with current version.",
  140. },
  141. []string{"server_go_version"})
  142. serverID = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  143. Namespace: "etcd",
  144. Subsystem: "server",
  145. Name: "id",
  146. Help: "Server or member ID in hexadecimal format. 1 for 'server_id' label with current ID.",
  147. },
  148. []string{"server_id"})
  149. )
  150. func init() {
  151. prometheus.MustRegister(hasLeader)
  152. prometheus.MustRegister(isLeader)
  153. prometheus.MustRegister(leaderChanges)
  154. prometheus.MustRegister(heartbeatSendFailures)
  155. prometheus.MustRegister(slowApplies)
  156. prometheus.MustRegister(applySnapshotInProgress)
  157. prometheus.MustRegister(proposalsCommitted)
  158. prometheus.MustRegister(proposalsApplied)
  159. prometheus.MustRegister(proposalsPending)
  160. prometheus.MustRegister(proposalsFailed)
  161. prometheus.MustRegister(slowReadIndex)
  162. prometheus.MustRegister(readIndexFailed)
  163. prometheus.MustRegister(leaseExpired)
  164. prometheus.MustRegister(quotaBackendBytes)
  165. prometheus.MustRegister(currentVersion)
  166. prometheus.MustRegister(currentGoVersion)
  167. prometheus.MustRegister(serverID)
  168. prometheus.MustRegister(isLearner)
  169. prometheus.MustRegister(learnerPromoteSucceed)
  170. prometheus.MustRegister(learnerPromoteFailed)
  171. currentVersion.With(prometheus.Labels{
  172. "server_version": version.Version,
  173. }).Set(1)
  174. currentGoVersion.With(prometheus.Labels{
  175. "server_go_version": goruntime.Version(),
  176. }).Set(1)
  177. }
  178. func monitorFileDescriptor(lg *zap.Logger, done <-chan struct{}) {
  179. ticker := time.NewTicker(5 * time.Second)
  180. defer ticker.Stop()
  181. for {
  182. used, err := runtime.FDUsage()
  183. if err != nil {
  184. if lg != nil {
  185. lg.Warn("failed to get file descriptor usage", zap.Error(err))
  186. } else {
  187. plog.Errorf("cannot monitor file descriptor usage (%v)", err)
  188. }
  189. return
  190. }
  191. limit, err := runtime.FDLimit()
  192. if err != nil {
  193. if lg != nil {
  194. lg.Warn("failed to get file descriptor limit", zap.Error(err))
  195. } else {
  196. plog.Errorf("cannot monitor file descriptor usage (%v)", err)
  197. }
  198. return
  199. }
  200. if used >= limit/5*4 {
  201. if lg != nil {
  202. lg.Warn("80% of file descriptors are used", zap.Uint64("used", used), zap.Uint64("limit", limit))
  203. } else {
  204. plog.Warningf("80%% of the file descriptor limit is used [used = %d, limit = %d]", used, limit)
  205. }
  206. }
  207. select {
  208. case <-ticker.C:
  209. case <-done:
  210. return
  211. }
  212. }
  213. }