/
ha_tracker_http.go
109 lines (96 loc) · 2.56 KB
/
ha_tracker_http.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
package ha
import (
"html/template"
"net/http"
"sort"
"strings"
"time"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/cortexproject/cortex/pkg/util"
)
const trackerTpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>{{ .Config.Title }}</title>
</head>
<body>
<h1>{{ .Config.Title }}</h1>
<p>Current time: {{ .Now }}</p>
<table width="100%" border="1">
<thead>
<tr>
<th>User ID</th>
<th>{{ .Config.ReplicaGroupLabel }}</th>
<th>Replica</th>
<th>Elected Time</th>
<th>Time Until Update</th>
<th>Time Until Failover</th>
</tr>
</thead>
<tbody>
{{ range .Elected }}
<tr>
<td>{{ .UserID }}</td>
<td>{{ .Cluster }}</td>
<td>{{ .Replica }}</td>
<td>{{ .ElectedAt }}</td>
<td>{{ .UpdateTime }}</td>
<td>{{ .FailoverTime }}</td>
</tr>
{{ end }}
</tbody>
</table>
</body>
</html>`
var trackerTmpl *template.Template
// nolint:revive
type HATrackerStatusConfig struct {
Title string
ReplicaGroupLabel string
}
func init() {
trackerTmpl = template.Must(template.New("ha-tracker").Parse(trackerTpl))
}
func (h *HATracker) ServeHTTP(w http.ResponseWriter, req *http.Request) {
h.electedLock.RLock()
type replica struct {
UserID string `json:"userID"`
Cluster string `json:"cluster"`
Replica string `json:"replica"`
ElectedAt time.Time `json:"electedAt"`
UpdateTime time.Duration `json:"updateDuration"`
FailoverTime time.Duration `json:"failoverDuration"`
}
electedReplicas := []replica{}
for key, desc := range h.elected {
chunks := strings.SplitN(key, "/", 2)
electedReplicas = append(electedReplicas, replica{
UserID: chunks[0],
Cluster: chunks[1],
Replica: desc.Replica,
ElectedAt: timestamp.Time(desc.ReceivedAt),
UpdateTime: time.Until(timestamp.Time(desc.ReceivedAt).Add(h.cfg.UpdateTimeout)),
FailoverTime: time.Until(timestamp.Time(desc.ReceivedAt).Add(h.cfg.FailoverTimeout)),
})
}
h.electedLock.RUnlock()
sort.Slice(electedReplicas, func(i, j int) bool {
first := electedReplicas[i]
second := electedReplicas[j]
if first.UserID != second.UserID {
return first.UserID < second.UserID
}
return first.Cluster < second.Cluster
})
util.RenderHTTPResponse(w, struct {
Elected []replica `json:"elected"`
Now time.Time `json:"now"`
Config HATrackerStatusConfig `json:"config"`
}{
Elected: electedReplicas,
Now: time.Now(),
Config: h.trackerStatusConfig,
}, trackerTmpl, req)
}