-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vrlog.go
125 lines (110 loc) · 3.19 KB
/
vrlog.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
/*
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.
*/
/*
* A human readable streaming log of vreplication events for vttablets available at /debug/vrlog
*/
package vreplication
import (
"net/http"
"strconv"
"text/template"
"time"
"vitess.io/vitess/go/streamlog"
"vitess.io/vitess/go/vt/log"
)
var (
vrLogStatsLogger = streamlog.New("VReplication", 50)
vrLogStatsTemplate = template.Must(template.New("vrlog").
Parse("{{.Type}} Event {{.Detail}} {{.LogTime}} {{.DurationNs}}\n"))
)
// VrLogStats collects attributes of a vreplication event for logging
type VrLogStats struct {
Type string
Detail string
StartTime time.Time
LogTime string
DurationNs int64
}
// NewVrLogStats should be called at the start of the event to be logged
func NewVrLogStats(eventType string) *VrLogStats {
return &VrLogStats{Type: eventType, StartTime: time.Now()}
}
// Send records the log event, should be called on a stats object constructed by NewVrLogStats()
func (stats *VrLogStats) Send(detail string) {
if stats.StartTime.IsZero() {
stats.Type = "Error: Type not specified"
stats.StartTime = time.Now()
}
stats.LogTime = stats.StartTime.Format("2006-01-02T15:04:05")
stats.Detail = detail
stats.DurationNs = time.Since(stats.StartTime).Nanoseconds()
vrLogStatsLogger.Send(stats)
}
func init() {
http.HandleFunc("/debug/vrlog", func(w http.ResponseWriter, r *http.Request) {
ch := vrLogStatsLogger.Subscribe("vrlogstats")
defer vrLogStatsLogger.Unsubscribe(ch)
vrlogStatsHandler(ch, w, r)
})
}
func vrlogStatsHandler(ch chan any, w http.ResponseWriter, r *http.Request) {
timeout, limit := parseTimeoutLimitParams(r)
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.(*VrLogStats)
if !ok {
log.Error("Log received is not of type VrLogStats")
continue
}
if err := vrLogStatsTemplate.Execute(w, stats); err != nil {
log.Errorf("vrlog: couldn't execute template: %v", err)
}
if f, ok := w.(http.Flusher); ok {
f.Flush()
}
case <-tmr.C:
return
}
}
}
func parseTimeoutLimitParams(req *http.Request) (time.Duration, int) {
timeout := 1000
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
}