forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
txlogz.go
106 lines (97 loc) · 2.49 KB
/
txlogz.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
// 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"
"html/template"
"io"
"net/http"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/acl"
)
var (
txlogzHeader = []byte(`<thead>
<tr>
<th>Transaction id</th>
<th>Start</th>
<th>End</th>
<th>Duration</th>
<th>Decision</th>
<th>Statements</th>
</tr>
</thead>
`)
txlogzFuncMap = template.FuncMap{
"stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) },
}
txlogzTmpl = template.Must(template.New("example").Funcs(txlogzFuncMap).Parse(`
<tr class="{{.ColorLevel}}">
<td>{{.TransactionID}}</td>
<td>{{.StartTime | stampMicro}}</td>
<td>{{.EndTime | stampMicro}}</td>
<td>{{.Duration}}</td>
<td>{{.Conclusion}}</td>
<td>
{{ range .Queries }}
{{.}}<br>
{{ end}}
</td>
</tr>`))
)
func init() {
http.HandleFunc("/txlogz", txlogzHandler)
}
// txlogzHandler serves a human readable snapshot of the
// current transaction log.
// Endpoint: /txlogz?timeout=%d&limit=%d
// timeout: the txlogz will keep dumping transactions until timeout
// limit: txlogz will keep dumping transcations until it hits the limit
func txlogzHandler(w http.ResponseWriter, req *http.Request) {
if err := acl.CheckAccessHTTP(req, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
}
timeout, limit := parseTimeoutLimitParams(req)
ch := TxLogger.Subscribe("txlogz")
defer TxLogger.Unsubscribe(ch)
startHTMLTable(w)
defer endHTMLTable(w)
w.Write(txlogzHeader)
tmr := time.NewTimer(timeout)
defer tmr.Stop()
for i := 0; i < limit; i++ {
select {
case out := <-ch:
txc, ok := out.(*TxConnection)
if !ok {
err := fmt.Errorf("Unexpected value in %s: %#v (expecting value of type %T)", TxLogger.Name(), out, &TxConnection{})
io.WriteString(w, `<tr class="error">`)
io.WriteString(w, err.Error())
io.WriteString(w, "</tr>")
log.Error(err)
continue
}
duration := txc.EndTime.Sub(txc.StartTime).Seconds()
var level string
if duration < 0.1 {
level = "low"
} else if duration < 1.0 {
level = "medium"
} else {
level = "high"
}
tmplData := struct {
*TxConnection
Duration float64
ColorLevel string
}{txc, duration, level}
if err := txlogzTmpl.Execute(w, tmplData); err != nil {
log.Errorf("txlogz: couldn't execute template: %v", err)
}
case <-tmr.C:
return
}
}
}