-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollector.go
176 lines (165 loc) · 4.78 KB
/
collector.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package prometheus
import (
"fmt"
"github.com/alexfalkowski/go-service/os"
"github.com/alexfalkowski/go-service/version"
"github.com/jmoiron/sqlx"
"github.com/linxGnu/mssqlx"
"github.com/prometheus/client_golang/prometheus"
)
// StatsCollector implements the prometheus.Collector interface.
type StatsCollector struct {
db *mssqlx.DBs
// descriptions of exported metrics
maxOpenDesc *prometheus.Desc
openDesc *prometheus.Desc
inUseDesc *prometheus.Desc
idleDesc *prometheus.Desc
waitedForDesc *prometheus.Desc
blockedSecondsDesc *prometheus.Desc
closedMaxIdleDesc *prometheus.Desc
closedMaxLifetimeDesc *prometheus.Desc
closedMaxIdleTimeDesc *prometheus.Desc
}
// NewStatsCollector for prometheus.
func NewStatsCollector(name string, db *mssqlx.DBs, version version.Version) *StatsCollector {
labels := prometheus.Labels{"name": os.ExecutableName(), "version": string(version)}
return &StatsCollector{
db: db,
maxOpenDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_max_open_total", name),
fmt.Sprintf("Maximum number of open connections to %s.", name),
[]string{"db_name"},
labels,
),
openDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_open_total", name),
fmt.Sprintf("The number of established connections both in use and idle for %s.", name),
[]string{"db_name"},
labels,
),
inUseDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_in_use_total", name),
fmt.Sprintf("The number of connections currently in use for %s.", name),
[]string{"db_name"},
labels,
),
idleDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_idle_total", name),
fmt.Sprintf("The number of idle connections for %s.", name),
[]string{"db_name"},
labels,
),
waitedForDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_waited_for_total", name),
fmt.Sprintf("The total number of connections waited for in %s.", name),
[]string{"db_name"},
labels,
),
blockedSecondsDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_blocked_seconds_total", name),
fmt.Sprintf("The total time blocked waiting for a new connection for %s.", name),
[]string{"db_name"},
labels,
),
closedMaxIdleDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_closed_max_idle_total", name),
fmt.Sprintf("The total number of connections closed due to SetMaxIdleConns for %s.", name),
[]string{"db_name"},
labels,
),
closedMaxLifetimeDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_closed_max_lifetime_total", name),
fmt.Sprintf("The total number of connections closed due to SetConnMaxLifetime for %s.", name),
[]string{"db_name"},
labels,
),
closedMaxIdleTimeDesc: prometheus.NewDesc(
fmt.Sprintf("%s_sql_closed_max_idle_time_total", name),
fmt.Sprintf("The total number of connections closed due to SetConnMaxIdleTime for %s.", name),
[]string{"db_name"},
labels,
),
}
}
// Describe implements the prometheus.Collector interface.
func (c StatsCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.maxOpenDesc
ch <- c.openDesc
ch <- c.inUseDesc
ch <- c.idleDesc
ch <- c.waitedForDesc
ch <- c.blockedSecondsDesc
ch <- c.closedMaxIdleDesc
ch <- c.closedMaxLifetimeDesc
ch <- c.closedMaxIdleTimeDesc
}
// Collect implements the prometheus.Collector interface.
func (c StatsCollector) Collect(ch chan<- prometheus.Metric) {
ms, _ := c.db.GetAllMasters()
for i, m := range ms {
c.collect(fmt.Sprintf("master_%d", i), m, ch)
}
ss, _ := c.db.GetAllSlaves()
for i, s := range ss {
c.collect(fmt.Sprintf("slave_%d", i), s, ch)
}
}
// Collect implements the prometheus.Collector interface.
func (c StatsCollector) collect(name string, db *sqlx.DB, ch chan<- prometheus.Metric) {
stats := db.Stats()
ch <- prometheus.MustNewConstMetric(
c.maxOpenDesc,
prometheus.GaugeValue,
float64(stats.MaxOpenConnections),
name,
)
ch <- prometheus.MustNewConstMetric(
c.openDesc,
prometheus.GaugeValue,
float64(stats.OpenConnections),
name,
)
ch <- prometheus.MustNewConstMetric(
c.inUseDesc,
prometheus.GaugeValue,
float64(stats.InUse),
name,
)
ch <- prometheus.MustNewConstMetric(
c.idleDesc,
prometheus.GaugeValue,
float64(stats.Idle),
name,
)
ch <- prometheus.MustNewConstMetric(
c.waitedForDesc,
prometheus.CounterValue,
float64(stats.WaitCount),
name,
)
ch <- prometheus.MustNewConstMetric(
c.blockedSecondsDesc,
prometheus.CounterValue,
stats.WaitDuration.Seconds(),
name,
)
ch <- prometheus.MustNewConstMetric(
c.closedMaxIdleDesc,
prometheus.CounterValue,
float64(stats.MaxIdleClosed),
name,
)
ch <- prometheus.MustNewConstMetric(
c.closedMaxLifetimeDesc,
prometheus.CounterValue,
float64(stats.MaxLifetimeClosed),
name,
)
ch <- prometheus.MustNewConstMetric(
c.closedMaxIdleTimeDesc,
prometheus.CounterValue,
float64(stats.MaxIdleTimeClosed),
name,
)
}