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