Skip to content

Commit

Permalink
fix copylocks linter err
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Vaillancourt <[email protected]>
  • Loading branch information
timvaillancourt committed Feb 19, 2024
1 parent 150727c commit 4246773
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 49 deletions.
120 changes: 79 additions & 41 deletions go/streamlog/bind_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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() {
Expand All @@ -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
}
6 changes: 5 additions & 1 deletion go/vt/vtgate/logstats/logstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
4 changes: 2 additions & 2 deletions go/vt/vtgate/logstats/logstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
{
Expand Down
6 changes: 5 additions & 1 deletion go/vt/vttablet/tabletserver/tabletenv/logstats.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand Down
6 changes: 2 additions & 4 deletions go/vt/vttablet/tabletserver/tabletenv/logstats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
{
Expand Down

0 comments on commit 4246773

Please sign in to comment.