forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth_check.go
240 lines (186 loc) · 5.02 KB
/
health_check.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package gateway
import (
"context"
"encoding/json"
"errors"
"net/http"
"sync"
"sync/atomic"
"time"
"github.com/TykTechnologies/tyk/rpc"
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/headers"
"github.com/TykTechnologies/tyk/storage"
"github.com/sirupsen/logrus"
)
type (
HealthCheckStatus string
HealthCheckComponentType string
)
const (
Pass HealthCheckStatus = "pass"
Fail = "fail"
Warn = "warn"
Component HealthCheckComponentType = "component"
Datastore = "datastore"
System = "system"
)
var (
healthCheckInfo atomic.Value
healthCheckLock sync.Mutex
)
func setCurrentHealthCheckInfo(h map[string]HealthCheckItem) {
healthCheckLock.Lock()
healthCheckInfo.Store(h)
healthCheckLock.Unlock()
}
func getHealthCheckInfo() map[string]HealthCheckItem {
healthCheckLock.Lock()
ret := healthCheckInfo.Load().(map[string]HealthCheckItem)
healthCheckLock.Unlock()
return ret
}
type HealthCheckResponse struct {
Status HealthCheckStatus `json:"status"`
Version string `json:"version,omitempty"`
Output string `json:"output,omitempty"`
Description string `json:"description,omitempty"`
Details map[string]HealthCheckItem `json:"details,omitempty"`
}
type HealthCheckItem struct {
Status HealthCheckStatus `json:"status"`
Output string `json:"output,omitempty"`
ComponentType string `json:"componentType,omitempty"`
ComponentID string `json:"componentId,omitempty"`
Time string `json:"time"`
}
func initHealthCheck(ctx context.Context) {
setCurrentHealthCheckInfo(make(map[string]HealthCheckItem, 3))
go func(ctx context.Context) {
var n = config.Global().LivenessCheck.CheckDuration
if n == 0 {
n = 10
}
ticker := time.NewTicker(time.Second * n)
for {
select {
case <-ctx.Done():
ticker.Stop()
mainLog.WithFields(logrus.Fields{
"prefix": "health-check",
}).Debug("Stopping Health checks for all components")
return
case <-ticker.C:
gatherHealthChecks()
}
}
}(ctx)
}
type SafeHealthCheck struct {
info map[string]HealthCheckItem
mux sync.Mutex
}
func gatherHealthChecks() {
allInfos := SafeHealthCheck{info: make(map[string]HealthCheckItem, 3)}
redisStore := storage.RedisCluster{KeyPrefix: "livenesscheck-"}
key := "tyk-liveness-probe"
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
var checkItem = HealthCheckItem{
Status: Pass,
ComponentType: Datastore,
Time: time.Now().Format(time.RFC3339),
}
err := redisStore.SetRawKey(key, key, 10)
if err != nil {
mainLog.WithField("liveness-check", true).WithError(err).Error("Redis health check failed")
checkItem.Output = err.Error()
checkItem.Status = Fail
}
allInfos.mux.Lock()
allInfos.info["redis"] = checkItem
allInfos.mux.Unlock()
}()
if config.Global().UseDBAppConfigs {
wg.Add(1)
go func() {
defer wg.Done()
var checkItem = HealthCheckItem{
Status: Pass,
ComponentType: Datastore,
Time: time.Now().Format(time.RFC3339),
}
if DashService == nil {
err := errors.New("Dashboard service not initialized")
mainLog.WithField("liveness-check", true).Error(err)
checkItem.Output = err.Error()
checkItem.Status = Fail
} else if err := DashService.Ping(); err != nil {
mainLog.WithField("liveness-check", true).Error(err)
checkItem.Output = err.Error()
checkItem.Status = Fail
}
checkItem.ComponentType = System
allInfos.mux.Lock()
allInfos.info["dashboard"] = checkItem
allInfos.mux.Unlock()
}()
}
if config.Global().Policies.PolicySource == "rpc" {
wg.Add(1)
go func() {
defer wg.Done()
var checkItem = HealthCheckItem{
Status: Pass,
ComponentType: Datastore,
Time: time.Now().Format(time.RFC3339),
}
if !rpc.Login() {
checkItem.Output = "Could not connect to RPC"
checkItem.Status = Fail
}
checkItem.ComponentType = System
allInfos.mux.Lock()
allInfos.info["rpc"] = checkItem
allInfos.mux.Unlock()
}()
}
wg.Wait()
allInfos.mux.Lock()
setCurrentHealthCheckInfo(allInfos.info)
allInfos.mux.Unlock()
}
func liveCheckHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
doJSONWrite(w, http.StatusMethodNotAllowed, apiError(http.StatusText(http.StatusMethodNotAllowed)))
return
}
checks := getHealthCheckInfo()
res := HealthCheckResponse{
Status: Pass,
Version: VERSION,
Description: "Tyk GW",
Details: checks,
}
var failCount int
for _, v := range checks {
if v.Status == Fail {
failCount++
}
}
var status HealthCheckStatus
switch failCount {
case 0:
status = Pass
case len(checks):
status = Fail
default:
status = Warn
}
res.Status = status
w.Header().Set("Content-Type", headers.ApplicationJSON)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(res)
}