metrics.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. )
  25. const (
  26. pathMetrics = "/metrics"
  27. PathHealth = "/health"
  28. )
  29. // HandleMetricsHealth registers metrics and health handlers.
  30. func HandleMetricsHealth(mux *http.ServeMux, srv etcdserver.ServerV2) {
  31. mux.Handle(pathMetrics, prometheus.Handler())
  32. mux.Handle(PathHealth, NewHealthHandler(func() Health { return checkHealth(srv) }))
  33. }
  34. // HandlePrometheus registers prometheus handler on '/metrics'.
  35. func HandlePrometheus(mux *http.ServeMux) {
  36. mux.Handle(pathMetrics, prometheus.Handler())
  37. }
  38. // HandleHealth registers health handler on '/health'.
  39. func HandleHealth(mux *http.ServeMux, srv etcdserver.ServerV2) {
  40. mux.Handle(PathHealth, NewHealthHandler(func() Health { return checkHealth(srv) }))
  41. }
  42. // NewHealthHandler handles '/health' requests.
  43. func NewHealthHandler(hfunc func() Health) http.HandlerFunc {
  44. return func(w http.ResponseWriter, r *http.Request) {
  45. if r.Method != http.MethodGet {
  46. w.Header().Set("Allow", http.MethodGet)
  47. http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
  48. return
  49. }
  50. h := hfunc()
  51. d, _ := json.Marshal(h)
  52. if !h.Health {
  53. http.Error(w, string(d), http.StatusServiceUnavailable)
  54. return
  55. }
  56. w.WriteHeader(http.StatusOK)
  57. w.Write(d)
  58. }
  59. }
  60. // Health defines etcd server health status.
  61. // TODO: remove manual parsing in etcdctl cluster-health
  62. type Health struct {
  63. Health bool `json:"health"`
  64. Errors []string `json:"errors,omitempty"`
  65. }
  66. func checkHealth(srv etcdserver.ServerV2) Health {
  67. h := Health{Health: false}
  68. as := srv.Alarms()
  69. if len(as) > 0 {
  70. for _, v := range as {
  71. h.Errors = append(h.Errors, v.Alarm.String())
  72. }
  73. return h
  74. }
  75. if uint64(srv.Leader()) == raft.None {
  76. h.Errors = append(h.Errors, etcdserver.ErrNoLeader.Error())
  77. return h
  78. }
  79. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  80. _, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"})
  81. cancel()
  82. if err != nil {
  83. h.Errors = append(h.Errors, err.Error())
  84. }
  85. h.Health = err == nil
  86. return h
  87. }