@@ -20,58 +20,48 @@ type Gauges struct {
2020 inUse metrics.Gauge
2121 open metrics.Gauge
2222
23- dbName bool
24- driver bool
23+ dbName string
24+ driver string
2525}
2626
2727// NewGauges returns a new Gauges.
2828func NewGauges (idle , inUse , open metrics.Gauge ) * Gauges {
2929 return & Gauges {
30- idle : idle ,
31- inUse : inUse ,
32- open : open ,
30+ idle : idle ,
31+ inUse : inUse ,
32+ open : open ,
33+ dbName : "unknown" ,
34+ driver : "default" ,
3335 }
3436}
3537
3638// DBName sets the dbname label of metrics.
3739func (g * Gauges ) DBName (dbName string ) * Gauges {
38- withValues := []string {"dbname" , dbName }
3940 return & Gauges {
40- idle : g .idle . With ( withValues ... ) ,
41- inUse : g .inUse . With ( withValues ... ) ,
42- open : g .open . With ( withValues ... ) ,
43- dbName : true ,
41+ idle : g .idle ,
42+ inUse : g .inUse ,
43+ open : g .open ,
44+ dbName : dbName ,
4445 driver : g .driver ,
4546 }
4647}
4748
4849// Driver sets the driver label of metrics.
4950func (g * Gauges ) Driver (driver string ) * Gauges {
50- withValues := []string {"driver" , driver }
5151 return & Gauges {
52- idle : g .idle . With ( withValues ... ) ,
53- inUse : g .inUse . With ( withValues ... ) ,
54- open : g .open . With ( withValues ... ) ,
52+ idle : g .idle ,
53+ inUse : g .inUse ,
54+ open : g .open ,
5555 dbName : g .dbName ,
56- driver : true ,
56+ driver : driver ,
5757 }
5858}
5959
6060// Observe records the DBStats collected. It should be called periodically.
6161func (g * Gauges ) Observe (stats sql.DBStats ) {
62- if ! g .dbName {
63- g .idle = g .idle .With ("dbname" , "" )
64- g .inUse = g .inUse .With ("dbname" , "" )
65- g .open = g .open .With ("dbname" , "" )
66- }
67- if ! g .driver {
68- g .idle = g .idle .With ("driver" , "" )
69- g .inUse = g .inUse .With ("driver" , "" )
70- g .open = g .open .With ("driver" , "" )
71- }
72- g .idle .Set (float64 (stats .Idle ))
73- g .inUse .Set (float64 (stats .InUse ))
74- g .open .Set (float64 (stats .OpenConnections ))
62+ g .idle .With ("dbname" , g .dbName , "driver" , g .driver ).Set (float64 (stats .Idle ))
63+ g .inUse .With ("dbname" , g .dbName , "driver" , g .driver ).Set (float64 (stats .InUse ))
64+ g .open .With ("dbname" , g .dbName , "driver" , g .driver ).Set (float64 (stats .OpenConnections ))
7565}
7666
7767// newCollector creates a new database wrapper containing the name of the database,
0 commit comments