[logger] Implement Database.Trace so SQL trace logging is not swallowed - #1065
[logger] Implement Database.Trace so SQL trace logging is not swallowed#1065Atishyy27 wants to merge 2 commits into
Conversation
Database.Trace had an empty body, so every SQL statement GORM traced along with its execution time, row count and error was silently discarded. Info, Warn and Error in the same file all forward to the underlying logger, Trace did not. Forward the statement to the logger the way the neighbouring methods do: errors go to the error handler, everything else to the default handler. The message format matches GORM's own logger, including its convention of reporting a row count of -1 as a dash when a count does not apply to the statement. Fixes meshery#1063 Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
There was a problem hiding this comment.
Code Review
This pull request implements the Trace method for the GORM database logger in logger/database.go to log SQL execution details, and introduces a comprehensive test suite in logger/database_test.go. The review feedback recommends adding defensive nil checks for the logger receiver, its base, and the callback function fc to prevent potential nil pointer dereferences and panics.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| // time, the affected row count and any error to the underlying logger, matching the format used | ||
| // by GORM's own logger. | ||
| func (c *Database) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) { | ||
| sql, rows := fc() |
There was a problem hiding this comment.
There was a problem hiding this comment.
good catch, added. guarded against a nil receiver, nil base, and nil fc, one line at the top
of Trace. covered by TestDatabase_Trace_NilReceiverAndArgs, which panics without the guard and
passes with it. pushed in 1dbe409
gemini flagged that Trace could be called with a nil Database, a Database with a nil base, or a nil fc. GORM does not do this in practice, but the guard is one line and removes the possibility outright. Covered by TestDatabase_Trace_NilReceiverAndArgs, which panics without the guard. Signed-off-by: Atishyy27 <142108881+Atishyy27@users.noreply.github.com>
ritzorama
left a comment
There was a problem hiding this comment.
Is this Opentracing compatible?
ritzorama
left a comment
There was a problem hiding this comment.
Let's use Opentracing for this functionality...
|
Thanks for taking a look! I think there's a mix-up on what this Trace is. It isn't distributed tracing - it's the Trace method on GORM's logger interface (gorm.io/gorm/logger.Interface), which the Database logger has to implement. GORM calls it after every SQL statement with the query, timing, row count, and any error, purely for logging. Ours had an empty body, so all of |
ishwar170695
left a comment
There was a problem hiding this comment.
Looks good to me. This fixes the missing Trace implementation with a minimal, targeted change, follows the existing logger conventions, and includes good test coverage for the expected paths and nil guards. Thanks!
Notes for Reviewers
Database.Trace()inlogger/database.gohad an empty body, so everything GORM handed it after each SQL statement (the statement itself, execution time, row count, and any error) was silently dropped.Info,WarnandErrorin the same file all forward to the underlying logger,Tracewas the odd one out.what changed
Tracenow forwards to the logger the way its neighbours do: errors go to the error handler, everything else to the default handler. The message format matches GORM's own logger ([%.3fms] [rows:%v] %s), including its convention of reporting a row count of-1as a dash when a count does not apply to the statement. I mirrored the existing format rather than inventing one so output stays consistent with what GORM users already read.before
after
tests
Added
TestDatabase_Tracecovering a successful query, a failed query (asserting it reaches the error handler with the error), and therows:-1case. Verified it fails before the change (every assertion sees an empty buffer) and passes after.gofmtandgo vetare clean on the touched files.Signed commits