metrics.go 3.4 KB

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