Skip to content

Commit aa60419

Browse files
committed
database/redis: More verbosity in OnRetryableError
The OnRetryableError functions came with conditions to only log the error if it had changed since the last iteration. While this keeps the log file clean, it hides reoccurring errors and makes it harder to identify specific errors. The changed version always logs the reoccurring error and also includes the duration since the start and the current attempt.
1 parent 481f79f commit aa60419

File tree

3 files changed

+14
-11
lines changed

3 files changed

+14
-11
lines changed

database/db.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,10 +879,11 @@ func (db *DB) HasTable(ctx context.Context, table string) (bool, error) {
879879
func (db *DB) GetDefaultRetrySettings() retry.Settings {
880880
return retry.Settings{
881881
Timeout: retry.DefaultTimeout,
882-
OnRetryableError: func(_ time.Duration, _ uint64, err, lastErr error) {
883-
if lastErr == nil || err.Error() != lastErr.Error() {
884-
db.logger.Warnw("Can't execute query. Retrying", zap.Error(err))
885-
}
882+
OnRetryableError: func(elapsed time.Duration, attempt uint64, err, lastErr error) {
883+
db.logger.Warnw("Can't execute query. Retrying",
884+
zap.Error(err),
885+
zap.Duration("after", elapsed),
886+
zap.Uint64("attempt", attempt))
886887
},
887888
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
888889
if attempt > 1 {

database/driver.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,10 @@ func (c RetryConnector) Connect(ctx context.Context) (driver.Conn, error) {
6767
c.callbacks.OnRetryableError(elapsed, attempt, err, lastErr)
6868
}
6969

70-
if lastErr == nil || err.Error() != lastErr.Error() {
71-
c.logger.Warnw("Can't connect to database. Retrying", zap.Error(err))
72-
}
70+
c.logger.Warnw("Can't connect to database. Retrying",
71+
zap.Error(err),
72+
zap.Duration("after", elapsed),
73+
zap.Uint64("attempt", attempt))
7374
},
7475
OnSuccess: func(elapsed time.Duration, attempt uint64, lastErr error) {
7576
if c.callbacks.OnSuccess != nil {

redis/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,11 @@ func dialWithLogging(dialer ctxDialerFunc, logger *logging.Logger) ctxDialerFunc
310310
backoff.NewExponentialWithJitter(1*time.Millisecond, 1*time.Second),
311311
retry.Settings{
312312
Timeout: retry.DefaultTimeout,
313-
OnRetryableError: func(_ time.Duration, _ uint64, err, lastErr error) {
314-
if lastErr == nil || err.Error() != lastErr.Error() {
315-
logger.Warnw("Can't connect to Redis. Retrying", zap.Error(err))
316-
}
313+
OnRetryableError: func(elapsed time.Duration, attempt uint64, err, lastErr error) {
314+
logger.Warnw("Can't connect to Redis. Retrying",
315+
zap.Error(err),
316+
zap.Duration("after", elapsed),
317+
zap.Uint64("attempt", attempt))
317318
},
318319
OnSuccess: func(elapsed time.Duration, attempt uint64, _ error) {
319320
if attempt > 1 {

0 commit comments

Comments
 (0)