From 5df6d8771bd23d8131bd5297523e05ef89d5f927 Mon Sep 17 00:00:00 2001 From: Jonathan Amsterdam Date: Tue, 27 Oct 2020 07:35:41 -0400 Subject: [PATCH] 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 Run-TryBot: Jonathan Amsterdam TryBot-Result: kokoro Reviewed-by: Julie Qiu --- internal/database/database.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/database/database.go b/internal/database/database.go index d4dfc031..9f0ec43e 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -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...)