Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 81a9556

Browse filesBrowse files
committed
Make dbStatsCollector more DRY
Signed-off-by: beorn7 <beorn@grafana.com>
1 parent a66da1d commit 81a9556
Copy full SHA for 81a9556

File tree

Expand file treeCollapse file tree

3 files changed

+33
-44
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+33
-44
lines changed

‎prometheus/collectors/dbstats_collector.go

Copy file name to clipboardExpand all lines: prometheus/collectors/dbstats_collector.go
+19-2Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,27 @@ func NewDBStatsCollector(db *sql.DB, dbName string) prometheus.Collector {
9393

9494
// Describe implements Collector.
9595
func (c *dbStatsCollector) Describe(ch chan<- *prometheus.Desc) {
96-
c.describe(ch)
96+
ch <- c.maxOpenConnections
97+
ch <- c.openConnections
98+
ch <- c.inUseConnections
99+
ch <- c.idleConnections
100+
ch <- c.waitCount
101+
ch <- c.waitDuration
102+
ch <- c.maxIdleClosed
103+
ch <- c.maxLifetimeClosed
104+
c.describeNewInGo115(ch)
97105
}
98106

99107
// Collect implements Collector.
100108
func (c *dbStatsCollector) Collect(ch chan<- prometheus.Metric) {
101-
c.collect(ch)
109+
stats := c.db.Stats()
110+
ch <- prometheus.MustNewConstMetric(c.maxOpenConnections, prometheus.GaugeValue, float64(stats.MaxOpenConnections))
111+
ch <- prometheus.MustNewConstMetric(c.openConnections, prometheus.GaugeValue, float64(stats.OpenConnections))
112+
ch <- prometheus.MustNewConstMetric(c.inUseConnections, prometheus.GaugeValue, float64(stats.InUse))
113+
ch <- prometheus.MustNewConstMetric(c.idleConnections, prometheus.GaugeValue, float64(stats.Idle))
114+
ch <- prometheus.MustNewConstMetric(c.waitCount, prometheus.CounterValue, float64(stats.WaitCount))
115+
ch <- prometheus.MustNewConstMetric(c.waitDuration, prometheus.CounterValue, stats.WaitDuration.Seconds())
116+
ch <- prometheus.MustNewConstMetric(c.maxIdleClosed, prometheus.CounterValue, float64(stats.MaxIdleClosed))
117+
ch <- prometheus.MustNewConstMetric(c.maxLifetimeClosed, prometheus.CounterValue, float64(stats.MaxLifetimeClosed))
118+
c.collectNewInGo115(ch, stats)
102119
}

‎prometheus/collectors/dbstats_collector_go115.go

Copy file name to clipboardExpand all lines: prometheus/collectors/dbstats_collector_go115.go
+7-20Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,16 @@
1515

1616
package collectors
1717

18-
import "github.com/prometheus/client_golang/prometheus"
18+
import (
19+
"database/sql"
1920

20-
func (c *dbStatsCollector) describe(ch chan<- *prometheus.Desc) {
21-
ch <- c.maxOpenConnections
22-
ch <- c.openConnections
23-
ch <- c.inUseConnections
24-
ch <- c.idleConnections
25-
ch <- c.waitCount
26-
ch <- c.waitDuration
27-
ch <- c.maxIdleClosed
21+
"github.com/prometheus/client_golang/prometheus"
22+
)
23+
24+
func (c *dbStatsCollector) describeNewInGo115(ch chan<- *prometheus.Desc) {
2825
ch <- c.maxIdleTimeClosed
29-
ch <- c.maxLifetimeClosed
3026
}
3127

32-
func (c *dbStatsCollector) collect(ch chan<- prometheus.Metric) {
33-
stats := c.db.Stats()
34-
ch <- prometheus.MustNewConstMetric(c.maxOpenConnections, prometheus.GaugeValue, float64(stats.MaxOpenConnections))
35-
ch <- prometheus.MustNewConstMetric(c.openConnections, prometheus.GaugeValue, float64(stats.OpenConnections))
36-
ch <- prometheus.MustNewConstMetric(c.inUseConnections, prometheus.GaugeValue, float64(stats.InUse))
37-
ch <- prometheus.MustNewConstMetric(c.idleConnections, prometheus.GaugeValue, float64(stats.Idle))
38-
ch <- prometheus.MustNewConstMetric(c.waitCount, prometheus.CounterValue, float64(stats.WaitCount))
39-
ch <- prometheus.MustNewConstMetric(c.waitDuration, prometheus.CounterValue, stats.WaitDuration.Seconds())
40-
ch <- prometheus.MustNewConstMetric(c.maxIdleClosed, prometheus.CounterValue, float64(stats.MaxIdleClosed))
28+
func (c *dbStatsCollector) collectNewInGo115(ch chan<- prometheus.Metric, stats sql.DBStats) {
4129
ch <- prometheus.MustNewConstMetric(c.maxIdleTimeClosed, prometheus.CounterValue, float64(stats.MaxIdleTimeClosed))
42-
ch <- prometheus.MustNewConstMetric(c.maxLifetimeClosed, prometheus.CounterValue, float64(stats.MaxLifetimeClosed))
4330
}

‎prometheus/collectors/dbstats_collector_pre_go115.go

Copy file name to clipboardExpand all lines: prometheus/collectors/dbstats_collector_pre_go115.go
+7-22Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,12 @@
1515

1616
package collectors
1717

18-
import "github.com/prometheus/client_golang/prometheus"
18+
import (
19+
"database/sql"
1920

20-
func (c *dbStatsCollector) describe(ch chan<- *prometheus.Desc) {
21-
ch <- c.maxOpenConnections
22-
ch <- c.openConnections
23-
ch <- c.inUseConnections
24-
ch <- c.idleConnections
25-
ch <- c.waitCount
26-
ch <- c.waitDuration
27-
ch <- c.maxIdleClosed
28-
ch <- c.maxLifetimeClosed
29-
}
21+
"github.com/prometheus/client_golang/prometheus"
22+
)
3023

31-
func (c *dbStatsCollector) collect(ch chan<- prometheus.Metric) {
32-
stats := c.db.Stats()
33-
ch <- prometheus.MustNewConstMetric(c.maxOpenConnections, prometheus.GaugeValue, float64(stats.MaxOpenConnections))
34-
ch <- prometheus.MustNewConstMetric(c.openConnections, prometheus.GaugeValue, float64(stats.OpenConnections))
35-
ch <- prometheus.MustNewConstMetric(c.inUseConnections, prometheus.GaugeValue, float64(stats.InUse))
36-
ch <- prometheus.MustNewConstMetric(c.idleConnections, prometheus.GaugeValue, float64(stats.Idle))
37-
ch <- prometheus.MustNewConstMetric(c.waitCount, prometheus.CounterValue, float64(stats.WaitCount))
38-
ch <- prometheus.MustNewConstMetric(c.waitDuration, prometheus.CounterValue, stats.WaitDuration.Seconds())
39-
ch <- prometheus.MustNewConstMetric(c.maxIdleClosed, prometheus.CounterValue, float64(stats.MaxIdleClosed))
40-
ch <- prometheus.MustNewConstMetric(c.maxLifetimeClosed, prometheus.CounterValue, float64(stats.MaxLifetimeClosed))
41-
}
24+
func (c *dbStatsCollector) describeNewInGo115(ch chan<- *prometheus.Desc) {}
25+
26+
func (c *dbStatsCollector) collectNewInGo115(ch chan<- prometheus.Metric, stats sql.DBStats) {}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.