File: health.go

package info (click to toggle)
golang-github-coreos-discovery-etcd-io 2.0.0%2Bgit2019.04.19.git.78fb45d3c9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 23,460 kB
  • sloc: asm: 452; sh: 94; ansic: 24; makefile: 16
file content (45 lines) | stat: -rw-r--r-- 1,108 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package handlers

import (
	"context"
	"fmt"
	"log"
	"net/http"

	"github.com/coreos/discovery.etcd.io/handlers/httperror"
	"github.com/prometheus/client_golang/prometheus"
)

var healthCounter *prometheus.CounterVec

func init() {
	healthCounter = prometheus.NewCounterVec(
		prometheus.CounterOpts{
			Name: "endpoint_health_requests_total",
			Help: "How many /health requests processed, partitioned by status code and HTTP method.",
		},
		[]string{"code", "method"},
	)
	prometheus.MustRegister(healthCounter)
}

func HealthHandler(ctx context.Context, w http.ResponseWriter, r *http.Request) {
	st := ctx.Value(stateKey).(*State)

	token, err := st.setupToken(0)
	if err != nil || token == "" {
		log.Printf("health failed to setupToken %v", err)
		httperror.Error(w, r, "health failed to setupToken", 400, healthCounter)
		return
	}

	err = st.deleteToken(token)
	if err != nil {
		log.Printf("health failed to deleteToken %v", err)
		httperror.Error(w, r, "health failed to deleteToken", 400, healthCounter)
		return
	}

	fmt.Fprintf(w, "OK")
	healthCounter.WithLabelValues("200", r.Method).Add(1)
}