internal/database: fix logQuery call in QueryRow

The logQuery call was inside the defer, so instead of happening
at the start of the query, it happened at the end.

Also, tweak the error logging so information is only computed when
needed.

Change-Id: I2bf4b6e93880a2c44c9fb5732676935b1cdd418a
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/265477
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
This commit is contained in:
Jonathan Amsterdam 2020-10-27 07:35:41 -04:00
Родитель 5092a44d7e
Коммит 5df6d8771b
1 изменённых файлов: 3 добавлений и 3 удалений

Просмотреть файл

@ -108,14 +108,14 @@ func (db *DB) Query(ctx context.Context, query string, args ...interface{}) (_ *
// QueryRow runs the query and returns a single row.
func (db *DB) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row {
defer logQuery(ctx, query, args, db.instanceID)(nil)
start := time.Now()
defer func() {
d, _ := ctx.Deadline()
msg := fmt.Sprintf("args=%v; elapsed=%q, start=%q, deadline=%q", args, time.Since(start), start, d)
if ctx.Err() != nil {
d, _ := ctx.Deadline()
msg := fmt.Sprintf("args=%v; elapsed=%q, start=%q, deadline=%q", args, time.Since(start), start, d)
log.Errorf(ctx, "QueryRow context error: %v "+msg, ctx.Err())
}
logQuery(ctx, query, args, db.instanceID)(nil)
}()
if db.tx != nil {
return db.tx.QueryRowContext(ctx, query, args...)