metrics.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2017 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 etcdhttp
  15. import (
  16. "context"
  17. "encoding/json"
  18. "net/http"
  19. "time"
  20. "go.etcd.io/etcd/etcdserver"
  21. "go.etcd.io/etcd/etcdserver/etcdserverpb"
  22. "go.etcd.io/etcd/raft"
  23. "github.com/prometheus/client_golang/prometheus"
  24. "github.com/prometheus/client_golang/prometheus/promhttp"
  25. )
  26. const (
  27. pathMetrics = "/metrics"
  28. PathHealth = "/health"
  29. )
  30. // HandleMetricsHealth registers metrics and health handlers.
  31. func HandleMetricsHealth(mux *http.ServeMux, srv etcdserver.ServerV2) {
  32. mux.Handle(pathMetrics, promhttp.Handler())
  33. mux.Handle(PathHealth, NewHealthHandler(func() Health { return checkHealth(srv) }))
  34. }
  35. // HandlePrometheus registers prometheus handler on '/metrics'.
  36. func HandlePrometheus(mux *http.ServeMux) {
  37. mux.Handle(pathMetrics, promhttp.Handler())
  38. }
  39. // HandleHealth registers health handler on '/health'.
  40. func HandleHealth(mux *http.ServeMux, srv etcdserver.ServerV2) {
  41. mux.Handle(PathHealth, NewHealthHandler(func() Health { return checkHealth(srv) }))
  42. }
  43. // NewHealthHandler handles '/health' requests.
  44. func NewHealthHandler(hfunc func() Health) http.HandlerFunc {
  45. return func(w http.ResponseWriter, r *http.Request) {
  46. if r.Method != http.MethodGet {
  47. w.Header().Set("Allow", http.MethodGet)
  48. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  49. return
  50. }
  51. h := hfunc()
  52. d, _ := json.Marshal(h)
  53. if h.Health != "true" {
  54. http.Error(w, string(d), http.StatusServiceUnavailable)
  55. return
  56. }
  57. w.WriteHeader(http.StatusOK)
  58. w.Write(d)
  59. }
  60. }
  61. var (
  62. healthSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  63. Namespace: "etcd",
  64. Subsystem: "server",
  65. Name: "health_success",
  66. Help: "The total number of successful health checks",
  67. })
  68. healthFailed = prometheus.NewCounter(prometheus.CounterOpts{
  69. Namespace: "etcd",
  70. Subsystem: "server",
  71. Name: "health_failures",
  72. Help: "The total number of failed health checks",
  73. })
  74. )
  75. func init() {
  76. prometheus.MustRegister(healthSuccess)
  77. prometheus.MustRegister(healthFailed)
  78. }
  79. // Health defines etcd server health status.
  80. // TODO: remove manual parsing in etcdctl cluster-health
  81. type Health struct {
  82. Health string `json:"health"`
  83. }
  84. // TODO: server NOSPACE, etcdserver.ErrNoLeader in health API
  85. func checkHealth(srv etcdserver.ServerV2) Health {
  86. h := Health{Health: "true"}
  87. as := srv.Alarms()
  88. if len(as) > 0 {
  89. h.Health = "false"
  90. }
  91. if h.Health == "true" {
  92. if uint64(srv.Leader()) == raft.None {
  93. h.Health = "false"
  94. }
  95. }
  96. if h.Health == "true" {
  97. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  98. _, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"})
  99. cancel()
  100. if err != nil {
  101. h.Health = "false"
  102. }
  103. }
  104. if h.Health == "true" {
  105. healthSuccess.Inc()
  106. } else {
  107. healthFailed.Inc()
  108. }
  109. return h
  110. }