forked from dolthub/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logstats.go
276 lines (243 loc) · 8.05 KB
/
logstats.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
Copyright 2017 Google Inc.
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 tabletenv
import (
"bytes"
"fmt"
"html/template"
"net/url"
"strings"
"time"
"golang.org/x/net/context"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/streamlog"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/callinfo"
querypb "vitess.io/vitess/go/vt/proto/query"
)
const (
// QuerySourceConsolidator means query result is found in consolidator.
QuerySourceConsolidator = 1 << iota
// QuerySourceMySQL means query result is returned from MySQL.
QuerySourceMySQL
)
// LogStats records the stats for a single query
type LogStats struct {
Ctx context.Context
Method string
Target *querypb.Target
PlanType string
OriginalSQL string
BindVariables map[string]*querypb.BindVariable
rewrittenSqls []string
RowsAffected int
NumberOfQueries int
StartTime time.Time
EndTime time.Time
MysqlResponseTime time.Duration
WaitingForConnection time.Duration
QuerySources byte
Rows [][]sqltypes.Value
TransactionID int64
Error error
}
// NewLogStats constructs a new LogStats with supplied Method and ctx
// field values, and the StartTime field set to the present time.
func NewLogStats(ctx context.Context, methodName string) *LogStats {
return &LogStats{
Ctx: ctx,
Method: methodName,
StartTime: time.Now(),
}
}
// Send finalizes a record and sends it
func (stats *LogStats) Send() {
stats.EndTime = time.Now()
StatsLogger.Send(stats)
}
// Context returns the context used by LogStats.
func (stats *LogStats) Context() context.Context {
return stats.Ctx
}
// ImmediateCaller returns the immediate caller stored in LogStats.Ctx
func (stats *LogStats) ImmediateCaller() string {
return callerid.GetUsername(callerid.ImmediateCallerIDFromContext(stats.Ctx))
}
// EffectiveCaller returns the effective caller stored in LogStats.Ctx
func (stats *LogStats) EffectiveCaller() string {
return callerid.GetPrincipal(callerid.EffectiveCallerIDFromContext(stats.Ctx))
}
// EventTime returns the time the event was created.
func (stats *LogStats) EventTime() time.Time {
return stats.EndTime
}
// AddRewrittenSQL adds a single sql statement to the rewritten list
func (stats *LogStats) AddRewrittenSQL(sql string, start time.Time) {
stats.QuerySources |= QuerySourceMySQL
stats.NumberOfQueries++
stats.rewrittenSqls = append(stats.rewrittenSqls, sql)
stats.MysqlResponseTime += time.Now().Sub(start)
}
// TotalTime returns how long this query has been running
func (stats *LogStats) TotalTime() time.Duration {
return stats.EndTime.Sub(stats.StartTime)
}
// RewrittenSQL returns a semicolon separated list of SQL statements
// that were executed.
func (stats *LogStats) RewrittenSQL() string {
return strings.Join(stats.rewrittenSqls, "; ")
}
// SizeOfResponse returns the approximate size of the response in
// bytes (this does not take in account protocol encoding). It will return
// 0 for streaming requests.
func (stats *LogStats) SizeOfResponse() int {
if stats.Rows == nil {
return 0
}
size := 0
for _, row := range stats.Rows {
for _, field := range row {
size += field.Len()
}
}
return size
}
// FmtBindVariables returns the map of bind variables as a string or a json
// string depending on the streamlog.QueryLogFormat value. If RedactDebugUIQueries
// is true then this returns the string "[REDACTED]"
//
// For values that are strings or byte slices it only reports their type
// and length unless full is true.
func (stats *LogStats) FmtBindVariables(full bool) string {
if *streamlog.RedactDebugUIQueries {
return "\"[REDACTED]\""
}
var out map[string]*querypb.BindVariable
if full {
out = stats.BindVariables
} else {
// NOTE(szopa): I am getting rid of potentially large bind
// variables.
out = make(map[string]*querypb.BindVariable)
for k, v := range stats.BindVariables {
if sqltypes.IsIntegral(v.Type) || sqltypes.IsFloat(v.Type) {
out[k] = v
} else if v.Type == querypb.Type_TUPLE {
out[k] = sqltypes.StringBindVariable(fmt.Sprintf("%v items", len(v.Values)))
} else {
out[k] = sqltypes.StringBindVariable(fmt.Sprintf("%v bytes", len(v.Value)))
}
}
}
if *streamlog.QueryLogFormat == streamlog.QueryLogFormatJSON {
var buf bytes.Buffer
buf.WriteString("{")
first := true
for k, v := range out {
if !first {
buf.WriteString(", ")
} else {
first = false
}
if sqltypes.IsIntegral(v.Type) || sqltypes.IsFloat(v.Type) {
fmt.Fprintf(&buf, "%q: {\"type\": %q, \"value\": %v}", k, v.Type, string(v.Value))
} else {
fmt.Fprintf(&buf, "%q: {\"type\": %q, \"value\": %q}", k, v.Type, string(v.Value))
}
}
buf.WriteString("}")
return buf.String()
}
return fmt.Sprintf("%v", out)
}
// FmtQuerySources returns a comma separated list of query
// sources. If there were no query sources, it returns the string
// "none".
func (stats *LogStats) FmtQuerySources() string {
if stats.QuerySources == 0 {
return "none"
}
sources := make([]string, 2)
n := 0
if stats.QuerySources&QuerySourceMySQL != 0 {
sources[n] = "mysql"
n++
}
if stats.QuerySources&QuerySourceConsolidator != 0 {
sources[n] = "consolidator"
n++
}
return strings.Join(sources[:n], ",")
}
// ContextHTML returns the HTML version of the context that was used, or "".
// This is a method on LogStats instead of a field so that it doesn't need
// to be passed by value everywhere.
func (stats *LogStats) ContextHTML() template.HTML {
return callinfo.HTMLFromContext(stats.Ctx)
}
// ErrorStr returns the error string or ""
func (stats *LogStats) ErrorStr() string {
if stats.Error != nil {
return stats.Error.Error()
}
return ""
}
// RemoteAddrUsername returns some parts of CallInfo if set
func (stats *LogStats) RemoteAddrUsername() (string, string) {
ci, ok := callinfo.FromContext(stats.Ctx)
if !ok {
return "", ""
}
return ci.RemoteAddr(), ci.Username()
}
// Format returns a tab separated list of logged fields.
func (stats *LogStats) Format(params url.Values) string {
rewrittenSQL := "[REDACTED]"
if !*streamlog.RedactDebugUIQueries {
rewrittenSQL = stats.RewrittenSQL()
}
_, fullBindParams := params["full"]
formattedBindVars := stats.FmtBindVariables(fullBindParams)
// TODO: remove username here we fully enforce immediate caller id
remoteAddr, username := stats.RemoteAddrUsername()
// Valid options for the QueryLogFormat are text or json
var fmtString string
switch *streamlog.QueryLogFormat {
case streamlog.QueryLogFormatText:
fmtString = "%v\t%v\t%v\t'%v'\t'%v'\t%v\t%v\t%.6f\t%v\t%q\t%v\t%v\t%q\t%v\t%.6f\t%.6f\t%v\t%v\t%q\t\n"
case streamlog.QueryLogFormatJSON:
fmtString = "{\"Method\": %q, \"RemoteAddr\": %q, \"Username\": %q, \"ImmediateCaller\": %q, \"Effective Caller\": %q, \"Start\": \"%v\", \"End\": \"%v\", \"TotalTime\": %.6f, \"PlanType\": %q, \"OriginalSQL\": %q, \"BindVars\": %v, \"Queries\": %v, \"RewrittenSQL\": %q, \"QuerySources\": %q, \"MysqlTime\": %.6f, \"ConnWaitTime\": %.6f, \"RowsAffected\": %v, \"ResponseSize\": %v, \"Error\": %q}\n"
}
return fmt.Sprintf(
fmtString,
stats.Method,
remoteAddr,
username,
stats.ImmediateCaller(),
stats.EffectiveCaller(),
stats.StartTime.Format(time.StampMicro),
stats.EndTime.Format(time.StampMicro),
stats.TotalTime().Seconds(),
stats.PlanType,
stats.OriginalSQL,
formattedBindVars,
stats.NumberOfQueries,
rewrittenSQL,
stats.FmtQuerySources(),
stats.MysqlResponseTime.Seconds(),
stats.WaitingForConnection.Seconds(),
stats.RowsAffected,
stats.SizeOfResponse(),
stats.ErrorStr(),
)
}