Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
review iteration 2
Browse files Browse the repository at this point in the history
  • Loading branch information
VineethReddy02 committed Jun 30, 2021
1 parent f50c9be commit 9d18464
Showing 1 changed file with 22 additions and 28 deletions.
50 changes: 22 additions & 28 deletions pkg/pgxconn/pgx_conn.go
Expand Up @@ -43,7 +43,6 @@ type PgxRows interface {
func NewQueryLoggingPgxConn(pool *pgxpool.Pool) PgxConn {
return &loggingConnImpl{
connImpl: connImpl{Conn: pool},
Conn: pool,
}
}

Expand All @@ -55,39 +54,21 @@ func NewPgxConn(pool *pgxpool.Pool) PgxConn {

type loggingConnImpl struct {
connImpl
Conn *pgxpool.Pool
}

type connImpl struct {
Conn *pgxpool.Pool
}

type pgxRows struct {
type loggingPgxRows struct {
pgx.Rows
sqlQuery string
args []interface{}
startTime time.Time
logged bool
}

func (p *loggingConnImpl) Query(ctx context.Context, sql string, args ...interface{}) (PgxRows, error) {
startTime := time.Now()
rows, err := p.Conn.Query(ctx, sql, args...)
return &pgxRows{Rows: rows, sqlQuery: sql, args: args, startTime: startTime}, err
}

func (p *loggingConnImpl) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
defer logQueryStats(sql, time.Time{}, args...)()
return p.Conn.QueryRow(ctx, sql, args...)
}

func (p *connImpl) Close() {
conn := p.Conn
p.Conn = nil
conn.Close()
}

func (p *pgxRows) Next() bool {
func (p *loggingPgxRows) Next() bool {
// The query fetch is async and happens on the first call to next.
// so to get timing right, we log timing after first Next() call.
if !p.logged {
Expand All @@ -99,7 +80,18 @@ func (p *pgxRows) Next() bool {
return p.Rows.Next()
}

// calc SQL query execution time
func (p *loggingConnImpl) Query(ctx context.Context, sql string, args ...interface{}) (PgxRows, error) {
startTime := time.Now()
rows, err := p.Conn.Query(ctx, sql, args...)
return &loggingPgxRows{Rows: rows, sqlQuery: sql, args: args, startTime: startTime}, err
}

func (p *loggingConnImpl) QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row {
defer logQueryStats(sql, time.Time{}, args...)()
return p.Conn.QueryRow(ctx, sql, args...)
}

// log the SQL query, args and time consumed by the query in execution
func logQueryStats(sql string, startTime time.Time, args ...interface{}) func() {
if startTime.IsZero() {
startTime = time.Now()
Expand All @@ -109,6 +101,12 @@ func logQueryStats(sql string, startTime time.Time, args ...interface{}) func()
}
}

func (p *connImpl) Close() {
conn := p.Conn
p.Conn = nil
conn.Close()
}

func (p *connImpl) Exec(ctx context.Context, sql string, args ...interface{}) (pgconn.CommandTag, error) {
return p.Conn.Exec(ctx, sql, args...)
}
Expand Down Expand Up @@ -140,11 +138,7 @@ func (p *connImpl) SendBatch(ctx context.Context, b PgxBatch) (pgx.BatchResults,
// filters out indentation characters from the
// SQL query for better query logging
func filterIndentChars(query string) string {
dropChars := []string{"\n", "\t", "\""}
query = strings.ReplaceAll(query, "\n\t", " ")
for _, c := range dropChars {
query = strings.ReplaceAll(query, c, "")
}

dropChars := strings.NewReplacer("\n\t", " ", "\n", "", "\t", "", "\"", "")
query = dropChars.Replace(query)
return query
}

0 comments on commit 9d18464

Please sign in to comment.