-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhealth.go
214 lines (189 loc) · 6.4 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
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
// Author: Adrián López (https://github.com/adrianlop)
// Copyright (c) Jeevanandam M. (https://github.com/jeevatkm)
// Source code and usage is governed by a MIT style
// license that can be found in the LICENSE file.
package health
import (
"fmt"
"net/http"
"path"
"sync"
"time"
aah "aahframe.work"
"aahframe.work/ainsp"
"aahframe.work/router"
)
var (
defaultCollector *Collector
)
// Reporter interface for a dependency that can be health-checked.
type Reporter interface {
// Check will return nil if dependency is reachable/healthy
// You should implement this func with a sensible timeout (< 3 or 5 sec)
Check() error
}
// Config struct contains a Reporter configuration
type Config struct {
Name string
Reporter Reporter
SoftFail bool // if true it will allow errors so won't report unhealthy
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// Collector struct and its methods
//______________________________________________________________________________
// Collector contains the health reporters to check and its responded
// data for the JSON response.
type Collector struct {
globalHealth bool
reporters map[string]*Config
reportersData map[string]string
mu sync.RWMutex
}
// NewCollector method returns a `Collector` instance. It periodically checks
// all its registered reporters.
func NewCollector(interval time.Duration) *Collector {
defaultCollector = &Collector{
reporters: make(map[string]*Config),
reportersData: make(map[string]string),
globalHealth: true,
}
if interval <= 0 {
// if interval is negative or 0, default to 10s interval checks
interval = 10
}
go func(interval time.Duration) {
//sleep 5s + do initial runChecks, so we don't wait 10s when app starts
time.Sleep(5 * time.Second)
defaultCollector.runChecks()
// ticker to check reporters periodically using specified interval
t := time.NewTicker(interval * time.Second)
for range t.C {
defaultCollector.runChecks()
}
}(interval)
return defaultCollector
}
// AddReporter method adds a dependency to health check reporter
// that will be called per interval to get health report.
func (c *Collector) AddReporter(config *Config) error {
c.mu.Lock()
defer c.mu.Unlock()
if _, exists := c.reporters[config.Name]; exists {
return fmt.Errorf("health: reporter name '%s' already exists", config.Name)
}
c.reporters[config.Name] = config
return nil
}
// RunChecks method performs a check in all the dependencies and update the global status
func (c *Collector) runChecks() {
//create syncgroup and check all dependencies
var wg sync.WaitGroup
wg.Add(len(c.reporters))
globalHealthy := true
for _, cfg := range c.reporters {
go func(rc *Config) {
defer wg.Done()
//change the dependency health values
if err := rc.Reporter.Check(); err != nil {
if !rc.SoftFail {
c.mu.Lock()
globalHealthy = false
c.mu.Unlock()
}
c.mu.Lock()
c.reportersData[rc.Name] = "KO: " + err.Error()
c.mu.Unlock()
} else {
c.mu.Lock()
c.reportersData[rc.Name] = "OK: Healthy"
c.mu.Unlock()
}
}(cfg)
}
// wait for all the deps to finish the checks
wg.Wait()
// update global health status
if globalHealthy {
c.mu.Lock()
c.globalHealth = true
c.mu.Unlock()
} else {
c.mu.Lock()
c.globalHealth = false
c.mu.Unlock()
}
}
// Register method registers the health collector into aah application with
// two routes `/healthcheck` and `/ping`.
//
// Provides optional base path or route prefix for the above routes.
func (c *Collector) Register(app *aah.Application, basePath ...string) error {
return c.RegisterForDomain(app, app.Router().RootDomain().Key, basePath...)
}
// RegisterForDomain method registers the health collector into
// aah application with two routes `/healthcheck` and `/ping` for
// given domain hostname.
//
// Provides optional base path or route prefix for the above routes.
func (c *Collector) RegisterForDomain(app *aah.Application, domainName string, basePath ...string) error {
routePrefix := ""
if len(basePath) > 0 {
routePrefix = basePath[0]
}
return registerInApp(app, domainName, routePrefix)
}
func registerInApp(app *aah.Application, domainName, basePath string) error {
app.AddController((*healthController)(nil), []*ainsp.Method{
{Name: "Healthcheck"},
{Name: "Ping"},
})
hcRoute := &router.Route{
Name: "healthcheck",
Path: composeRoutePath(basePath, "healthcheck"),
Method: http.MethodGet,
Target: "aahframe.work/ec/health/healthController",
Action: "Healthcheck",
Auth: "anonymous",
}
if err := app.Router().Lookup(domainName).AddRoute(hcRoute); err != nil {
return fmt.Errorf("health: cannot add route '%v': %v", hcRoute.Name, err.Error())
}
pingRoute := &router.Route{
Name: "ping",
Path: composeRoutePath(basePath, "ping"),
Method: http.MethodGet,
Target: "aahframe.work/ec/health/healthController",
Action: "Ping",
Auth: "anonymous",
}
if err := app.Router().Lookup(domainName).AddRoute(pingRoute); err != nil {
return fmt.Errorf("health: cannot add route '%v': %v", hcRoute.Name, err.Error())
}
return nil
}
func composeRoutePath(basePath, routePath string) string {
return path.Join("/", basePath, routePath)
}
//‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
// HealthController struct and its methods
//______________________________________________________________________________
// HealthController provides action methods for health check and ping
// for the aah application.
type healthController struct {
*aah.Context
}
// TODO: this action should take input parameter *Collector, to support multiple collectors
// Healthcheck action responds with reporter's health status.
func (c *healthController) Healthcheck() {
defaultCollector.mu.RLock()
defer defaultCollector.mu.RUnlock()
if defaultCollector.globalHealth {
c.Reply().Ok().JSON(defaultCollector.reportersData)
} else {
c.Reply().ServiceUnavailable().JSON(defaultCollector.reportersData)
}
}
// Ping action responds with static text response as `pong!` with status `200 OK`.
func (c *healthController) Ping() {
c.Reply().Ok().Text("pong!\n")
}