@@ -33,20 +33,27 @@ var interval = 10 * time.Second
3333
3434// pollDBStats calls (*DB).Stats on the db at a predetermined interval. It pushes the DBStats off to the statsd client.
3535// the caller should always ensure that db & statsd are non-nil
36- func pollDBStats (statsd internal.StatsdClient , db * sql.DB ) {
36+ func pollDBStats (statsd internal.StatsdClient , db * sql.DB , stop chan struct {} ) {
3737 log .Debug ("DB stats will be gathered and sent every %v." , interval )
38- for range time .NewTicker (interval ).C {
39- log .Debug ("Reporting DB.Stats metrics..." )
40- stat := db .Stats ()
41- statsd .Gauge (MaxOpenConnections , float64 (stat .MaxOpenConnections ), []string {}, 1 )
42- statsd .Gauge (OpenConnections , float64 (stat .OpenConnections ), []string {}, 1 )
43- statsd .Gauge (InUse , float64 (stat .InUse ), []string {}, 1 )
44- statsd .Gauge (Idle , float64 (stat .Idle ), []string {}, 1 )
45- statsd .Gauge (WaitCount , float64 (stat .WaitCount ), []string {}, 1 )
46- statsd .Timing (WaitDuration , stat .WaitDuration , []string {}, 1 )
47- statsd .Gauge (MaxIdleClosed , float64 (stat .MaxIdleClosed ), []string {}, 1 )
48- statsd .Gauge (MaxIdleTimeClosed , float64 (stat .MaxIdleTimeClosed ), []string {}, 1 )
49- statsd .Gauge (MaxLifetimeClosed , float64 (stat .MaxLifetimeClosed ), []string {}, 1 )
38+ ticker := time .NewTicker (interval )
39+ defer ticker .Stop ()
40+ for {
41+ select {
42+ case <- ticker .C :
43+ log .Debug ("Reporting DB.Stats metrics..." )
44+ stat := db .Stats ()
45+ statsd .Gauge (MaxOpenConnections , float64 (stat .MaxOpenConnections ), []string {}, 1 )
46+ statsd .Gauge (OpenConnections , float64 (stat .OpenConnections ), []string {}, 1 )
47+ statsd .Gauge (InUse , float64 (stat .InUse ), []string {}, 1 )
48+ statsd .Gauge (Idle , float64 (stat .Idle ), []string {}, 1 )
49+ statsd .Gauge (WaitCount , float64 (stat .WaitCount ), []string {}, 1 )
50+ statsd .Timing (WaitDuration , stat .WaitDuration , []string {}, 1 )
51+ statsd .Gauge (MaxIdleClosed , float64 (stat .MaxIdleClosed ), []string {}, 1 )
52+ statsd .Gauge (MaxIdleTimeClosed , float64 (stat .MaxIdleTimeClosed ), []string {}, 1 )
53+ statsd .Gauge (MaxLifetimeClosed , float64 (stat .MaxLifetimeClosed ), []string {}, 1 )
54+ case <- stop :
55+ return
56+ }
5057 }
5158}
5259
0 commit comments