/
status.go
343 lines (303 loc) · 8.99 KB
/
status.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright 2020 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package servenv
import (
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
"github.com/google/safehtml"
"github.com/google/safehtml/template"
"github.com/google/safehtml/template/uncheckedconversions"
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/vt/log"
)
// AddStatusPart adds a new section to status. fragment is used as a
// subtemplate of the template used to render /debug/status, and will
// be executed using the value of invoking f at the time of the
// /debug/status request. fragment is parsed and executed with the
// html/template package. Functions registered with AddStatusFuncs
// may be used in the template.
func AddStatusPart(banner, fragment string, f func() any) {
globalStatus.addStatusPart(banner, fragment, f)
}
// AddStatusFuncs merges the provided functions into the set of
// functions used to render /debug/status. Call this before AddStatusPart
// if your template requires custom functions.
func AddStatusFuncs(fmap template.FuncMap) {
globalStatus.addStatusFuncs(fmap)
}
// AddStatusSection registers a function that generates extra
// information for the global status page, it will be
// used as a header before the information. If more complex output
// than a simple string is required use AddStatusPart instead.
func AddStatusSection(banner string, f func() string) {
globalStatus.addStatusSection(banner, f)
}
// StatusURLPath returns the path to the status page.
func StatusURLPath() string {
return "/debug/status"
}
//-----------------------------------------------------------------
var (
binaryName = filepath.Base(os.Args[0])
hostname string
serverStart = time.Now()
blockProfileRate = 0
globalStatus = newStatusPage("")
)
var statusHTML = `<!DOCTYPE html>
<html>
<head>
<title>Status for {{.BinaryName}}</title>
<style>
body {
font-family: sans-serif;
}
h1 {
clear: both;
width: 100%;
text-align: center;
font-size: 120%;
background: #eef;
}
.lefthand {
float: left;
width: 80%;
}
.righthand {
text-align: right;
}
</style>
</head>
<h1>Status for {{.BinaryName}}</h1>
<script>
function refreshTablesHaveClassRefreshRequired() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "/debug/status", true);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
var data = this.responseText;
var parser = new DOMParser();
var htmlDoc = parser.parseFromString(data, "text/html");
var tables = document.getElementsByClassName("refreshRequired");
var counter = 0;
for (var i = 0; i < tables.length; i++) {
var newTable = htmlDoc.querySelectorAll("table.refreshRequired")[counter];
if (newTable) {
tables[i].innerHTML = newTable.innerHTML;
}
counter++;
}
}
};
xhr.send();
}
if (` + strconv.Itoa(tableRefreshInterval) + ` !== 0) {
setInterval(refreshTablesHaveClassRefreshRequired, ` + strconv.Itoa(tableRefreshInterval) + `);
}
</script>
<div>
<div class=lefthand>
Started: {{.StartTime}}<br>
</div>
<div class=righthand>
Running on {{.Hostname}}<br>
View: <a href=/debug/vars>variables</a>,
<a href=/debug/pprof>debugging profiles</a>
</div>
<br>
<div class=righthand>
Toggle (careful!): <a href=/debug/blockprofilerate>block profiling</a>,
<a href=/debug/mutexprofilefraction>mutex profiling</a>
</div>
</div>`
type statusPage struct {
mu sync.RWMutex
sections []section
tmpl *template.Template
funcMap template.FuncMap
}
type section struct {
Banner string
Fragment string
F func() any
}
func newStatusPage(name string) *statusPage {
sp := &statusPage{
funcMap: make(template.FuncMap),
}
sp.tmpl = template.Must(sp.reparse(nil))
if name == "" {
HTTPHandleFunc(StatusURLPath(), sp.statusHandler)
// Debug profiles are only supported for the top level status page.
registerDebugBlockProfileRate()
registerDebugMutexProfileFraction()
} else {
pat, _ := url.JoinPath("/", name, StatusURLPath())
HTTPHandleFunc(pat, sp.statusHandler)
}
return sp
}
func (sp *statusPage) reset() {
sp.mu.Lock()
defer sp.mu.Unlock()
sp.sections = nil
sp.tmpl = template.Must(sp.reparse(nil))
sp.funcMap = make(template.FuncMap)
}
func (sp *statusPage) addStatusFuncs(fmap template.FuncMap) {
sp.mu.Lock()
defer sp.mu.Unlock()
for name, fun := range fmap {
if !strings.HasPrefix(name, "github_com_vitessio_vitess_") {
panic("status func registered without proper prefix, need github_com_vitessio_vitess_:" + name)
}
if _, ok := sp.funcMap[name]; ok {
panic("duplicate status func registered: " + name)
}
sp.funcMap[name] = fun
}
}
func (sp *statusPage) addStatusPart(banner, fragment string, f func() any) {
sp.mu.Lock()
defer sp.mu.Unlock()
secs := append(sp.sections, section{
Banner: banner,
Fragment: fragment,
F: f,
})
var err error
sp.tmpl, err = sp.reparse(secs)
if err != nil {
secs[len(secs)-1] = section{
Banner: banner,
Fragment: "<code>bad status template: {{.}}</code>",
F: func() any { return err },
}
}
sp.tmpl, _ = sp.reparse(secs)
sp.sections = secs
}
func (sp *statusPage) addStatusSection(banner string, f func() string) {
sp.addStatusPart(banner, `{{.}}`, func() any { return f() })
}
func (sp *statusPage) statusHandler(w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
}
sp.mu.Lock()
defer sp.mu.Unlock()
data := struct {
Sections []section
BinaryName string
Hostname string
StartTime string
}{
Sections: sp.sections,
BinaryName: binaryName,
Hostname: hostname,
StartTime: serverStart.Format(time.RFC1123),
}
if err := sp.tmpl.ExecuteTemplate(w, "status", data); err != nil {
if _, ok := err.(net.Error); !ok {
log.Errorf("servenv: couldn't execute template: %v", err)
}
}
}
func (sp *statusPage) reparse(sections []section) (*template.Template, error) {
var buf strings.Builder
io.WriteString(&buf, `{{define "status"}}`)
io.WriteString(&buf, statusHTML)
for i, sec := range sections {
fmt.Fprintf(&buf, "<h1>%s</h1>\n", safehtml.HTMLEscaped(sec.Banner))
fmt.Fprintf(&buf, "{{$sec := index .Sections %d}}\n", i)
fmt.Fprintf(&buf, `{{template "sec-%d" call $sec.F}}`+"\n", i)
}
fmt.Fprintf(&buf, `</html>`)
io.WriteString(&buf, "{{end}}\n")
for i, sec := range sections {
fmt.Fprintf(&buf, `{{define "sec-%d"}}%s{{end}}\n`, i, sec.Fragment)
}
return template.New("").Funcs(sp.funcMap).ParseFromTrustedTemplate(uncheckedconversions.TrustedTemplateFromStringKnownToSatisfyTypeContract(buf.String()))
}
// Toggle the block profile rate to/from 100%, unless specific rate is passed in
func registerDebugBlockProfileRate() {
HTTPHandleFunc("/debug/blockprofilerate", func(w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
}
rate, err := strconv.Atoi(r.FormValue("rate"))
message := "block profiling enabled"
if rate < 0 || err != nil {
// We can't get the current profiling rate
// from runtime, so we depend on a global var
if blockProfileRate == 0 {
rate = 1
} else {
rate = 0
message = "block profiling disabled"
}
} else {
message = fmt.Sprintf("Block profiling rate set to %d", rate)
}
blockProfileRate = rate
runtime.SetBlockProfileRate(rate)
log.Infof("Set block profile rate to: %d", rate)
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, message)
})
}
// Toggle the mutex profiling fraction to/from 100%, unless specific fraction is passed in
func registerDebugMutexProfileFraction() {
HTTPHandleFunc("/debug/mutexprofilefraction", func(w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
}
currentFraction := runtime.SetMutexProfileFraction(-1)
fraction, err := strconv.Atoi(r.FormValue("fraction"))
message := "mutex profiling enabled"
if fraction < 0 || err != nil {
if currentFraction == 0 {
fraction = 1
} else {
fraction = 0
message = "mutex profiling disabled"
}
} else {
message = fmt.Sprintf("Mutex profiling set to fraction %d", fraction)
}
runtime.SetMutexProfileFraction(fraction)
log.Infof("Set mutex profiling fraction to: %d", fraction)
w.Header().Set("Content-Type", "text/plain")
io.WriteString(w, message)
})
}
func init() {
var err error
hostname, err = os.Hostname()
if err != nil {
log.Exitf("os.Hostname: %v", err)
}
}