-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathhealth.go
102 lines (79 loc) · 1.84 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
package health
import "encoding/json"
type status string
const (
up status = "UP"
down = "DOWN"
outOfService = "OUT OF SERVICE"
unknown = "UNKNOWN"
)
// Health is a health status struct
type Health struct {
status status
info map[string]interface{}
}
// MarshalJSON is a custom JSON marshaller
func (h Health) MarshalJSON() ([]byte, error) {
data := map[string]interface{}{}
for k, v := range h.info {
data[k] = v
}
data["status"] = h.status
return json.Marshal(data)
}
// NewHealth return a new Health with status Down
func NewHealth() Health {
h := Health{
info: make(map[string]interface{}),
}
h.Unknown()
return h
}
// AddInfo adds a info value to the Info map
func (h *Health) AddInfo(key string, value interface{}) *Health {
if h.info == nil {
h.info = make(map[string]interface{})
}
h.info[key] = value
return h
}
// GetInfo returns a value from the info map
func (h Health) GetInfo(key string) interface{} {
return h.info[key]
}
// IsUnknown returns true if Status is Unknown
func (h Health) IsUnknown() bool {
return h.status == unknown
}
// IsUp returns true if Status is Up
func (h Health) IsUp() bool {
return h.status == up
}
// IsDown returns true if Status is Down
func (h Health) IsDown() bool {
return h.status == down
}
// IsOutOfService returns true if Status is IsOutOfService
func (h Health) IsOutOfService() bool {
return h.status == outOfService
}
// Down set the status to Down
func (h *Health) Down() *Health {
h.status = down
return h
}
// OutOfService set the status to OutOfService
func (h *Health) OutOfService() *Health {
h.status = outOfService
return h
}
// Unknown set the status to Unknown
func (h *Health) Unknown() *Health {
h.status = unknown
return h
}
// Up set the status to Up
func (h *Health) Up() *Health {
h.status = up
return h
}