-
Notifications
You must be signed in to change notification settings - Fork 52
/
health.go
76 lines (64 loc) · 1.94 KB
/
health.go
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package proxy
import (
"os"
"github.com/sirupsen/logrus"
"github.com/valyala/fasthttp"
"github.com/wallarm/api-firewall/internal/platform/proxy"
"github.com/wallarm/api-firewall/internal/platform/web"
)
type Health struct {
Build string
Logger *logrus.Logger
Pool proxy.Pool
}
// Readiness checks if the Fasthttp connection pool is ready to handle new requests.
func (h Health) Readiness(ctx *fasthttp.RequestCtx) error {
status := "ok"
statusCode := fasthttp.StatusOK
reverseProxy, err := h.Pool.Get()
if err != nil {
status = "not ready"
statusCode = fasthttp.StatusInternalServerError
}
if reverseProxy != nil {
if err := h.Pool.Put(reverseProxy); err != nil {
status = "not ready"
statusCode = fasthttp.StatusInternalServerError
}
}
data := struct {
Status string `json:"status"`
}{
Status: status,
}
return web.Respond(ctx, data, statusCode)
}
// Liveness returns simple status info if the service is alive. If the
// app is deployed to a Kubernetes cluster, it will also return pod, node, and
// namespace details via the Downward API. The Kubernetes environment variables
// need to be set within your Pod/Deployment manifest.
func (h Health) Liveness(ctx *fasthttp.RequestCtx) error {
host, err := os.Hostname()
if err != nil {
host = "unavailable"
}
data := struct {
Status string `json:"status,omitempty"`
Build string `json:"build,omitempty"`
Host string `json:"host,omitempty"`
Pod string `json:"pod,omitempty"`
PodIP string `json:"podIP,omitempty"`
Node string `json:"node,omitempty"`
Namespace string `json:"namespace,omitempty"`
}{
Status: "up",
Build: h.Build,
Host: host,
Pod: os.Getenv("KUBERNETES_PODNAME"),
PodIP: os.Getenv("KUBERNETES_NAMESPACE_POD_IP"),
Node: os.Getenv("KUBERNETES_NODENAME"),
Namespace: os.Getenv("KUBERNETES_NAMESPACE"),
}
statusCode := fasthttp.StatusOK
return web.Respond(ctx, data, statusCode)
}