-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
querylogz.go
158 lines (147 loc) · 3.94 KB
/
querylogz.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
/*
Copyright 2019 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 vtgate
import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"text/template"
"time"
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logz"
"vitess.io/vitess/go/vt/sqlparser"
)
var (
querylogzHeader = []byte(`
<thead>
<tr>
<th>Method</th>
<th>Context</th>
<th>Effective Caller</th>
<th>Immediate Caller</th>
<th>Start</th>
<th>End</th>
<th>Duration</th>
<th>Plan Time</th>
<th>Execute Time</th>
<th>Commit Time</th>
<th>Stmt Type</th>
<th>SQL</th>
<th>ShardQueries</th>
<th>RowsAffected</th>
<th>Error</th>
</tr>
</thead>
`)
querylogzFuncMap = template.FuncMap{
"stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) },
"cssWrappable": logz.Wrappable,
"truncateQuery": sqlparser.TruncateForUI,
"unquote": func(s string) string { return strings.Trim(s, "\"") },
}
querylogzTmpl = template.Must(template.New("example").Funcs(querylogzFuncMap).Parse(`
<tr class="{{.ColorLevel}}">
<td>{{.Method}}</td>
<td>{{.ContextHTML}}</td>
<td>{{.EffectiveCaller}}</td>
<td>{{.ImmediateCaller}}</td>
<td>{{.StartTime | stampMicro}}</td>
<td>{{.EndTime | stampMicro}}</td>
<td>{{.TotalTime.Seconds}}</td>
<td>{{.PlanTime.Seconds}}</td>
<td>{{.ExecuteTime.Seconds}}</td>
<td>{{.CommitTime.Seconds}}</td>
<td>{{.StmtType}}</td>
<td>{{.SQL | truncateQuery | unquote | cssWrappable}}</td>
<td>{{.ShardQueries}}</td>
<td>{{.RowsAffected}}</td>
<td>{{.ErrorStr}}</td>
</tr>
`))
)
// querylogzHandler serves a human readable snapshot of the
// current query log.
func querylogzHandler(ch chan interface{}, w http.ResponseWriter, r *http.Request) {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
}
timeout, limit := parseTimeoutLimitParams(r)
logz.StartHTMLTable(w)
defer logz.EndHTMLTable(w)
w.Write(querylogzHeader)
tmr := time.NewTimer(timeout)
defer tmr.Stop()
for i := 0; i < limit; i++ {
select {
case out := <-ch:
select {
case <-tmr.C:
return
default:
}
stats, ok := out.(*LogStats)
if !ok {
err := fmt.Errorf("unexpected value in %s: %#v (expecting value of type %T)", QueryLogger.Name(), out, &LogStats{})
_, _ = io.WriteString(w, `<tr class="error">`)
_, _ = io.WriteString(w, err.Error())
_, _ = io.WriteString(w, "</tr>")
log.Error(err)
continue
}
var level string
if stats.TotalTime().Seconds() < 0.01 {
level = "low"
} else if stats.TotalTime().Seconds() < 0.1 {
level = "medium"
} else {
level = "high"
}
tmplData := struct {
*LogStats
ColorLevel string
}{stats, level}
if err := querylogzTmpl.Execute(w, tmplData); err != nil {
log.Errorf("querylogz: couldn't execute template: %v", err)
}
case <-tmr.C:
return
}
}
}
func parseTimeoutLimitParams(req *http.Request) (time.Duration, int) {
timeout := 10
limit := 300
if ts, ok := req.URL.Query()["timeout"]; ok {
if t, err := strconv.Atoi(ts[0]); err == nil {
timeout = adjustValue(t, 0, 60)
}
}
if l, ok := req.URL.Query()["limit"]; ok {
if lim, err := strconv.Atoi(l[0]); err == nil {
limit = adjustValue(lim, 1, 200000)
}
}
return time.Duration(timeout) * time.Second, limit
}
func adjustValue(val int, lower int, upper int) int {
if val < lower {
return lower
} else if val > upper {
return upper
}
return val
}