@@ -13,6 +13,7 @@ import (
1313 "github.com/pingcap/tidb/parser/mysql"
1414 "github.com/pingcap/tiproxy/lib/util/errors"
1515 "github.com/siddontang/go/hack"
16+ "go.uber.org/zap"
1617)
1718
1819const (
@@ -431,3 +432,32 @@ func IsResultSetOKPacket(firstByte byte, length int) bool {
431432func IsErrorPacket (firstByte byte ) bool {
432433 return firstByte == ErrHeader .Byte ()
433434}
435+
436+ // The connection attribute names that are logged.
437+ // https://dev.mysql.com/doc/mysql-perfschema-excerpt/8.2/en/performance-schema-connection-attribute-tables.html
438+ const (
439+ AttrNameClientVersion = "_client_version" // libmysqlclient & Connector/C++ & Connector/J & Connector/Net & Connector/Python
440+ AttrNameClientName1 = "_client_name" // libmysqlclient & Connector/C++ & Connector/J & Connector/Python & mysqlnd
441+ AttrNameClientName2 = "_program_name" // Connector/Net
442+ AttrNameProgramName = "program_name" // MySQL Client & MySQL Shell
443+ )
444+
445+ // Attr2ZapFields converts connection attributes to log fields.
446+ // We only pick some of them because others may be too sensitive to be logged.
447+ func Attr2ZapFields (attrs map [string ]string ) []zap.Field {
448+ fields := make ([]zap.Field , 0 , 3 )
449+ if attrs != nil {
450+ if version , ok := attrs [AttrNameClientVersion ]; ok {
451+ fields = append (fields , zap .String ("client_version" , version ))
452+ }
453+ if name , ok := attrs [AttrNameClientName1 ]; ok {
454+ fields = append (fields , zap .String ("client_name" , name ))
455+ } else if name , ok := attrs [AttrNameClientName2 ]; ok {
456+ fields = append (fields , zap .String ("client_name" , name ))
457+ }
458+ if name , ok := attrs [AttrNameProgramName ]; ok {
459+ fields = append (fields , zap .String ("program_name" , name ))
460+ }
461+ }
462+ return fields
463+ }
0 commit comments