forked from vitessio/vitess
/
txlogz.go
95 lines (88 loc) · 1.91 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
// 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"
"net/http"
"strconv"
"strings"
"time"
)
var (
txlogzHeader = []byte(`
<tr>
<th>Transaction id</th>
<th>Start</th>
<th>End</th>
<th>Duration</th>
<th>Decision</th>
<th>Statements</th>
</tr>
`)
txlogzTmpl = template.Must(template.New("example").Parse(`
<tr class="{{.Color}}">
<td>{{.Tid}}</td>
<td>{{.Start}}</td>
<td>{{.End}}</td>
<td>{{.Duration}}</td>
<td>{{.Decision}}</td>
<td>
{{ range .Statements }}
{{.}}<br>
{{ end}}
</td>
</tr>
`))
)
type txlogzRow struct {
Tid string
Start, End string
Duration string
Decision string
Statements []string
Color string
}
func init() {
http.HandleFunc("/txlogz", txlogzHandler)
}
// txlogzHandler serves a human readable snapshot of the
// current transaction log.
func txlogzHandler(w http.ResponseWriter, r *http.Request) {
ch := TxLogger.Subscribe(nil)
defer TxLogger.Unsubscribe(ch)
startHTMLTable(w)
defer endHTMLTable(w)
w.Write(txlogzHeader)
deadline := time.After(10 * time.Second)
for i := 0; i < 300; i++ {
select {
case out := <-ch:
strs := strings.Split(strings.Trim(out, "\n"), "\t")
if len(strs) < 6 {
txlogzTmpl.Execute(w, &txlogzRow{Tid: fmt.Sprintf("Short: %d", len(strs))})
continue
}
Value := &txlogzRow{
Tid: strs[0],
Start: strs[1],
End: strs[2],
Duration: strs[3],
Decision: strs[4],
Statements: strings.Split(strs[5], ";"),
}
duration, _ := strconv.ParseFloat(Value.Duration, 64)
if duration < 0.1 {
Value.Color = "low"
} else if duration < 1.0 {
Value.Color = "medium"
} else {
Value.Color = "high"
}
txlogzTmpl.Execute(w, Value)
case <-deadline:
return
}
}
}