Log MySQL driver errors at the debug level instead of discarding them

The MySQL driver logs something like unexpected EOF, busy buffer,
broken pipe, bad connection, invalid connection etc. in the event
of an error. Most of these are retryable and are handled either by
the MySQL driver or our retry logic. Previously these messages
were discarded, but since they can be useful when debugging
connectivity issues, we'll now log them with the debug severity as
otherwise they would add too much noise to the logs where you might
see error messages with no actual effect.
This commit is contained in:
Eric Lippmann 2021-06-21 22:16:51 +02:00
parent b5b169aea8
commit ebd3f10d69

View file

@ -9,8 +9,6 @@ import (
"github.com/icinga/icingadb/pkg/retry"
"github.com/pkg/errors"
"go.uber.org/zap"
"io/ioutil"
"log"
"sync"
"syscall"
"time"
@ -72,6 +70,26 @@ func (d Driver) OpenConnector(name string) (driver.Connector, error) {
}, nil
}
// Register makes our database Driver available under the name "icingadb-mysql".
func Register(logger *zap.SugaredLogger) {
sql.Register("icingadb-mysql", &Driver{ctxDriver: &mysql.MySQLDriver{}, Logger: logger})
_ = mysql.SetLogger(mysqlLogger(func(v ...interface{}) { logger.Debug(v...) }))
}
// ctxDriver helps ensure that we only support drivers that implement driver.Driver and driver.DriverContext.
type ctxDriver interface {
driver.Driver
driver.DriverContext
}
// mysqlLogger is an adapter that allows ordinary functions to be used as a logger for mysql.SetLogger.
type mysqlLogger func(v ...interface{})
// Print implements the mysql.Logger interface.
func (log mysqlLogger) Print(v ...interface{}) {
log(v)
}
func shouldRetry(err error) bool {
if errors.Is(err, driver.ErrBadConn) || errors.Is(err, syscall.ECONNREFUSED) {
return true
@ -93,15 +111,3 @@ func shouldRetry(err error) bool {
return false
}
func Register(logger *zap.SugaredLogger) {
sql.Register("icingadb-mysql", &Driver{ctxDriver: &mysql.MySQLDriver{}, Logger: logger})
// TODO(el): Don't discard but hide?
_ = mysql.SetLogger(log.New(ioutil.Discard, "", 0))
}
// ctxDriver helps ensure that we only support drivers that implement driver.Driver and driver.DriverContext.
type ctxDriver interface {
driver.Driver
driver.DriverContext
}