feat: add batch logging

pull/1212/head
Stepan Rabotkin 2022-05-09 14:01:19 +03:00 committed by Jack Christensen
parent bfb19cd4f6
commit 4099b447b9
1 changed files with 23 additions and 5 deletions

28
conn.go
View File

@ -710,6 +710,8 @@ func (c *Conn) QueryFunc(ctx context.Context, sql string, args []interface{}, sc
// explicit transaction control statements are executed. The returned BatchResults must be closed before the connection // explicit transaction control statements are executed. The returned BatchResults must be closed before the connection
// is used again. // is used again.
func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults { func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
startTime := time.Now()
simpleProtocol := c.config.PreferSimpleProtocol simpleProtocol := c.config.PreferSimpleProtocol
var sb strings.Builder var sb strings.Builder
if simpleProtocol { if simpleProtocol {
@ -768,23 +770,23 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
var err error var err error
sd, err = stmtCache.Get(ctx, bi.query) sd, err = stmtCache.Get(ctx, bi.query)
if err != nil { if err != nil {
return &batchResults{ctx: ctx, conn: c, err: err} return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: err})
} }
} }
if len(sd.ParamOIDs) != len(bi.arguments) { if len(sd.ParamOIDs) != len(bi.arguments) {
return &batchResults{ctx: ctx, conn: c, err: fmt.Errorf("mismatched param and argument count")} return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: fmt.Errorf("mismatched param and argument count")})
} }
args, err := convertDriverValuers(bi.arguments) args, err := convertDriverValuers(bi.arguments)
if err != nil { if err != nil {
return &batchResults{ctx: ctx, conn: c, err: err} return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: err})
} }
for i := range args { for i := range args {
err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i]) err = c.eqb.AppendParam(c.connInfo, sd.ParamOIDs[i], args[i])
if err != nil { if err != nil {
return &batchResults{ctx: ctx, conn: c, err: err} return c.logBatchResults(ctx, startTime, &batchResults{ctx: ctx, conn: c, err: err})
} }
} }
@ -803,13 +805,29 @@ func (c *Conn) SendBatch(ctx context.Context, b *Batch) BatchResults {
mrr := c.pgConn.ExecBatch(ctx, batch) mrr := c.pgConn.ExecBatch(ctx, batch)
return &batchResults{ return c.logBatchResults(ctx, startTime, &batchResults{
ctx: ctx, ctx: ctx,
conn: c, conn: c,
mrr: mrr, mrr: mrr,
b: b, b: b,
ix: 0, ix: 0,
})
}
func (c *Conn) logBatchResults(ctx context.Context, startTime time.Time, results *batchResults) BatchResults {
if results.err != nil {
if c.shouldLog(LogLevelError) {
endTime := time.Now()
c.log(ctx, LogLevelError, "SendBatch", map[string]interface{}{"err": results.err, "time": endTime.Sub(startTime)})
}
} }
if c.shouldLog(LogLevelInfo) {
endTime := time.Now()
c.log(ctx, LogLevelInfo, "SendBatch", map[string]interface{}{"batchLen": results.b.Len(), "time": endTime.Sub(startTime)})
}
return results
} }
func (c *Conn) sanitizeForSimpleQuery(sql string, args ...interface{}) (string, error) { func (c *Conn) sanitizeForSimpleQuery(sql string, args ...interface{}) (string, error) {