From 9036de862f5989ba68e886567b7a8e0f48e43caf Mon Sep 17 00:00:00 2001 From: Dirkjan Bussink Date: Thu, 18 Jan 2024 12:03:39 +0100 Subject: [PATCH] Use the correct parser for truncation (#14985) Signed-off-by: Dirkjan Bussink --- go/vt/vtgate/querylog.go | 2 +- go/vt/vtgate/querylogz.go | 14 +++++------ go/vt/vtgate/querylogz_test.go | 9 ++++---- go/vt/vttablet/tabletserver/querylogz.go | 23 ++++++------------- go/vt/vttablet/tabletserver/querylogz_test.go | 9 ++++---- go/vt/vttablet/tabletserver/tabletserver.go | 14 +++++++++++ go/vt/vttablet/tabletserver/txlogz.go | 5 ---- 7 files changed, 39 insertions(+), 37 deletions(-) diff --git a/go/vt/vtgate/querylog.go b/go/vt/vtgate/querylog.go index 7425f2feba9..5d0dedf87d7 100644 --- a/go/vt/vtgate/querylog.go +++ b/go/vt/vtgate/querylog.go @@ -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) { diff --git a/go/vt/vtgate/querylogz.go b/go/vt/vtgate/querylogz.go index 0e8c8044515..7c72e950d4a 100644 --- a/go/vt/vtgate/querylogz.go +++ b/go/vt/vtgate/querylogz.go @@ -55,10 +55,9 @@ var ( `) 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(` @@ -74,7 +73,7 @@ var ( {{.ExecuteTime.Seconds}} {{.CommitTime.Seconds}} {{.StmtType}} - {{.SQL | truncateQuery | unquote | cssWrappable}} + {{.SQL | .Parser.TruncateForUI | unquote | cssWrappable}} {{.ShardQueries}} {{.RowsAffected}} {{.ErrorStr}} @@ -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 @@ -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) } diff --git a/go/vt/vtgate/querylogz_test.go b/go/vt/vtgate/querylogz_test.go index ce0f4d4311b..3cecb983b3f 100644 --- a/go/vt/vtgate/querylogz_test.go +++ b/go/vt/vtgate/querylogz_test.go @@ -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" @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/go/vt/vttablet/tabletserver/querylogz.go b/go/vt/vttablet/tabletserver/querylogz.go index 8f42192c330..33341d1641b 100644 --- a/go/vt/vttablet/tabletserver/querylogz.go +++ b/go/vt/vttablet/tabletserver/querylogz.go @@ -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" ) @@ -57,10 +56,9 @@ var ( `) 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(` @@ -74,7 +72,7 @@ var ( {{.MysqlResponseTime.Seconds}} {{.WaitingForConnection.Seconds}} {{.PlanType}} - {{.OriginalSQL | truncateQuery | unquote | cssWrappable}} + {{.OriginalSQL | .Parser.TruncateForUI | unquote | cssWrappable}} {{.NumberOfQueries}} {{.FmtQuerySources}} {{.RowsAffected}} @@ -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 @@ -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) } diff --git a/go/vt/vttablet/tabletserver/querylogz_test.go b/go/vt/vttablet/tabletserver/querylogz_test.go index 2e5caa3891b..25f03c762c7 100644 --- a/go/vt/vttablet/tabletserver/querylogz_test.go +++ b/go/vt/vttablet/tabletserver/querylogz_test.go @@ -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" ) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/go/vt/vttablet/tabletserver/tabletserver.go b/go/vt/vttablet/tabletserver/tabletserver.go index 7d3271b58e2..a70eeaadb68 100644 --- a/go/vt/vttablet/tabletserver/tabletserver.go +++ b/go/vt/vttablet/tabletserver/tabletserver.go @@ -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() @@ -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) diff --git a/go/vt/vttablet/tabletserver/txlogz.go b/go/vt/vttablet/tabletserver/txlogz.go index 04a2147a7e0..8d1b88c8c85 100644 --- a/go/vt/vttablet/tabletserver/txlogz.go +++ b/go/vt/vttablet/tabletserver/txlogz.go @@ -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" ) @@ -72,10 +71,6 @@ var ( `)) ) -func init() { - servenv.HTTPHandleFunc("/txlogz", txlogzHandler) -} - // txlogzHandler serves a human readable snapshot of the // current transaction log. // Endpoint: /txlogz?timeout=%d&limit=%d