diff --git a/contrib/database/sql/metrics.go b/contrib/database/sql/metrics.go index a37a0cae50..d8ff4ed266 100644 --- a/contrib/database/sql/metrics.go +++ b/contrib/database/sql/metrics.go @@ -32,12 +32,9 @@ const ( var interval = 10 * time.Second // pollDBStats calls (*DB).Stats on the db at a predetermined interval. It pushes the DBStats off to the statsd client. +// the caller should always ensure that db & statsd are non-nil func pollDBStats(statsd internal.StatsdClient, db *sql.DB) { - if db == nil { - log.Debug("No traced DB connection found; cannot pull DB stats.") - return - } - log.Debug("Traced DB connection found: DB stats will be gathered and sent every %v.", interval) + log.Debug("DB stats will be gathered and sent every %v.", interval) for range time.NewTicker(interval).C { log.Debug("Reporting DB.Stats metrics...") stat := db.Stats() diff --git a/contrib/database/sql/option.go b/contrib/database/sql/option.go index 1685999279..567c861cb8 100644 --- a/contrib/database/sql/option.go +++ b/contrib/database/sql/option.go @@ -43,8 +43,10 @@ func (c *config) checkStatsdRequired() { sc, err := internal.NewStatsdClient(globalconfig.DogstatsdAddr(), statsTags(c)) if err == nil { c.statsdClient = sc + log.Debug("Metrics from the database/sql contrib will be sent to %v", globalconfig.DogstatsdAddr()) } else { - log.Warn("Error creating statsd client for database/sql contrib package; DB Stats will be dropped: %v", err) + log.Warn("Error creating statsd client for database/sql contrib; DB Stats disabled: %v", err) + c.dbStats = false } } } diff --git a/contrib/database/sql/option_test.go b/contrib/database/sql/option_test.go index 1f3a24f3a4..e04c5ef452 100644 --- a/contrib/database/sql/option_test.go +++ b/contrib/database/sql/option_test.go @@ -72,4 +72,12 @@ func TestCheckStatsdRequired(t *testing.T) { _, ok := cfg.statsdClient.(*statsd.Client) assert.True(t, ok) }) + t.Run("invalid address", func(t *testing.T) { + globalconfig.SetDogstatsdAddr("unreachable/socket/path/dsd.socket") + cfg := new(config) + cfg.dbStats = true + cfg.checkStatsdRequired() + assert.Nil(t, cfg.statsdClient) + assert.False(t, cfg.dbStats) + }) } diff --git a/contrib/database/sql/sql.go b/contrib/database/sql/sql.go index 0832d5bd85..b26318d0d3 100644 --- a/contrib/database/sql/sql.go +++ b/contrib/database/sql/sql.go @@ -210,7 +210,7 @@ func OpenDB(c driver.Connector, opts ...Option) *sql.DB { cfg: cfg, } db := sql.OpenDB(tc) - if cfg.dbStats { + if cfg.dbStats && cfg.statsdClient != nil { go pollDBStats(cfg.statsdClient, db) } return db