metrics.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "time"
  17. "github.com/coreos/etcd/pkg/runtime"
  18. "github.com/coreos/etcd/version"
  19. "github.com/prometheus/client_golang/prometheus"
  20. )
  21. var (
  22. hasLeader = prometheus.NewGauge(prometheus.GaugeOpts{
  23. Namespace: "etcd",
  24. Subsystem: "server",
  25. Name: "has_leader",
  26. Help: "Whether or not a leader exists. 1 is existence, 0 is not.",
  27. })
  28. isLeader = prometheus.NewGauge(prometheus.GaugeOpts{
  29. Namespace: "etcd",
  30. Subsystem: "server",
  31. Name: "is_leader",
  32. Help: "Whether or not this member is a leader. 1 if is, 0 otherwise.",
  33. })
  34. leaderChanges = prometheus.NewCounter(prometheus.CounterOpts{
  35. Namespace: "etcd",
  36. Subsystem: "server",
  37. Name: "leader_changes_seen_total",
  38. Help: "The number of leader changes seen.",
  39. })
  40. proposalsCommitted = prometheus.NewGauge(prometheus.GaugeOpts{
  41. Namespace: "etcd",
  42. Subsystem: "server",
  43. Name: "proposals_committed_total",
  44. Help: "The total number of consensus proposals committed.",
  45. })
  46. proposalsApplied = prometheus.NewGauge(prometheus.GaugeOpts{
  47. Namespace: "etcd",
  48. Subsystem: "server",
  49. Name: "proposals_applied_total",
  50. Help: "The total number of consensus proposals applied.",
  51. })
  52. proposalsPending = prometheus.NewGauge(prometheus.GaugeOpts{
  53. Namespace: "etcd",
  54. Subsystem: "server",
  55. Name: "proposals_pending",
  56. Help: "The current number of pending proposals to commit.",
  57. })
  58. proposalsFailed = prometheus.NewCounter(prometheus.CounterOpts{
  59. Namespace: "etcd",
  60. Subsystem: "server",
  61. Name: "proposals_failed_total",
  62. Help: "The total number of failed proposals seen.",
  63. })
  64. leaseExpired = prometheus.NewCounter(prometheus.CounterOpts{
  65. Namespace: "etcd_debugging",
  66. Subsystem: "server",
  67. Name: "lease_expired_total",
  68. Help: "The total number of expired leases.",
  69. })
  70. currentVersion = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  71. Namespace: "etcd",
  72. Subsystem: "server",
  73. Name: "version",
  74. Help: "Which version is running. 1 for 'server_version' label with current version.",
  75. },
  76. []string{"server_version"})
  77. )
  78. func init() {
  79. prometheus.MustRegister(hasLeader)
  80. prometheus.MustRegister(isLeader)
  81. prometheus.MustRegister(leaderChanges)
  82. prometheus.MustRegister(proposalsCommitted)
  83. prometheus.MustRegister(proposalsApplied)
  84. prometheus.MustRegister(proposalsPending)
  85. prometheus.MustRegister(proposalsFailed)
  86. prometheus.MustRegister(leaseExpired)
  87. prometheus.MustRegister(currentVersion)
  88. currentVersion.With(prometheus.Labels{
  89. "server_version": version.Version,
  90. }).Set(1)
  91. }
  92. func monitorFileDescriptor(done <-chan struct{}) {
  93. ticker := time.NewTicker(5 * time.Second)
  94. defer ticker.Stop()
  95. for {
  96. used, err := runtime.FDUsage()
  97. if err != nil {
  98. plog.Errorf("cannot monitor file descriptor usage (%v)", err)
  99. return
  100. }
  101. limit, err := runtime.FDLimit()
  102. if err != nil {
  103. plog.Errorf("cannot monitor file descriptor usage (%v)", err)
  104. return
  105. }
  106. if used >= limit/5*4 {
  107. plog.Warningf("80%% of the file descriptor limit is used [used = %d, limit = %d]", used, limit)
  108. }
  109. select {
  110. case <-ticker.C:
  111. case <-done:
  112. return
  113. }
  114. }
  115. }