metrics.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "fmt"
  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.EtcdServer) {
  31. mux.Handle(pathMetrics, prometheus.Handler())
  32. mux.Handle(pathHealth, newHealthHandler(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.EtcdServer) {
  40. mux.Handle(pathHealth, newHealthHandler(srv))
  41. }
  42. // newHealthHandler handles '/health' requests.
  43. func newHealthHandler(srv *etcdserver.EtcdServer) 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 := checkHealth(srv)
  51. d := []byte(fmt.Sprintf(`{"health": "%v"}`, h.Health))
  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. type health struct {
  61. Health bool `json:"health"`
  62. }
  63. func checkHealth(srv *etcdserver.EtcdServer) health {
  64. h := health{Health: false}
  65. if len(srv.Alarms()) > 0 {
  66. // TODO: provide alarm lists
  67. return h
  68. }
  69. if uint64(srv.Leader()) == raft.None {
  70. return h
  71. }
  72. ctx, cancel := context.WithTimeout(context.Background(), time.Second)
  73. _, err := srv.Do(ctx, etcdserverpb.Request{Method: "QGET"})
  74. cancel()
  75. h.Health = err == nil
  76. return h
  77. }