From 011294d1d74eee92a79342b8ba8c5b8a7811149c Mon Sep 17 00:00:00 2001 From: Sean Grider Date: Mon, 6 Jan 2025 14:16:51 -0500 Subject: [PATCH 1/2] Support sending custom tags when using pollDBStats --- contrib/database/sql/metrics.go | 20 ++++++++++---------- contrib/database/sql/option.go | 8 ++++++++ contrib/database/sql/sql.go | 2 +- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/contrib/database/sql/metrics.go b/contrib/database/sql/metrics.go index d8ff4ed266..f0b5a5069b 100644 --- a/contrib/database/sql/metrics.go +++ b/contrib/database/sql/metrics.go @@ -33,20 +33,20 @@ 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) { +func pollDBStats(statsd internal.StatsdClient, db *sql.DB, tags []string) { 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() - statsd.Gauge(MaxOpenConnections, float64(stat.MaxOpenConnections), []string{}, 1) - statsd.Gauge(OpenConnections, float64(stat.OpenConnections), []string{}, 1) - statsd.Gauge(InUse, float64(stat.InUse), []string{}, 1) - statsd.Gauge(Idle, float64(stat.Idle), []string{}, 1) - statsd.Gauge(WaitCount, float64(stat.WaitCount), []string{}, 1) - statsd.Timing(WaitDuration, stat.WaitDuration, []string{}, 1) - statsd.Gauge(MaxIdleClosed, float64(stat.MaxIdleClosed), []string{}, 1) - statsd.Gauge(MaxIdleTimeClosed, float64(stat.MaxIdleTimeClosed), []string{}, 1) - statsd.Gauge(MaxLifetimeClosed, float64(stat.MaxLifetimeClosed), []string{}, 1) + statsd.Gauge(MaxOpenConnections, float64(stat.MaxOpenConnections), tags, 1) + statsd.Gauge(OpenConnections, float64(stat.OpenConnections), tags, 1) + statsd.Gauge(InUse, float64(stat.InUse), tags, 1) + statsd.Gauge(Idle, float64(stat.Idle), tags, 1) + statsd.Gauge(WaitCount, float64(stat.WaitCount), tags, 1) + statsd.Timing(WaitDuration, stat.WaitDuration, tags, 1) + statsd.Gauge(MaxIdleClosed, float64(stat.MaxIdleClosed), tags, 1) + statsd.Gauge(MaxIdleTimeClosed, float64(stat.MaxIdleTimeClosed), tags, 1) + statsd.Gauge(MaxLifetimeClosed, float64(stat.MaxLifetimeClosed), tags, 1) } } diff --git a/contrib/database/sql/option.go b/contrib/database/sql/option.go index 567c861cb8..7524ae8fbb 100644 --- a/contrib/database/sql/option.go +++ b/contrib/database/sql/option.go @@ -31,6 +31,7 @@ type config struct { tags map[string]interface{} dbmPropagationMode tracer.DBMPropagationMode dbStats bool + statsTags []string statsdClient internal.StatsdClient } @@ -292,3 +293,10 @@ func WithDBStats() Option { cfg.dbStats = true } } + +func WithDBStatsTags(tags []string) Option { + return func(cfg *config) { + cfg.dbStats = true + cfg.statsTags = tags + } +} diff --git a/contrib/database/sql/sql.go b/contrib/database/sql/sql.go index b26318d0d3..5aa3c5df81 100644 --- a/contrib/database/sql/sql.go +++ b/contrib/database/sql/sql.go @@ -211,7 +211,7 @@ func OpenDB(c driver.Connector, opts ...Option) *sql.DB { } db := sql.OpenDB(tc) if cfg.dbStats && cfg.statsdClient != nil { - go pollDBStats(cfg.statsdClient, db) + go pollDBStats(cfg.statsdClient, db, cfg.statsTags) } return db } From 042408265a873974c26e8e02280bc5e624bcf0cd Mon Sep 17 00:00:00 2001 From: Sean Grider Date: Tue, 14 Jan 2025 09:30:18 -0500 Subject: [PATCH 2/2] gofmt --- .github/workflows/apps/latest_major_version.go | 2 +- contrib/database/sql/option.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/apps/latest_major_version.go b/.github/workflows/apps/latest_major_version.go index f471afe580..50edd0dc41 100644 --- a/.github/workflows/apps/latest_major_version.go +++ b/.github/workflows/apps/latest_major_version.go @@ -8,12 +8,12 @@ package main import ( "encoding/json" "fmt" - "sort" "github.com/Masterminds/semver/v3" "golang.org/x/mod/modfile" "net/http" "os" "regexp" + "sort" "strings" ) diff --git a/contrib/database/sql/option.go b/contrib/database/sql/option.go index 7524ae8fbb..a8b8c36087 100644 --- a/contrib/database/sql/option.go +++ b/contrib/database/sql/option.go @@ -31,7 +31,7 @@ type config struct { tags map[string]interface{} dbmPropagationMode tracer.DBMPropagationMode dbStats bool - statsTags []string + statsTags []string statsdClient internal.StatsdClient }