internal/database: log additional info for QueryRow errors

We're seeing several 500s from queries that should be cheap, such as
db.IsExcluded, db.GetUnitMeta, and getPathID in db.GetUnit. These
queries all call QueryRow. Additional logging is added to debug this
issue.

Change-Id: I286b5b2115d757ef6bcc2a3f4d8530d04037bbae
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/259619
Trust: Julie Qiu <julie@golang.org>
Run-TryBot: Julie Qiu <julie@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
diff --git a/internal/database/database.go b/internal/database/database.go
index bff5b08..fc82059 100644
--- a/internal/database/database.go
+++ b/internal/database/database.go
@@ -15,6 +15,7 @@
 	"regexp"
 	"strings"
 	"sync"
+	"time"
 
 	"github.com/lib/pq"
 	"golang.org/x/pkgsite/internal/derrors"
@@ -107,7 +108,17 @@
 
 // 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 {
+			log.Errorf(ctx, "QueryRow context error: %v "+msg, ctx.Err())
+		} else {
+			log.Debugf(ctx, "QueryRow: "+msg)
+		}
+		logQuery(ctx, query, args, db.instanceID)(nil)
+	}()
 	if db.tx != nil {
 		return db.tx.QueryRowContext(ctx, query, args...)
 	}