-
Notifications
You must be signed in to change notification settings - Fork 781
/
http.go
138 lines (123 loc) · 3.24 KB
/
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
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
package ring
import (
"context"
"fmt"
"html/template"
"math"
"net/http"
"sort"
"time"
"github.com/go-kit/kit/log/level"
"github.com/gogo/protobuf/proto"
"github.com/cortexproject/cortex/pkg/util"
)
const tpl = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Cortex Ring Status</title>
</head>
<body>
<h1>Cortex Ring Status</h1>
<p>Current time: {{ .Now }}</p>
<form action="" method="POST">
<input type="hidden" name="csrf_token" value="$__CSRF_TOKEN_PLACEHOLDER__">
<table width="100%" border="1">
<thead>
<tr>
<th>Ingester</th>
<th>State</th>
<th>Address</th>
<th>Last Heartbeat</th>
<th>Tokens</th>
<th>Ownership</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
{{ range .Ingesters }}
<tr>
<td>{{ .ID }}</td>
<td>{{ .State }}</td>
<td>{{ .Address }}</td>
<td>{{ .Timestamp }}</td>
<td>{{ .Tokens }}</td>
<td>{{ .Ownership }}%</td>
<td><button name="forget" value="{{ .ID }}" type="submit">Forget</button></td>
</tr>
{{ end }}
</tbody>
</table>
<pre>{{ .Ring }}</pre>
</form>
</body>
</html>`
var tmpl *template.Template
func init() {
tmpl = template.Must(template.New("webpage").Parse(tpl))
}
func (r *Ring) forget(ctx context.Context, id string) error {
unregister := func(in interface{}) (out interface{}, retry bool, err error) {
if in == nil {
return nil, false, fmt.Errorf("found empty ring when trying to unregister")
}
ringDesc := in.(*Desc)
ringDesc.RemoveIngester(id)
return ringDesc, true, nil
}
return r.KVClient.CAS(ctx, ConsulKey, unregister)
}
func (r *Ring) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodPost {
ingesterID := req.FormValue("forget")
if err := r.forget(req.Context(), ingesterID); err != nil {
level.Error(util.WithContext(req.Context(), util.Logger)).Log("msg", "error forgetting ingester", "err", err)
}
// Implement PRG pattern to prevent double-POST and work with CSRF middleware.
// https://en.wikipedia.org/wiki/Post/Redirect/Get
http.Redirect(w, req, req.RequestURI, http.StatusFound)
return
}
r.mtx.RLock()
defer r.mtx.RUnlock()
ingesterIDs := []string{}
for id := range r.ringDesc.Ingesters {
ingesterIDs = append(ingesterIDs, id)
}
sort.Strings(ingesterIDs)
ingesters := []interface{}{}
tokens, owned := countTokens(r.ringDesc)
for _, id := range ingesterIDs {
ing := r.ringDesc.Ingesters[id]
timestamp := time.Unix(ing.Timestamp, 0)
state := ing.State.String()
if !r.IsHealthy(&ing, Reporting) {
state = unhealthy
}
ingesters = append(ingesters, struct {
ID, State, Address, Timestamp string
Tokens uint32
Ownership float64
}{
ID: id,
State: state,
Address: ing.Addr,
Timestamp: timestamp.String(),
Tokens: tokens[id],
Ownership: (float64(owned[id]) / float64(math.MaxUint32)) * 100,
})
}
if err := tmpl.Execute(w, struct {
Ingesters []interface{}
Now time.Time
Ring string
}{
Ingesters: ingesters,
Now: time.Now(),
Ring: proto.MarshalTextString(r.ringDesc),
}); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}