|
1 | 1 | package healthcheck
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "encoding/json" |
5 |
| - "fmt" |
6 |
| - "io" |
7 |
| - "net/http" |
8 |
| - "os" |
9 |
| - "strings" |
10 |
| - |
11 |
| - "github.com/go-ping/ping" |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/go-ping/ping" |
12 | 12 | )
|
13 | 13 |
|
14 | 14 | // HTTP the request as done by routing
|
15 | 15 | func HTTP(w http.ResponseWriter, r *http.Request) {
|
16 |
| - hc := HealthCheck{ |
17 |
| - Name: os.Getenv("SERVICE_NAME"), |
18 |
| - URL: r.Host, |
19 |
| - Dependencies: os.Getenv("SERVICE_DEPENDENCIES"), |
20 |
| - } |
21 |
| - |
22 |
| - health, err := hc.Check() |
23 |
| - if err != nil { |
24 |
| - w.Header().Set("Content-Type", "application/health+json") |
25 |
| - j, _ := json.Marshal(Health{ |
26 |
| - Status: HealthFail, |
27 |
| - }) |
28 |
| - w.WriteHeader(http.StatusOK) |
29 |
| - if _, err := w.Write(j); err != nil { |
30 |
| - fmt.Printf("write response: %v\n", err) |
31 |
| - } |
32 |
| - fmt.Printf("http health failed: %+v\n", err) |
33 |
| - return |
34 |
| - } |
35 |
| - |
36 |
| - j, _ := json.Marshal(health) |
37 |
| - w.Header().Set("Content-Type", "application/health+json") |
38 |
| - if _, err := w.Write(j); err != nil { |
39 |
| - fmt.Printf("write response: %v\n", err) |
40 |
| - } |
| 16 | + hc := HealthCheck{ |
| 17 | + Name: os.Getenv("SERVICE_NAME"), |
| 18 | + URL: r.Host, |
| 19 | + Dependencies: os.Getenv("SERVICE_DEPENDENCIES"), |
| 20 | + } |
| 21 | + |
| 22 | + health, err := hc.Check() |
| 23 | + if err != nil { |
| 24 | + w.Header().Set("Content-Type", "application/health+json") |
| 25 | + j, _ := json.Marshal(Health{ |
| 26 | + Status: HealthFail, |
| 27 | + }) |
| 28 | + w.WriteHeader(http.StatusOK) |
| 29 | + if _, err := w.Write(j); err != nil { |
| 30 | + fmt.Printf("write response: %v\n", err) |
| 31 | + } |
| 32 | + fmt.Printf("http health failed: %+v\n", err) |
| 33 | + return |
| 34 | + } |
| 35 | + |
| 36 | + j, _ := json.Marshal(health) |
| 37 | + w.Header().Set("Content-Type", "application/health+json") |
| 38 | + if _, err := w.Write(j); err != nil { |
| 39 | + fmt.Printf("write response: %v\n", err) |
| 40 | + } |
41 | 41 | }
|
42 | 42 |
|
43 | 43 | // Check do the health check itself
|
44 | 44 | func (h HealthCheck) Check() (Health, error) {
|
45 |
| - health := Health{ |
46 |
| - Name: h.Name, |
47 |
| - URL: h.URL, |
48 |
| - Status: HealthFail, |
49 |
| - Dependencies: nil, |
50 |
| - } |
51 |
| - |
52 |
| - health.Status = HealthPass |
53 |
| - if h.Dependencies != "" { |
54 |
| - deps, err := h.getDependencies() |
55 |
| - if err != nil { |
56 |
| - return health, err |
57 |
| - } |
58 |
| - |
59 |
| - checkedDeps := []Health{} |
60 |
| - for _, dep := range deps.Dependencies { |
61 |
| - d, err := dep.check() |
62 |
| - if err != nil { |
63 |
| - return health, err |
64 |
| - } |
65 |
| - checkedDeps = append(checkedDeps, d) |
66 |
| - } |
67 |
| - |
68 |
| - health.Dependencies = checkedDeps |
69 |
| - } |
70 |
| - |
71 |
| - // now set to failed if a dependency failed |
72 |
| - for _, dep := range health.Dependencies { |
73 |
| - if dep.Status == HealthFail { |
74 |
| - health.Status = HealthFail |
75 |
| - } |
76 |
| - } |
77 |
| - |
78 |
| - return health, nil |
| 45 | + health := Health{ |
| 46 | + Name: h.Name, |
| 47 | + URL: h.URL, |
| 48 | + Status: HealthFail, |
| 49 | + Dependencies: nil, |
| 50 | + } |
| 51 | + |
| 52 | + health.Status = HealthPass |
| 53 | + if h.Dependencies != "" { |
| 54 | + deps, err := h.getDependencies() |
| 55 | + if err != nil { |
| 56 | + return health, err |
| 57 | + } |
| 58 | + |
| 59 | + checkedDeps := []Health{} |
| 60 | + for _, dep := range deps.Dependencies { |
| 61 | + d, err := dep.check() |
| 62 | + if err != nil { |
| 63 | + return health, err |
| 64 | + } |
| 65 | + checkedDeps = append(checkedDeps, d) |
| 66 | + } |
| 67 | + |
| 68 | + health.Dependencies = checkedDeps |
| 69 | + } |
| 70 | + |
| 71 | + // now set to failed if a dependency failed |
| 72 | + for _, dep := range health.Dependencies { |
| 73 | + if dep.Status == HealthFail { |
| 74 | + health.Status = HealthFail |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return health, nil |
79 | 79 | }
|
80 | 80 |
|
81 | 81 | // getDependencies get the list of dependencies
|
82 | 82 | func (h HealthCheck) getDependencies() (Dependencies, error) {
|
83 |
| - deps := Dependencies{} |
84 |
| - if err := json.Unmarshal([]byte(h.Dependencies), &deps); err != nil { |
85 |
| - return deps, err |
86 |
| - } |
| 83 | + deps := Dependencies{} |
| 84 | + if err := json.Unmarshal([]byte(h.Dependencies), &deps); err != nil { |
| 85 | + return deps, err |
| 86 | + } |
87 | 87 |
|
88 |
| - return deps, nil |
| 88 | + return deps, nil |
89 | 89 | }
|
90 | 90 |
|
91 | 91 | // check the dependency status
|
92 | 92 | func (d Dependency) check() (Health, error) {
|
93 |
| - if strings.Contains(d.URL, "$") { |
94 |
| - d.URL = os.Getenv(d.URL[1:]) |
95 |
| - } |
| 93 | + if strings.Contains(d.URL, "$") { |
| 94 | + d.URL = os.Getenv(d.URL[1:]) |
| 95 | + } |
96 | 96 |
|
97 |
| - // Ping check |
98 |
| - if d.Ping { |
99 |
| - return d.ping() |
100 |
| - } |
| 97 | + // Ping check |
| 98 | + if d.Ping { |
| 99 | + return d.ping() |
| 100 | + } |
101 | 101 |
|
102 |
| - // Standard check |
103 |
| - return d.curl() |
| 102 | + // Standard check |
| 103 | + return d.curl() |
104 | 104 | }
|
105 | 105 |
|
106 | 106 | // ping checks
|
107 | 107 | func (d Dependency) ping() (Health, error) {
|
108 |
| - h := Health{ |
109 |
| - Name: d.Name, |
110 |
| - URL: d.URL, |
111 |
| - Status: HealthFail, |
112 |
| - } |
113 |
| - |
114 |
| - pinger, err := ping.NewPinger(h.URL) |
115 |
| - if err != nil { |
116 |
| - return h, err |
117 |
| - } |
118 |
| - |
119 |
| - pinger.Count = 3 |
120 |
| - if err := pinger.Run(); err != nil { |
121 |
| - return h, err |
122 |
| - } |
123 |
| - |
124 |
| - h.Status = HealthPass |
125 |
| - return h, nil |
| 108 | + h := Health{ |
| 109 | + Name: d.Name, |
| 110 | + URL: d.URL, |
| 111 | + Status: HealthFail, |
| 112 | + } |
| 113 | + |
| 114 | + pinger, err := ping.NewPinger(h.URL) |
| 115 | + if err != nil { |
| 116 | + return h, err |
| 117 | + } |
| 118 | + |
| 119 | + pinger.Count = 3 |
| 120 | + if err := pinger.Run(); err != nil { |
| 121 | + return h, err |
| 122 | + } |
| 123 | + |
| 124 | + h.Status = HealthPass |
| 125 | + return h, nil |
126 | 126 | }
|
127 | 127 |
|
128 | 128 | // curl checks
|
129 | 129 | func (d Dependency) curl() (Health, error) {
|
130 |
| - h := Health{} |
131 |
| - p, err := http.Get(d.URL) |
132 |
| - if err != nil { |
133 |
| - h = Health{ |
134 |
| - URL: d.URL, |
135 |
| - Status: HealthFail, |
136 |
| - } |
137 |
| - return h, err |
138 |
| - } |
139 |
| - |
140 |
| - defer func() { |
141 |
| - _ = p.Body.Close() |
142 |
| - }() |
143 |
| - |
144 |
| - b, err := io.ReadAll(p.Body) |
145 |
| - if err != nil { |
146 |
| - h = Health{ |
147 |
| - URL: d.URL, |
148 |
| - Status: HealthFail, |
149 |
| - } |
150 |
| - return h, err |
151 |
| - } |
152 |
| - if err := json.Unmarshal(b, &h); err != nil { |
153 |
| - return h, err |
154 |
| - } |
155 |
| - return h, nil |
| 130 | + h := Health{} |
| 131 | + p, err := http.Get(d.URL) |
| 132 | + if err != nil { |
| 133 | + h = Health{ |
| 134 | + URL: d.URL, |
| 135 | + Status: HealthFail, |
| 136 | + } |
| 137 | + return h, err |
| 138 | + } |
| 139 | + |
| 140 | + defer func() { |
| 141 | + _ = p.Body.Close() |
| 142 | + }() |
| 143 | + |
| 144 | + b, err := io.ReadAll(p.Body) |
| 145 | + if err != nil { |
| 146 | + h = Health{ |
| 147 | + URL: d.URL, |
| 148 | + Status: HealthFail, |
| 149 | + } |
| 150 | + return h, err |
| 151 | + } |
| 152 | + if err := json.Unmarshal(b, &h); err != nil { |
| 153 | + return h, err |
| 154 | + } |
| 155 | + return h, nil |
156 | 156 | }
|
0 commit comments