Skip to content

Commit

Permalink
Use the correct parser for truncation (#14985)
Browse files Browse the repository at this point in the history
Signed-off-by: Dirkjan Bussink <[email protected]>
  • Loading branch information
dbussink authored Jan 18, 2024
1 parent 7800699 commit 9036de8
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 37 deletions.
2 changes: 1 addition & 1 deletion go/vt/vtgate/querylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (e *Executor) defaultQueryLogger() error {
servenv.HTTPHandleFunc(QueryLogzHandler, func(w http.ResponseWriter, r *http.Request) {
ch := queryLogger.Subscribe("querylogz")
defer queryLogger.Unsubscribe(ch)
querylogzHandler(ch, w, r)
querylogzHandler(ch, w, r, e.parser)
})

servenv.HTTPHandleFunc(QueryzHandler, func(w http.ResponseWriter, r *http.Request) {
Expand Down
14 changes: 7 additions & 7 deletions go/vt/vtgate/querylogz.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ var (
</thead>
`)
querylogzFuncMap = template.FuncMap{
"stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) },
"cssWrappable": logz.Wrappable,
"truncateQuery": sqlparser.NewTestParser().TruncateForUI,
"unquote": func(s string) string { return strings.Trim(s, "\"") },
"stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) },
"cssWrappable": logz.Wrappable,
"unquote": func(s string) string { return strings.Trim(s, "\"") },
}
querylogzTmpl = template.Must(template.New("example").Funcs(querylogzFuncMap).Parse(`
<tr class="{{.ColorLevel}}">
Expand All @@ -74,7 +73,7 @@ var (
<td>{{.ExecuteTime.Seconds}}</td>
<td>{{.CommitTime.Seconds}}</td>
<td>{{.StmtType}}</td>
<td>{{.SQL | truncateQuery | unquote | cssWrappable}}</td>
<td>{{.SQL | .Parser.TruncateForUI | unquote | cssWrappable}}</td>
<td>{{.ShardQueries}}</td>
<td>{{.RowsAffected}}</td>
<td>{{.ErrorStr}}</td>
Expand All @@ -84,7 +83,7 @@ var (

// querylogzHandler serves a human readable snapshot of the
// current query log.
func querylogzHandler(ch chan *logstats.LogStats, w http.ResponseWriter, r *http.Request) {
func querylogzHandler(ch chan *logstats.LogStats, w http.ResponseWriter, r *http.Request, parser *sqlparser.Parser) {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
Expand Down Expand Up @@ -115,7 +114,8 @@ func querylogzHandler(ch chan *logstats.LogStats, w http.ResponseWriter, r *http
tmplData := struct {
*logstats.LogStats
ColorLevel string
}{stats, level}
Parser *sqlparser.Parser
}{stats, level, parser}
if err := querylogzTmpl.Execute(w, tmplData); err != nil {
log.Errorf("querylogz: couldn't execute template: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions go/vt/vtgate/querylogz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"
"time"

"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vtgate/logstats"

"vitess.io/vitess/go/streamlog"
Expand Down Expand Up @@ -73,7 +74,7 @@ func TestQuerylogzHandlerFormatting(t *testing.T) {
response := httptest.NewRecorder()
ch := make(chan *logstats.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ := io.ReadAll(response.Body)
checkQuerylogzHasStats(t, fastQueryPattern, logStats, body)
Expand Down Expand Up @@ -103,7 +104,7 @@ func TestQuerylogzHandlerFormatting(t *testing.T) {
response = httptest.NewRecorder()
ch = make(chan *logstats.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ = io.ReadAll(response.Body)
checkQuerylogzHasStats(t, mediumQueryPattern, logStats, body)
Expand Down Expand Up @@ -132,7 +133,7 @@ func TestQuerylogzHandlerFormatting(t *testing.T) {
logStats.EndTime = logStats.StartTime.Add(500 * time.Millisecond)
ch = make(chan *logstats.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ = io.ReadAll(response.Body)
checkQuerylogzHasStats(t, slowQueryPattern, logStats, body)
Expand All @@ -142,7 +143,7 @@ func TestQuerylogzHandlerFormatting(t *testing.T) {
defer func() { streamlog.SetQueryLogFilterTag("") }()
ch = make(chan *logstats.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ = io.ReadAll(response.Body)
checkQuerylogzHasStats(t, slowQueryPattern, logStats, body)
Expand Down
23 changes: 7 additions & 16 deletions go/vt/vttablet/tabletserver/querylogz.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"vitess.io/vitess/go/acl"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/logz"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
)
Expand Down Expand Up @@ -57,10 +56,9 @@ var (
</thead>
`)
querylogzFuncMap = template.FuncMap{
"stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) },
"cssWrappable": logz.Wrappable,
"truncateQuery": sqlparser.NewTestParser().TruncateForUI,
"unquote": func(s string) string { return strings.Trim(s, "\"") },
"stampMicro": func(t time.Time) string { return t.Format(time.StampMicro) },
"cssWrappable": logz.Wrappable,
"unquote": func(s string) string { return strings.Trim(s, "\"") },
}
querylogzTmpl = template.Must(template.New("example").Funcs(querylogzFuncMap).Parse(`
<tr class="{{.ColorLevel}}">
Expand All @@ -74,7 +72,7 @@ var (
<td>{{.MysqlResponseTime.Seconds}}</td>
<td>{{.WaitingForConnection.Seconds}}</td>
<td>{{.PlanType}}</td>
<td>{{.OriginalSQL | truncateQuery | unquote | cssWrappable}}</td>
<td>{{.OriginalSQL | .Parser.TruncateForUI | unquote | cssWrappable}}</td>
<td>{{.NumberOfQueries}}</td>
<td>{{.FmtQuerySources}}</td>
<td>{{.RowsAffected}}</td>
Expand All @@ -86,17 +84,9 @@ var (
`))
)

func init() {
servenv.HTTPHandleFunc("/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 *tabletenv.LogStats, w http.ResponseWriter, r *http.Request) {
func querylogzHandler(ch chan *tabletenv.LogStats, w http.ResponseWriter, r *http.Request, parser *sqlparser.Parser) {
if err := acl.CheckAccessHTTP(r, acl.DEBUGGING); err != nil {
acl.SendError(w, err)
return
Expand Down Expand Up @@ -127,7 +117,8 @@ func querylogzHandler(ch chan *tabletenv.LogStats, w http.ResponseWriter, r *htt
tmplData := struct {
*tabletenv.LogStats
ColorLevel string
}{stats, level}
Parser *sqlparser.Parser
}{stats, level, parser}
if err := querylogzTmpl.Execute(w, tmplData); err != nil {
log.Errorf("querylogz: couldn't execute template: %v", err)
}
Expand Down
9 changes: 5 additions & 4 deletions go/vt/vttablet/tabletserver/querylogz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"vitess.io/vitess/go/streamlog"
"vitess.io/vitess/go/vt/callerid"
"vitess.io/vitess/go/vt/sqlparser"
"vitess.io/vitess/go/vt/vttablet/tabletserver/planbuilder"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
)
Expand Down Expand Up @@ -76,7 +77,7 @@ func TestQuerylogzHandler(t *testing.T) {
response := httptest.NewRecorder()
ch := make(chan *tabletenv.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ := io.ReadAll(response.Body)
checkQuerylogzHasStats(t, fastQueryPattern, logStats, body)
Expand Down Expand Up @@ -107,7 +108,7 @@ func TestQuerylogzHandler(t *testing.T) {
response = httptest.NewRecorder()
ch = make(chan *tabletenv.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ = io.ReadAll(response.Body)
checkQuerylogzHasStats(t, mediumQueryPattern, logStats, body)
Expand Down Expand Up @@ -137,7 +138,7 @@ func TestQuerylogzHandler(t *testing.T) {
logStats.EndTime = logStats.StartTime.Add(500 * time.Millisecond)
ch = make(chan *tabletenv.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ = io.ReadAll(response.Body)
checkQuerylogzHasStats(t, slowQueryPattern, logStats, body)
Expand All @@ -147,7 +148,7 @@ func TestQuerylogzHandler(t *testing.T) {
defer func() { streamlog.SetQueryLogFilterTag("") }()
ch = make(chan *tabletenv.LogStats, 1)
ch <- logStats
querylogzHandler(ch, response, req)
querylogzHandler(ch, response, req, sqlparser.NewTestParser())
close(ch)
body, _ = io.ReadAll(response.Body)
checkQuerylogzHasStats(t, slowQueryPattern, logStats, body)
Expand Down
14 changes: 14 additions & 0 deletions go/vt/vttablet/tabletserver/tabletserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ func NewTabletServer(ctx context.Context, name string, config *tabletenv.TabletC
tsv.registerHealthzHealthHandler()
tsv.registerDebugHealthHandler()
tsv.registerQueryzHandler()
tsv.registerQuerylogzHandler()
tsv.registerTxlogzHandler()
tsv.registerQueryListHandlers([]*QueryList{tsv.statelessql, tsv.statefulql, tsv.olapql})
tsv.registerTwopczHandler()
tsv.registerMigrationStatusHandler()
Expand Down Expand Up @@ -1851,6 +1853,18 @@ func (tsv *TabletServer) registerQueryzHandler() {
})
}

func (tsv *TabletServer) registerQuerylogzHandler() {
tsv.exporter.HandleFunc("/querylogz", func(w http.ResponseWriter, r *http.Request) {
ch := tabletenv.StatsLogger.Subscribe("querylogz")
defer tabletenv.StatsLogger.Unsubscribe(ch)
querylogzHandler(ch, w, r, tsv.parser)
})
}

func (tsv *TabletServer) registerTxlogzHandler() {
tsv.exporter.HandleFunc("/txlogz", txlogzHandler)
}

func (tsv *TabletServer) registerQueryListHandlers(queryLists []*QueryList) {
tsv.exporter.HandleFunc("/livequeryz/", func(w http.ResponseWriter, r *http.Request) {
livequeryzHandler(queryLists, w, r)
Expand Down
5 changes: 0 additions & 5 deletions go/vt/vttablet/tabletserver/txlogz.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"vitess.io/vitess/go/vt/logz"
querypb "vitess.io/vitess/go/vt/proto/query"
vtrpcpb "vitess.io/vitess/go/vt/proto/vtrpc"
"vitess.io/vitess/go/vt/servenv"
"vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
)

Expand Down Expand Up @@ -72,10 +71,6 @@ var (
</tr>`))
)

func init() {
servenv.HTTPHandleFunc("/txlogz", txlogzHandler)
}

// txlogzHandler serves a human readable snapshot of the
// current transaction log.
// Endpoint: /txlogz?timeout=%d&limit=%d
Expand Down

0 comments on commit 9036de8

Please sign in to comment.