forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
throttlerlogz.go
162 lines (145 loc) · 3.93 KB
/
throttlerlogz.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
package throttler
import (
"fmt"
"html/template"
"io"
"net/http"
"strings"
"time"
"github.com/youtube/vitess/go/vt/logz"
)
const logHeaderHTML = `
<style>
table.gridtable th {
/* Override the nowrap default to avoid that the table overflows. */
white-space: normal;
}
</style>
<thead>
<tr>
<th>Now</th>
<th>Rate Change</th>
<th>Old Rate</th>
<th>New Rate</th>
<th>Tablet</th>
<th>Lag</th>
<th>Last Change</th>
<th>Actual Rate</th>
<th>Good/​Bad?</th>
<th>If Skipped</th>
<th>Highest Good</th>
<th>Lowest Bad</th>
<th>Old State</th>
<th>Tested State</th>
<th>New State</th>
<th>Lag Before</th>
<th>Recorded Ago</th>
<th>Master Rate</th>
<th>Slave Rate</th>
<th>Old Backlog</th>
<th>New Backlog</th>
<th>Reason</th>
</tr>
</thead>
`
const logEntryHTML = `
<tr class="{{.ColorLevel}}">
<td>{{.Now.Format "15:04:05"}}</td>
<td>{{.RateChange}}</td>
<td>{{.OldRate}}</td>
<td>{{.NewRate}}</td>
<td>{{.Alias}}</td>
<td>{{.LagRecordNow.Stats.SecondsBehindMaster}}s</td>
<td>{{.TimeSinceLastRateChange}}</td>
<td>{{.CurrentRate}}</td>
<td>{{.GoodOrBad}}</td>
<td>{{.MemorySkipReason}}</td>
<td>{{.HighestGood}}</td>
<td>{{.LowestBad}}</td>
<td>{{.OldState}}</td>
<td>{{.TestedState}}</td>
<td>{{.NewState}}</td>
<td>{{.LagBefore}}</td>
<td>{{.AgeOfBeforeLag}}</td>
<td>{{.MasterRate}}</td>
<td>{{.GuessedSlaveRate}}</td>
<td>{{.GuessedSlaveBacklogOld}}</td>
<td>{{.GuessedSlaveBacklogNew}}</td>
<td>{{.Reason}}</td>
</tr>
`
const logFooterHTML = `
{{.Count}} lag records spanning the last {{.TimeSpan}} minutes are displayed.
`
var (
logEntryTemplate = template.Must(template.New("logEntry").Parse(logEntryHTML))
logFooterTemplate = template.Must(template.New("logFooter").Parse(logFooterHTML))
)
func init() {
http.HandleFunc("/throttlerlogz/", func(w http.ResponseWriter, r *http.Request) {
throttlerlogzHandler(w, r, GlobalManager)
})
}
func throttlerlogzHandler(w http.ResponseWriter, r *http.Request, m *managerImpl) {
// Longest supported URL: /throttlerlogz/<name>
parts := strings.SplitN(r.URL.Path, "/", 3)
if len(parts) != 3 {
errMsg := fmt.Sprintf("invalid /throttlerlogz path: %q expected paths: /throttlerlogz/ or /throttlerlogz/<throttler name>", r.URL.Path)
http.Error(w, errMsg, http.StatusInternalServerError)
return
}
name := parts[2]
if name == "" {
// If no name is given, redirect to the list of throttlers at /throttlerz.
http.Redirect(w, r, "/throttlerz", http.StatusTemporaryRedirect)
return
}
showThrottlerLog(w, m, name)
}
func showThrottlerLog(w http.ResponseWriter, m *managerImpl, name string) {
results, err := m.Log(name)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
logz.StartHTMLTable(w)
if _, err := io.WriteString(w, logHeaderHTML); err != nil {
panic(fmt.Sprintf("failed to execute logHeader template: %v", err))
}
for _, r := range results {
// Color based on max(tested state, new state).
state := r.TestedState
if stateGreater(r.NewState, state) {
state = r.NewState
}
var colorLevel string
switch state {
case stateIncreaseRate:
colorLevel = "low"
case stateDecreaseAndGuessRate:
colorLevel = "medium"
case stateEmergency:
colorLevel = "high"
}
data := struct {
result
ColorLevel string
}{r, colorLevel}
if err := logEntryTemplate.Execute(w, data); err != nil {
panic(fmt.Sprintf("failed to execute logEntry template: %v", err))
}
}
logz.EndHTMLTable(w)
// Print footer.
count := len(results)
var d time.Duration
if count > 0 {
d = results[0].Now.Sub(results[count-1].Now)
}
if err := logFooterTemplate.Execute(w, map[string]interface{}{
"Count": count,
"TimeSpan": fmt.Sprintf("%.1f", d.Minutes()),
}); err != nil {
panic(fmt.Sprintf("failed to execute logFooter template: %v", err))
}
}