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

contrib/database/sql: Disable DBStats if statsd client initialization fails #2682

Merged
merged 12 commits into from
May 8, 2024
Merged
Changes from 7 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
3 changes: 2 additions & 1 deletion contrib/database/sql/metrics.go
Original file line number Diff line number Diff line change
@@ -32,12 +32,13 @@ 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 statsd is non-nil
mtoffl01 marked this conversation as resolved.
Show resolved Hide resolved
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()
4 changes: 3 additions & 1 deletion contrib/database/sql/option.go
Original file line number Diff line number Diff line change
@@ -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
}
}
}
8 changes: 8 additions & 0 deletions contrib/database/sql/option_test.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
2 changes: 1 addition & 1 deletion contrib/database/sql/sql.go
Original file line number Diff line number Diff line change
@@ -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
Loading