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

Use the context logger, if available. #99

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 9 additions & 9 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (c *conn) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, e
// Ping attempts to verify that the server is accessible.
// Returns ErrBadConn if ping fails and consequently DB.Ping will remove the conn from the pool.
func (c *conn) Ping(ctx context.Context) error {
log := logger.WithContext(c.id, driverctx.CorrelationIdFromContext(ctx), "")
log := logger.AddContext(logger.Ctx(ctx), c.id, driverctx.CorrelationIdFromContext(ctx), "")
ctx = driverctx.NewContextWithConnId(ctx, c.id)
ctx1, cancel := context.WithTimeout(ctx, c.cfg.PingTimeout)
defer cancel()
Expand Down Expand Up @@ -92,7 +92,7 @@ func (c *conn) IsValid() bool {
// Statement ExecContext is the same as connection ExecContext
func (c *conn) ExecContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Result, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.WithContext(c.id, corrId, "")
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, "")
msg, start := logger.Track("ExecContext")
defer log.Duration(msg, start)

Expand All @@ -104,7 +104,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name

if exStmtResp != nil && exStmtResp.OperationHandle != nil {
// we have an operation id so update the logger
log = logger.WithContext(c.id, corrId, client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID))
log = logger.AddContext(logger.Ctx(ctx), c.id, corrId, client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID))

// since we have an operation handle we can close the operation if necessary
alreadyClosed := exStmtResp.DirectResults != nil && exStmtResp.DirectResults.CloseOperation != nil
Expand Down Expand Up @@ -135,7 +135,7 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name
// Statement QueryContext is the same as connection QueryContext
func (c *conn) QueryContext(ctx context.Context, query string, args []driver.NamedValue) (driver.Rows, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.WithContext(c.id, corrId, "")
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, "")
msg, start := log.Track("QueryContext")

ctx = driverctx.NewContextWithConnId(ctx, c.id)
Expand All @@ -147,7 +147,7 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
exStmtResp, _, err := c.runQuery(ctx, query, args)

if exStmtResp != nil && exStmtResp.OperationHandle != nil {
log = logger.WithContext(c.id, driverctx.CorrelationIdFromContext(ctx), client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID))
log = logger.AddContext(logger.Ctx(ctx), c.id, driverctx.CorrelationIdFromContext(ctx), client.SprintGuid(exStmtResp.OperationHandle.OperationId.GUID))
}
defer log.Duration(msg, start)

Expand All @@ -165,7 +165,7 @@ func (c *conn) QueryContext(ctx context.Context, query string, args []driver.Nam
}

func (c *conn) runQuery(ctx context.Context, query string, args []driver.NamedValue) (*cli_service.TExecuteStatementResp, *cli_service.TGetOperationStatusResp, error) {
log := logger.WithContext(c.id, driverctx.CorrelationIdFromContext(ctx), "")
log := logger.AddContext(logger.Ctx(ctx), c.id, driverctx.CorrelationIdFromContext(ctx), "")
// first we try to get the results synchronously.
// at any point in time that the context is done we must cancel and return
exStmtResp, err := c.executeStatement(ctx, query, args)
Expand All @@ -175,7 +175,7 @@ func (c *conn) runQuery(ctx context.Context, query string, args []driver.NamedVa
}
opHandle := exStmtResp.OperationHandle
if opHandle != nil && opHandle.OperationId != nil {
log = logger.WithContext(
log = logger.AddContext(logger.Ctx(ctx),
c.id,
driverctx.CorrelationIdFromContext(ctx), client.SprintGuid(opHandle.OperationId.GUID),
)
Expand Down Expand Up @@ -259,7 +259,7 @@ func logBadQueryState(log *logger.DBSQLLogger, opStatus *cli_service.TGetOperati

func (c *conn) executeStatement(ctx context.Context, query string, args []driver.NamedValue) (*cli_service.TExecuteStatementResp, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.WithContext(c.id, corrId, "")
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, "")

req := cli_service.TExecuteStatementReq{
SessionHandle: c.session.SessionHandle,
Expand Down Expand Up @@ -311,7 +311,7 @@ func (c *conn) executeStatement(ctx context.Context, query string, args []driver

func (c *conn) pollOperation(ctx context.Context, opHandle *cli_service.TOperationHandle) (*cli_service.TGetOperationStatusResp, error) {
corrId := driverctx.CorrelationIdFromContext(ctx)
log := logger.WithContext(c.id, corrId, client.SprintGuid(opHandle.OperationId.GUID))
log := logger.AddContext(logger.Ctx(ctx), c.id, corrId, client.SprintGuid(opHandle.OperationId.GUID))
var statusResp *cli_service.TGetOperationStatusResp
ctx = driverctx.NewContextWithConnId(ctx, c.id)
newCtx := driverctx.NewContextWithCorrelationId(driverctx.NewContextWithConnId(context.Background(), c.id), corrId)
Expand Down
18 changes: 17 additions & 1 deletion logger/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package logger

import (
"context"
"io"
"os"
"runtime"
Expand Down Expand Up @@ -128,9 +129,24 @@ func Err(err error) *zerolog.Event {
return Logger.Err(err)
}

// Ctx returns a DBSQLLogger from the provided context. If no logger is found,
// the default logger is returned.
func Ctx(ctx context.Context) *DBSQLLogger {
l := zerolog.Ctx(ctx)
if l == zerolog.DefaultContextLogger {
return Logger
}
return &DBSQLLogger{*l}
}

// AddContext sets connectionId, correlationId, and queryId as fields on the provided logger.
func AddContext(l *DBSQLLogger, connectionId string, correlationId string, queryId string) *DBSQLLogger {
return &DBSQLLogger{l.With().Str("connId", connectionId).Str("corrId", correlationId).Str("queryId", queryId).Logger()}
}

// WithContext sets connectionId, correlationId, and queryId to be used as fields.
func WithContext(connectionId string, correlationId string, queryId string) *DBSQLLogger {
return &DBSQLLogger{Logger.With().Str("connId", connectionId).Str("corrId", correlationId).Str("queryId", queryId).Logger()}
return AddContext(Logger, connectionId, correlationId, queryId)
}

// Track is a convenience function to track time spent
Expand Down