forked from vitessio/vitess
/
querylogz.go
159 lines (150 loc) · 3.99 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
159
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tabletserver
import (
"fmt"
"io"
"net/http"
"strconv"
"strings"
"text/template"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/acl"
"github.com/youtube/vitess/go/vt/logz"
"github.com/youtube/vitess/go/vt/vttablet/tabletserver/tabletenv"
"github.com/youtube/vitess/go/vt/utils"
)
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>MySQL time</th>
<th>Conn wait</th>
<th>Plan</th>
<th>SQL</th>
<th>Queries</th>
<th>Sources</th>
<th>RowsAffected</th>
<th>Response Size</th>
<th>Transaction ID</th>
<th>Error</th>
</tr>
</thead>
`)
querylogzFuncMap = template.FuncMap{
"stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) },
"cssWrappable": logz.Wrappable,
"truncateQuery": utils.TruncateQuery,
"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>{{.MysqlResponseTime.Seconds}}</td>
<td>{{.WaitingForConnection.Seconds}}</td>
<td>{{.PlanType}}</td>
<td>{{.OriginalSQL | truncateQuery | unquote | cssWrappable}}</td>
<td>{{.NumberOfQueries}}</td>
<td>{{.FmtQuerySources}}</td>
<td>{{.RowsAffected}}</td>
<td>{{.SizeOfResponse}}</td>
<td>{{.TransactionID}}</td>
<td>{{.ErrorStr}}</td>
</tr>
`))
)
func init() {
http.HandleFunc("/querylogz", func(w http.ResponseWriter, r *http.Request) {
ch := tabletenv.StatsLogger.Subscribe("querylogz")
defer tabletenv.StatsLogger.Unsubscribe(ch)
querylogzHandler(ch, w, r)
})
}
// 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.(*tabletenv.LogStats)
if !ok {
err := fmt.Errorf("Unexpected value in %s: %#v (expecting value of type %T)", tabletenv.TxLogger.Name(), out, &tabletenv.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 {
*tabletenv.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
}