| 1 | package handlers |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "net/http" |
| 8 | |
| 9 | "github.com/coreos/discovery.etcd.io/handlers/httperror" |
| 10 | "github.com/prometheus/client_golang/prometheus" |
| 11 | ) |
| 12 | |
| 13 | var healthCounter *prometheus.CounterVec |
| 14 | |
| 15 | func init() { |
| 16 | healthCounter = prometheus.NewCounterVec( |
| 17 | prometheus.CounterOpts{ |
| 18 | Name: "endpoint_health_requests_total", |
| 19 | Help: "How many /health requests processed, partitioned by status code and HTTP method.", |
| 20 | }, |
| 21 | []string{"code", "method"}, |
| 22 | ) |
| 23 | prometheus.MustRegister(healthCounter) |
| 24 | } |
| 25 | |
| 26 | func HealthHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) { |
| 27 | st := ctx.Value(stateKey).(*State) |
| 28 | |
| 29 | token, err := st.setupToken(0) |
| 30 | if err != nil || token == "" { |
| 31 | log.Printf("health failed to setupToken %v", err) |
| 32 | httperror.Error(w, r, "health failed to setupToken", 400, healthCounter) |
| 33 | return |
| 34 | } |
| 35 | |
| 36 | err = st.deleteToken(token) |
| 37 | if err != nil { |
| 38 | log.Printf("health failed to deleteToken %v", err) |
| 39 | httperror.Error(w, r, "health failed to deleteToken", 400, healthCounter) |
| 40 | return |
| 41 | } |
| 42 | |
| 43 | fmt.Fprintf(w, "OK") |
| 44 | healthCounter.WithLabelValues("200", r.Method).Add(1) |
| 45 | } |