@@ -29,6 +29,7 @@ import (
2929 "github.com/dolthub/vitess/go/race"
3030 "github.com/dolthub/vitess/go/sqltypes"
3131 "github.com/dolthub/vitess/go/vt/proto/query"
32+ "github.com/sirupsen/logrus"
3233 "github.com/stretchr/testify/assert"
3334 "github.com/stretchr/testify/require"
3435
@@ -1916,3 +1917,79 @@ func TestStatusVariableComUpdate(t *testing.T) {
19161917 checkSessionStatVar (t , sess1 , "Com_update" , uint64 (5 ))
19171918 checkSessionStatVar (t , sess2 , "Com_update" , uint64 (3 ))
19181919}
1920+
1921+ // TestLoggerFieldsSetup tests that handler properly sets up logger fields including query time
1922+ func TestLoggerFieldsSetup (t * testing.T ) {
1923+ e , pro := setupMemDB (require .New (t ))
1924+ dbFunc := pro .Database
1925+
1926+ handler := & Handler {
1927+ e : e ,
1928+ sm : NewSessionManager (
1929+ sql .NewContext ,
1930+ testSessionBuilder (pro ),
1931+ sql .NoopTracer ,
1932+ dbFunc ,
1933+ sql .NewMemoryManager (nil ),
1934+ sqle .NewProcessList (),
1935+ "foo" ,
1936+ ),
1937+ readTimeout : time .Second ,
1938+ }
1939+
1940+ conn := newConn (1 )
1941+ handler .NewConnection (conn )
1942+ err := handler .ComInitDB (conn , "test" )
1943+ require .NoError (t , err )
1944+
1945+ // Execute a query and verify basic logging setup
1946+ err = handler .ComQuery (context .Background (), conn , "SELECT 1" , dummyCb )
1947+ require .NoError (t , err )
1948+
1949+ // Verify that the session's logger has the expected fields
1950+ session := handler .sm .session (conn )
1951+ logger := session .GetLogger ()
1952+ require .NotNil (t , logger , "Session should have a logger" )
1953+
1954+ // Verify that the logger has the expected fields
1955+ require .Contains (t , logger .Data , sql .ConnectTimeLogKey , "Logger should contain connect time" )
1956+ require .Contains (t , logger .Data , sql .ConnectionIdLogField , "Logger should contain connection ID" )
1957+
1958+ // Verify that queryTime is actually used in logs by capturing a log entry
1959+ var capturedFields logrus.Fields
1960+ hook := & testHook {fields : & capturedFields }
1961+ logrus .AddHook (hook )
1962+ defer logrus .StandardLogger ().ReplaceHooks (make (logrus.LevelHooks ))
1963+
1964+ // Execute a query that will trigger error logging (which includes queryTime)
1965+ err = handler .ComQuery (context .Background (), conn , "SELECT * FROM nonexistent_table" , dummyCb )
1966+ require .Error (t , err ) // This should cause an error log with queryTime
1967+
1968+ // Verify that the log entry contained queryTime
1969+ require .Contains (t , capturedFields , sql .QueryTimeLogKey , "Log entry should contain queryTime field" )
1970+
1971+ // Verify the values are of correct types
1972+ connectTime , ok := logger .Data [sql .ConnectTimeLogKey ].(time.Time )
1973+ require .True (t , ok , "Connect time should be a time.Time" )
1974+ require .False (t , connectTime .IsZero (), "Connect time should not be zero" )
1975+
1976+ connID , ok := logger .Data [sql .ConnectionIdLogField ].(uint32 )
1977+ require .True (t , ok , "Connection ID should be a uint32" )
1978+ require .Equal (t , conn .ConnectionID , connID , "Connection ID should match" )
1979+ }
1980+
1981+ // Simple hook to capture log fields for testing
1982+ type testHook struct {
1983+ fields * logrus.Fields
1984+ }
1985+
1986+ func (h * testHook ) Levels () []logrus.Level {
1987+ return []logrus.Level {logrus .WarnLevel } // Only capture warning level (error logs)
1988+ }
1989+
1990+ func (h * testHook ) Fire (entry * logrus.Entry ) error {
1991+ if entry .Message == "error running query" {
1992+ * h .fields = entry .Data
1993+ }
1994+ return nil
1995+ }
0 commit comments