Skip to content

Commit af7f0c7

Browse files
committed
Actually test the field is populated correctly
1 parent 0efbf03 commit af7f0c7

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

server/handler_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

@@ -1954,8 +1955,18 @@ func TestLoggerFieldsSetup(t *testing.T) {
19541955
require.Contains(t, logger.Data, sql.ConnectTimeLogKey, "Logger should contain connect time")
19551956
require.Contains(t, logger.Data, sql.ConnectionIdLogField, "Logger should contain connection ID")
19561957

1957-
// Verify that the QueryTimeLogKey constant exists
1958-
require.NotEmpty(t, sql.QueryTimeLogKey, "QueryTimeLogKey constant should be defined")
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")
19591970

19601971
// Verify the values are of correct types
19611972
connectTime, ok := logger.Data[sql.ConnectTimeLogKey].(time.Time)
@@ -1966,3 +1977,19 @@ func TestLoggerFieldsSetup(t *testing.T) {
19661977
require.True(t, ok, "Connection ID should be a uint32")
19671978
require.Equal(t, conn.ConnectionID, connID, "Connection ID should match")
19681979
}
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

Comments
 (0)