11
11
// See the License for the specific language governing permissions and
12
12
// limitations under the License.
13
13
14
- //go:build go1.15
15
- // +build go1.15
16
-
17
14
package collectors
18
15
19
16
import (
@@ -38,100 +35,68 @@ type dbStatsCollector struct {
38
35
maxLifetimeClosed * prometheus.Desc
39
36
}
40
37
41
- // DBStatsCollectorOpts defines the behavior of a db stats collector
42
- // created with NewDBStatsCollector.
43
- type DBStatsCollectorOpts struct {
44
- // DriverName holds the name of driver.
45
- // It will not used for empty strings.
46
- DriverName string
47
- }
48
-
49
38
// NewDBStatsCollector returns a collector that exports metrics about the given *sql.DB.
50
39
// See https://golang.org/pkg/database/sql/#DBStats for more information on stats.
51
- func NewDBStatsCollector (db * sql.DB , opts DBStatsCollectorOpts ) prometheus.Collector {
52
- var fqName func (name string ) string
53
- if opts .DriverName == "" {
54
- fqName = func (name string ) string {
55
- return "go_db_stats_" + name
56
- }
57
- } else {
58
- fqName = func (name string ) string {
59
- return "go_" + opts .DriverName + "_db_stats_" + name
60
- }
40
+ func NewDBStatsCollector (db * sql.DB , dbName string ) prometheus.Collector {
41
+ fqName := func (name string ) string {
42
+ return "go_sql_" + name
61
43
}
62
44
return & dbStatsCollector {
63
45
db : db ,
64
46
maxOpenConnections : prometheus .NewDesc (
65
47
fqName ("max_open_connections" ),
66
48
"Maximum number of open connections to the database." ,
67
- nil , nil ,
49
+ nil , prometheus. Labels { "db_name" : dbName } ,
68
50
),
69
51
openConnections : prometheus .NewDesc (
70
52
fqName ("open_connections" ),
71
53
"The number of established connections both in use and idle." ,
72
- nil , nil ,
54
+ nil , prometheus. Labels { "db_name" : dbName } ,
73
55
),
74
56
inUseConnections : prometheus .NewDesc (
75
57
fqName ("in_use_connections" ),
76
58
"The number of connections currently in use." ,
77
- nil , nil ,
59
+ nil , prometheus. Labels { "db_name" : dbName } ,
78
60
),
79
61
idleConnections : prometheus .NewDesc (
80
62
fqName ("idle_connections" ),
81
63
"The number of idle connections." ,
82
- nil , nil ,
64
+ nil , prometheus. Labels { "db_name" : dbName } ,
83
65
),
84
66
waitCount : prometheus .NewDesc (
85
67
fqName ("wait_count_total" ),
86
68
"The total number of connections waited for." ,
87
- nil , nil ,
69
+ nil , prometheus. Labels { "db_name" : dbName } ,
88
70
),
89
71
waitDuration : prometheus .NewDesc (
90
72
fqName ("wait_duration_seconds_total" ),
91
73
"The total time blocked waiting for a new connection." ,
92
- nil , nil ,
74
+ nil , prometheus. Labels { "db_name" : dbName } ,
93
75
),
94
76
maxIdleClosed : prometheus .NewDesc (
95
77
fqName ("max_idle_closed_total" ),
96
78
"The total number of connections closed due to SetMaxIdleConns." ,
97
- nil , nil ,
79
+ nil , prometheus. Labels { "db_name" : dbName } ,
98
80
),
99
81
maxIdleTimeClosed : prometheus .NewDesc (
100
82
fqName ("max_idle_time_closed_total" ),
101
83
"The total number of connections closed due to SetConnMaxIdleTime." ,
102
- nil , nil ,
84
+ nil , prometheus. Labels { "db_name" : dbName } ,
103
85
),
104
86
maxLifetimeClosed : prometheus .NewDesc (
105
87
fqName ("max_lifetime_closed_total" ),
106
88
"The total number of connections closed due to SetConnMaxLifetime." ,
107
- nil , nil ,
89
+ nil , prometheus. Labels { "db_name" : dbName } ,
108
90
),
109
91
}
110
92
}
111
93
112
94
// Describe implements Collector.
113
95
func (c * dbStatsCollector ) Describe (ch chan <- * prometheus.Desc ) {
114
- ch <- c .maxOpenConnections
115
- ch <- c .openConnections
116
- ch <- c .inUseConnections
117
- ch <- c .idleConnections
118
- ch <- c .waitCount
119
- ch <- c .waitDuration
120
- ch <- c .maxIdleClosed
121
- ch <- c .maxIdleTimeClosed
122
- ch <- c .maxLifetimeClosed
96
+ c .describe (ch )
123
97
}
124
98
125
99
// Collect implements Collector.
126
100
func (c * dbStatsCollector ) Collect (ch chan <- prometheus.Metric ) {
127
- stats := c .db .Stats ()
128
- ch <- prometheus .MustNewConstMetric (c .maxOpenConnections , prometheus .GaugeValue , float64 (stats .MaxOpenConnections ))
129
- ch <- prometheus .MustNewConstMetric (c .openConnections , prometheus .GaugeValue , float64 (stats .OpenConnections ))
130
- ch <- prometheus .MustNewConstMetric (c .inUseConnections , prometheus .GaugeValue , float64 (stats .InUse ))
131
- ch <- prometheus .MustNewConstMetric (c .idleConnections , prometheus .GaugeValue , float64 (stats .Idle ))
132
- ch <- prometheus .MustNewConstMetric (c .waitCount , prometheus .CounterValue , float64 (stats .WaitCount ))
133
- ch <- prometheus .MustNewConstMetric (c .waitDuration , prometheus .CounterValue , stats .WaitDuration .Seconds ())
134
- ch <- prometheus .MustNewConstMetric (c .maxIdleClosed , prometheus .CounterValue , float64 (stats .MaxIdleClosed ))
135
- ch <- prometheus .MustNewConstMetric (c .maxIdleTimeClosed , prometheus .CounterValue , float64 (stats .MaxIdleTimeClosed ))
136
- ch <- prometheus .MustNewConstMetric (c .maxLifetimeClosed , prometheus .CounterValue , float64 (stats .MaxLifetimeClosed ))
101
+ c .collect (ch )
137
102
}
0 commit comments