Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly measure duration of Query #1042

Merged
merged 3 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (d *PgxDatabase) performAndReport(fields logging.Fields, fn func() (interfa
ret, err := fn()
duration := time.Since(start)
if duration > 100*time.Millisecond {
logger := d.getLogger().WithFields(fields).WithField("duration", duration)
logger := d.getLogger().WithFields(fields).WithField("took", duration)
if err != nil {
logger = logger.WithError(err)
}
Expand Down Expand Up @@ -117,15 +117,17 @@ func (d *PgxDatabase) GetPrimitive(dest interface{}, query string, args ...inter
return nil
}
func (d *PgxDatabase) Query(query string, args ...interface{}) (rows pgx.Rows, err error) {
ret, err := d.performAndReport(logging.Fields{
l := d.getLogger().WithFields(logging.Fields{
"type": "start query",
"query": query,
"args": args,
}, func() (interface{}, error) { return d.db.Query(d.getContext(), query, args...) })
})
start := time.Now()
ret, err := d.db.Query(d.getContext(), query, args...)
if ret == nil {
return nil, err
}
return ret.(pgx.Rows), err
return Logged(ret, start, l), err
}

func (d *PgxDatabase) Select(results interface{}, query string, args ...interface{}) error {
Expand Down
74 changes: 74 additions & 0 deletions db/logged_rows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package db

import (
"github.com/treeverse/lakefs/logging"

"time"

"github.com/jackc/pgconn"
"github.com/jackc/pgproto3/v2"
"github.com/jackc/pgx/v4"
)

// LoggedRows is a pgx.Rows that wraps and traces another pgx.Rows.
type LoggedRows struct {
pgx.Rows
Start time.Time
Closed bool
l logging.Logger
}

var _ pgx.Rows = &LoggedRows{}

// Logged returns a pgx.Rows that will forward calls to r and logs their durations.
func Logged(r pgx.Rows, start time.Time, l logging.Logger) *LoggedRows {
return &LoggedRows{
Rows: r,
Start: start,
l: l,
}
}

func (lr *LoggedRows) logDuration() {
if !lr.Closed {
lr.l.WithField("duration", time.Since(lr.Start)).Info("rows done")
}
}

func (lr *LoggedRows) Close() {
lr.Rows.Close()
lr.logDuration()
}

func (lr *LoggedRows) Next() bool {
ret := lr.Rows.Next()
if !ret {
// Underlying Rows closed itself, lr.Close() was not called
lr.logDuration()
}
return ret
}

func (lr *LoggedRows) Err() error {
return lr.Rows.Err()
}

func (lr *LoggedRows) CommandTag() pgconn.CommandTag {
return lr.Rows.CommandTag()
}

func (lr *LoggedRows) FieldDescriptions() []pgproto3.FieldDescription {
return lr.Rows.FieldDescriptions()
}

func (lr *LoggedRows) Scan(dest ...interface{}) error {
return lr.Rows.Scan(dest...)
}

func (lr *LoggedRows) Values() ([]interface{}, error) {
return lr.Rows.Values()
}

func (lr *LoggedRows) RawValues() [][]byte {
return lr.Rows.RawValues()
}
3 changes: 1 addition & 2 deletions db/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,13 @@ func (d *dbTx) Query(query string, args ...interface{}) (pgx.Rows, error) {
"type": "query",
"args": args,
"query": queryToString(query),
"took": time.Since(start),
})
if err != nil {
log.WithError(err).Error("SQL query failed with error")
return nil, err
}
log.Trace("SQL query started successfully")
return rows, nil
return Logged(rows, start, log), nil
}

func Select(d Tx, results interface{}, query string, args ...interface{}) error {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ require (
github.com/influxdata/tdigest v0.0.1 // indirect
github.com/jackc/pgconn v1.6.4
github.com/jackc/pgerrcode v0.0.0-20190803225404-afa3381909a6
github.com/jackc/pgproto3/v2 v2.0.4 // indirect
github.com/jackc/pgproto3/v2 v2.0.4
github.com/jackc/pgtype v1.4.2
github.com/jackc/pgx/v4 v4.8.1
github.com/jamiealquiza/tachymeter v2.0.0+incompatible
Expand Down