metrics.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. "github.com/coreos/etcd/etcdserver"
  21. "github.com/coreos/etcd/etcdserver/etcdserverpb"
  22. "github.com/coreos/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. // NewHealthHandler handles '/health' requests.
  40. func NewHealthHandler(hfunc func() Health) http.HandlerFunc {
  41. return func(w http.ResponseWriter, r *http.Request) {
  42. if r.Method != http.MethodGet {
  43. w.Header().Set("Allow", http.MethodGet)
  44. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  45. return
  46. }
  47. h := hfunc()
  48. d, _ := json.Marshal(h)
  49. if h.Health != "true" {
  50. http.Error(w, string(d), http.StatusServiceUnavailable)
  51. return
  52. }
  53. w.WriteHeader(http.StatusOK)
  54. w.Write(d)
  55. }
  56. }
  57. var (
  58. healthSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  59. Namespace: "etcd",
  60. Subsystem: "server",
  61. Name: "health_success",
  62. Help: "The total number of successful health checks",
  63. })
  64. healthFailed = prometheus.NewCounter(prometheus.CounterOpts{
  65. Namespace: "etcd",
  66. Subsystem: "server",
  67. Name: "health_failures",
  68. Help: "The total number of failed health checks",
  69. })
  70. )
  71. func init() {
  72. prometheus.MustRegister(healthSuccess)
  73. prometheus.MustRegister(healthFailed)
  74. }
  75. // Health defines etcd server health status.
  76. // TODO: remove manual parsing in etcdctl cluster-health
  77. type Health struct {
  78. Health string `json:"health"`
  79. }
  80. // TODO: server NOSPACE, etcdserver.ErrNoLeader in health API
  81. func checkHealth(srv etcdserver.ServerV2) Health {
  82. h := Health{Health: "true"}
  83. as := srv.Alarms()
  84. if len(as) > 0 {
  85. h.Health = "false"
  86. }
  87. if h.Health == "true" {
  88. if uint64(srv.Leader()) == raft.None {
  89. h.Health = "false"
  90. }
  91. }
  92. if h.Health == "true" {
  93. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  94. _, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"})
  95. cancel()
  96. if err != nil {
  97. h.Health = "false"
  98. }
  99. }
  100. if h.Health == "true" {
  101. healthSuccess.Inc()
  102. } else {
  103. healthFailed.Inc()
  104. }
  105. return h
  106. }