-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck.go
156 lines (133 loc) · 2.81 KB
/
healthcheck.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package healthcheck
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/go-ping/ping"
)
// HTTP the request as done by routing
func HTTP(w http.ResponseWriter, r *http.Request) {
hc := HealthCheck{
Name: os.Getenv("SERVICE_NAME"),
URL: r.Host,
Dependencies: os.Getenv("SERVICE_DEPENDENCIES"),
}
health, err := hc.Check()
if err != nil {
w.Header().Set("Content-Type", "application/health+json")
j, _ := json.Marshal(Health{
Status: HealthFail,
})
w.WriteHeader(http.StatusOK)
if _, err := w.Write(j); err != nil {
fmt.Printf("write response: %v\n", err)
}
fmt.Printf("http health failed: %+v\n", err)
return
}
j, _ := json.Marshal(health)
w.Header().Set("Content-Type", "application/health+json")
if _, err := w.Write(j); err != nil {
fmt.Printf("write response: %v\n", err)
}
}
// Check do the health check itself
func (h HealthCheck) Check() (Health, error) {
health := Health{
Name: h.Name,
URL: h.URL,
Status: HealthFail,
Dependencies: nil,
}
health.Status = HealthPass
if h.Dependencies != "" {
deps, err := h.getDependencies()
if err != nil {
return health, err
}
checkedDeps := []Health{}
for _, dep := range deps.Dependencies {
d, err := dep.check()
if err != nil {
return health, err
}
checkedDeps = append(checkedDeps, d)
}
health.Dependencies = checkedDeps
}
// now set to failed if a dependency failed
for _, dep := range health.Dependencies {
if dep.Status == HealthFail {
health.Status = HealthFail
}
}
return health, nil
}
// getDependencies get the list of dependencies
func (h HealthCheck) getDependencies() (Dependencies, error) {
deps := Dependencies{}
if err := json.Unmarshal([]byte(h.Dependencies), &deps); err != nil {
return deps, err
}
return deps, nil
}
// check the dependency status
func (d Dependency) check() (Health, error) {
if strings.Contains(d.URL, "$") {
d.URL = os.Getenv(d.URL[1:])
}
// Ping check
if d.Ping {
return d.ping()
}
// Standard check
return d.curl()
}
// ping checks
func (d Dependency) ping() (Health, error) {
h := Health{
Name: d.Name,
URL: d.URL,
Status: HealthFail,
}
pinger, err := ping.NewPinger(h.URL)
if err != nil {
return h, err
}
pinger.Count = 3
if err := pinger.Run(); err != nil {
return h, err
}
h.Status = HealthPass
return h, nil
}
// curl checks
func (d Dependency) curl() (Health, error) {
h := Health{}
p, err := http.Get(d.URL)
if err != nil {
h = Health{
URL: d.URL,
Status: HealthFail,
}
return h, err
}
defer func() {
_ = p.Body.Close()
}()
b, err := io.ReadAll(p.Body)
if err != nil {
h = Health{
URL: d.URL,
Status: HealthFail,
}
return h, err
}
if err := json.Unmarshal(b, &h); err != nil {
return h, err
}
return h, nil
}