-
Notifications
You must be signed in to change notification settings - Fork 0
/
etcd.go
69 lines (59 loc) · 1.68 KB
/
etcd.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
package healthindicator
import (
"context"
"time"
actuator "github.com/megaease/actuator-go/actuator"
clientv3 "go.etcd.io/etcd/client/v3"
)
// EtcdHealthIndicator checks the health of an etcd cluster.
type EtcdHealthIndicator struct {
etcdClient *clientv3.Client
}
// NewEtcdHealthIndicator creates a new EtcdHealthIndicator with the provided etcd client.
func NewEtcdHealthIndicator(etcdClient *clientv3.Client) *EtcdHealthIndicator {
return &EtcdHealthIndicator{
etcdClient: etcdClient,
}
}
// Health checks the health of the etcd cluster and returns the health status.
func (e *EtcdHealthIndicator) Health(withDetails bool) actuator.Health {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Initialize health response
health := actuator.Health{
Status: "UP",
Details: nil,
}
// Perform a status check on each etcd endpoint
details := make(map[string]interface{})
for _, endpoint := range e.etcdClient.Endpoints() {
resp, err := e.etcdClient.Status(ctx, endpoint)
if err != nil {
health.Status = "DOWN"
if withDetails {
details[endpoint] = map[string]string{
"error": err.Error(),
}
}
continue
}
if withDetails {
details[endpoint] = map[string]interface{}{
"version": resp.Version,
"dbSize": resp.DbSize,
"leader": resp.Leader,
"raftIndex": resp.RaftIndex,
"raftTerm": resp.RaftTerm,
"isLearner": resp.IsLearner,
"memberId": resp.Header.MemberId,
"raftAppliedIndex": resp.RaftAppliedIndex,
}
}
}
if withDetails {
health.Details = &actuator.HealthDetails{
"endpoints": details,
}
}
return health
}