From 4246773cb45ec70704ab4a7cc91504ea70e7a4e5 Mon Sep 17 00:00:00 2001 From: Tim Vaillancourt Date: Tue, 20 Feb 2024 00:55:55 +0100 Subject: [PATCH] fix copylocks linter err Signed-off-by: Tim Vaillancourt --- go/streamlog/bind_variable.go | 120 ++++++++++++------ go/vt/vtgate/logstats/logstats.go | 6 +- go/vt/vtgate/logstats/logstats_test.go | 4 +- .../tabletserver/tabletenv/logstats.go | 6 +- .../tabletserver/tabletenv/logstats_test.go | 6 +- 5 files changed, 93 insertions(+), 49 deletions(-) diff --git a/go/streamlog/bind_variable.go b/go/streamlog/bind_variable.go index 7a2eafbecfc..9e3de31903d 100644 --- a/go/streamlog/bind_variable.go +++ b/go/streamlog/bind_variable.go @@ -25,47 +25,27 @@ import ( var ErrUnrecognizedBindVarType = errors.New("unrecognized bind variable type") -// BindVariable is a wrapper for marshal/unmarshaling querypb.BindVariable as json. -type BindVariable querypb.BindVariable - -// NewBindVariable returns a wrapped *querypb.BindVariable object. -func NewBindVariable(bv *querypb.BindVariable) BindVariable { - return BindVariable(*bv) -} - -// NewBindVariables returns a string-map of wrapped *querypb.BindVariable objects. -func NewBindVariables(bvs map[string]*querypb.BindVariable) map[string]BindVariable { - out := make(map[string]BindVariable, len(bvs)) - for key, bindVar := range bvs { - out[key] = NewBindVariable(bindVar) - } - return out +// BindVariableValue is used to store querypb.BindVariable values. +type BindVariableValue struct { + Type string + Value []byte } -// UnmarshalJSON unmarshals the custom BindVariable json-format. -// See MarshalJSON for more information. -func (bv *BindVariable) UnmarshalJSON(b []byte) error { - in := struct { - Type string - Value []byte - }{} - if err := json.Unmarshal(b, &in); err != nil { - return err +// MarshalJSON renders the BindVariableValue as json and optionally redacts the value. +func (bv BindVariableValue) MarshalJSON() ([]byte, error) { + out := map[string]interface{}{ + "Type": bv.Type, + "Value": bv.Value, } - // convert type string to querypb.Type and pass along Value. - typeVal, found := querypb.Type_value[in.Type] - if !found { - return ErrUnrecognizedBindVarType + if GetRedactDebugUIQueries() { + out["Value"] = nil } - bv.Type = querypb.Type(typeVal) - bv.Value = in.Value - return nil + return json.Marshal(out) } -// MarshalJSON renders the wrapped *querypb.BindVariable as json. It -// ensures that the "Type" field is a string representation of the -// variable type instead of an integer-based code that is less -// portable and human-readable. +// BindVariable is a wrapper for marshal/unmarshaling querypb.BindVariable as json. +// It ensures that the "Type" field is a string representation of the variable +// type instead of an integer-based code that is less portable and human-readable. // // This allows a *querypb.BindVariable that would have marshaled // to this: @@ -79,10 +59,40 @@ func (bv *BindVariable) UnmarshalJSON(b []byte) error { // or if query redaction is enabled, like this: // // {"Type":"VARBINARY","Value":null} +type BindVariable struct { + Type string + Value []byte + Values []*BindVariableValue +} + +// NewBindVariable returns a wrapped *querypb.BindVariable object. +func NewBindVariable(bv *querypb.BindVariable) BindVariable { + newBv := BindVariable{ + Type: bv.Type.String(), + Value: bv.Value, + } + for _, val := range bv.Values { + newBv.Values = append(newBv.Values, &BindVariableValue{ + Type: val.Type.String(), + Value: val.Value, + }) + } + return newBv +} + +// NewBindVariables returns a string-map of wrapped *querypb.BindVariable objects. +func NewBindVariables(bvs map[string]*querypb.BindVariable) map[string]BindVariable { + out := make(map[string]BindVariable, len(bvs)) + for key, bindVar := range bvs { + out[key] = NewBindVariable(bindVar) + } + return out +} + +// MarshalJSON renders the BindVariable as json and optionally redacts the value. func (bv BindVariable) MarshalJSON() ([]byte, error) { - // convert querypb.Type integer to string and pass along Value. out := map[string]interface{}{ - "Type": bv.Type.String(), + "Type": bv.Type, "Value": bv.Value, } if GetRedactDebugUIQueries() { @@ -91,12 +101,40 @@ func (bv BindVariable) MarshalJSON() ([]byte, error) { return json.Marshal(out) } +// bindVariablesValuesToProto converts a slice of *BindVariableValue to *querypb.Value. +func bindVariablesValuesToProto(vals []*BindVariableValue) ([]*querypb.Value, error) { + values := make([]*querypb.Value, len(vals)) + for _, val := range vals { + varType, found := querypb.Type_value[val.Type] + if !found { + return nil, ErrUnrecognizedBindVarType + } + values = append(values, &querypb.Value{ + Type: querypb.Type(varType), + Value: val.Value, + }) + } + return values, nil +} + // BindVariablesToProto converts a string-map of BindVariable to a string-map of *querypb.BindVariable. -func BindVariablesToProto(bvs map[string]BindVariable) map[string]*querypb.BindVariable { +func BindVariablesToProto(bvs map[string]BindVariable) (map[string]*querypb.BindVariable, error) { out := make(map[string]*querypb.BindVariable, len(bvs)) for key, bindVar := range bvs { - bv := querypb.BindVariable(bindVar) - out[key] = &bv + // convert type string to querypb.Type. + varType, found := querypb.Type_value[bindVar.Type] + if !found { + return nil, ErrUnrecognizedBindVarType + } + values, err := bindVariablesValuesToProto(bindVar.Values) + if err != nil { + return nil, err + } + out[key] = &querypb.BindVariable{ + Type: querypb.Type(varType), + Value: bindVar.Value, + Values: values, + } } - return out + return out, nil } diff --git a/go/vt/vtgate/logstats/logstats.go b/go/vt/vtgate/logstats/logstats.go index 3c260a88182..4df588cd598 100644 --- a/go/vt/vtgate/logstats/logstats.go +++ b/go/vt/vtgate/logstats/logstats.go @@ -146,8 +146,12 @@ func (stats *LogStats) Logf(w io.Writer, params url.Values) error { formattedBindVars := "\"[REDACTED]\"" if !streamlog.GetRedactDebugUIQueries() && !streamlog.UseQueryLogJSONV2() { _, fullBindParams := params["full"] + bindVarsProto, err := streamlog.BindVariablesToProto(stats.BindVariables) + if err != nil { + return err + } formattedBindVars = sqltypes.FormatBindVariables( - streamlog.BindVariablesToProto(stats.BindVariables), + bindVarsProto, fullBindParams, streamlog.GetQueryLogFormat() == streamlog.QueryLogFormatJSON, ) diff --git a/go/vt/vtgate/logstats/logstats_test.go b/go/vt/vtgate/logstats/logstats_test.go index 06389e3ee15..e70d81b83bd 100644 --- a/go/vt/vtgate/logstats/logstats_test.go +++ b/go/vt/vtgate/logstats/logstats_test.go @@ -179,9 +179,9 @@ func TestLogStatsFormatJSONV2(t *testing.T) { assert.Nil(t, logStats.Logf(&buf, nil)) assert.Equal(t, `{"RemoteAddr":"","Username":"","ImmediateCaller":"","EffectiveCaller":"","Method":"test","TabletType":"PRIMARY","StmtType":"select","SQL":"select * from testtable where name = :strVal and message = :bytesVal","BindVariables":{"bytesVal":{"Type":"VARBINARY","Value":"FmtAtEq6S9Y="},"strVal":{"Type":"VARCHAR","Value":"YWJj"}},"StartTime":"2017-01-01T01:02:03Z","EndTime":"2017-01-01T01:02:04.000001234Z","ShardQueries":0,"RowsAffected":0,"RowsReturned":0,"PlanTime":0,"ExecuteTime":0,"CommitTime":0,"TablesUsed":["ks1.tbl1","ks2.tbl2"],"SessionUUID":"suuid","CachedPlan":false,"ActiveKeyspace":"db"}`, strings.TrimSpace(buf.String())) assert.Nil(t, json.Unmarshal(buf.Bytes(), &cmpStats)) - assert.Equal(t, querypb.Type_VARBINARY, cmpStats.BindVariables["bytesVal"].Type) + assert.Equal(t, "VARBINARY", cmpStats.BindVariables["bytesVal"].Type) assert.Equal(t, []byte("\x16k@\xb4J\xbaK\xd6"), cmpStats.BindVariables["bytesVal"].Value) - assert.Equal(t, querypb.Type_VARCHAR, cmpStats.BindVariables["strVal"].Type) + assert.Equal(t, "VARCHAR", cmpStats.BindVariables["strVal"].Type) assert.Equal(t, []byte("abc"), cmpStats.BindVariables["strVal"].Value) } { diff --git a/go/vt/vttablet/tabletserver/tabletenv/logstats.go b/go/vt/vttablet/tabletserver/tabletenv/logstats.go index 1d174bba52d..837f42bffa8 100644 --- a/go/vt/vttablet/tabletserver/tabletenv/logstats.go +++ b/go/vt/vttablet/tabletserver/tabletenv/logstats.go @@ -199,8 +199,12 @@ func (stats *LogStats) Logf(w io.Writer, params url.Values) error { formattedBindVars := "\"[REDACTED]\"" if !streamlog.GetRedactDebugUIQueries() && !streamlog.UseQueryLogJSONV2() { _, fullBindParams := params["full"] + bindVarsProto, err := streamlog.BindVariablesToProto(stats.BindVariables) + if err != nil { + return err + } formattedBindVars = sqltypes.FormatBindVariables( - streamlog.BindVariablesToProto(stats.BindVariables), + bindVarsProto, fullBindParams, streamlog.GetQueryLogFormat() == streamlog.QueryLogFormatJSON, ) diff --git a/go/vt/vttablet/tabletserver/tabletenv/logstats_test.go b/go/vt/vttablet/tabletserver/tabletenv/logstats_test.go index ad793f29026..9d62d8cc248 100644 --- a/go/vt/vttablet/tabletserver/tabletenv/logstats_test.go +++ b/go/vt/vttablet/tabletserver/tabletenv/logstats_test.go @@ -33,8 +33,6 @@ import ( "vitess.io/vitess/go/streamlog" "vitess.io/vitess/go/vt/callinfo" "vitess.io/vitess/go/vt/callinfo/fakecallinfo" - - querypb "vitess.io/vitess/go/vt/proto/query" ) func TestLogStats(t *testing.T) { @@ -221,9 +219,9 @@ func TestLogStatsFormatJSONV2(t *testing.T) { assert.Nil(t, logStats.Logf(&buf, nil)) assert.Equal(t, `{"CallInfo":"","Username":"","ImmediateCaller":"","EffectiveCaller":"","RewrittenSQL":"sql with pii","TotalTime":1000001234,"ResponseSize":1,"Method":"test","PlanType":"","OriginalSQL":"sql","BindVariables":{"bytesVal":{"Type":"VARBINARY","Value":"FmtAtEq6S9Y="},"intVal":{"Type":"INT64","Value":"MQ=="}},"RowsAffected":0,"NumberOfQueries":1,"StartTime":"2017-01-01T01:02:03Z","EndTime":"2017-01-01T01:02:04.000001234Z","MysqlResponseTime":0,"WaitingForConnection":0,"QuerySources":2,"TransactionID":12345,"ReservedID":0,"CachedPlan":false}`, strings.TrimSpace(buf.String())) assert.Nil(t, json.Unmarshal(buf.Bytes(), &cmpStats)) - assert.Equal(t, querypb.Type_VARBINARY, cmpStats.BindVariables["bytesVal"].Type) + assert.Equal(t, "VARBINARY", cmpStats.BindVariables["bytesVal"].Type) assert.Equal(t, []byte("\x16k@\xb4J\xbaK\xd6"), cmpStats.BindVariables["bytesVal"].Value) - assert.Equal(t, querypb.Type_INT64, cmpStats.BindVariables["intVal"].Type) + assert.Equal(t, "INT64", cmpStats.BindVariables["intVal"].Type) assert.Equal(t, []byte("1"), cmpStats.BindVariables["intVal"].Value) } {