@@ -41,22 +41,6 @@ const infoSchemaProcesslistQuery = `
41
41
ORDER BY null
42
42
`
43
43
44
- // Tunable flags.
45
- var (
46
- processlistMinTime = kingpin .Flag (
47
- "collect.info_schema.processlist.min_time" ,
48
- "Minimum time a thread must be in each state to be counted" ,
49
- ).Default ("0" ).Int ()
50
- processesByUserFlag = kingpin .Flag (
51
- "collect.info_schema.processlist.processes_by_user" ,
52
- "Enable collecting the number of processes by user" ,
53
- ).Default ("true" ).Bool ()
54
- processesByHostFlag = kingpin .Flag (
55
- "collect.info_schema.processlist.processes_by_host" ,
56
- "Enable collecting the number of processes by host" ,
57
- ).Default ("true" ).Bool ()
58
- )
59
-
60
44
// Metric descriptors.
61
45
var (
62
46
processlistCountDesc = prometheus .NewDesc (
@@ -159,7 +143,11 @@ var (
159
143
)
160
144
161
145
// ScrapeProcesslist collects from `information_schema.processlist`.
162
- type ScrapeProcesslist struct {}
146
+ type ScrapeProcesslist struct {
147
+ ProcessListMinTime int
148
+ ProcessesByUserFlag bool
149
+ ProcessesByHostFlag bool
150
+ }
163
151
164
152
// Name of the Scraper. Should be unique.
165
153
func (ScrapeProcesslist ) Name () string {
@@ -176,11 +164,27 @@ func (ScrapeProcesslist) Version() float64 {
176
164
return 5.1
177
165
}
178
166
167
+ // RegisterFlags adds flags to configure the Scraper.
168
+ func (s * ScrapeProcesslist ) RegisterFlags (application * kingpin.Application ) {
169
+ application .Flag (
170
+ "collect.info_schema.processlist.min_time" ,
171
+ "Minimum time a thread must be in each state to be counted" ,
172
+ ).Default ("0" ).IntVar (& s .ProcessListMinTime )
173
+ application .Flag (
174
+ "collect.info_schema.processlist.processes_by_user" ,
175
+ "Enable collecting the number of processes by user" ,
176
+ ).Default ("true" ).BoolVar (& s .ProcessesByUserFlag )
177
+ application .Flag (
178
+ "collect.info_schema.processlist.processes_by_host" ,
179
+ "Enable collecting the number of processes by host" ,
180
+ ).Default ("true" ).BoolVar (& s .ProcessesByHostFlag )
181
+ }
182
+
179
183
// Scrape collects data from database connection and sends it over channel as prometheus metric.
180
- func (ScrapeProcesslist ) Scrape (ctx context.Context , db * sql.DB , ch chan <- prometheus.Metric , logger log.Logger ) error {
184
+ func (s ScrapeProcesslist ) Scrape (ctx context.Context , db * sql.DB , ch chan <- prometheus.Metric , logger log.Logger ) error {
181
185
processQuery := fmt .Sprintf (
182
186
infoSchemaProcesslistQuery ,
183
- * processlistMinTime ,
187
+ s . ProcessListMinTime ,
184
188
)
185
189
processlistRows , err := db .QueryContext (ctx , processQuery )
186
190
if err != nil {
@@ -217,13 +221,13 @@ func (ScrapeProcesslist) Scrape(ctx context.Context, db *sql.DB, ch chan<- prome
217
221
userCount [user ] = userCount [user ] + processes
218
222
}
219
223
220
- if * processesByHostFlag {
224
+ if s . ProcessesByHostFlag {
221
225
for host , processes := range hostCount {
222
226
ch <- prometheus .MustNewConstMetric (processesByHostDesc , prometheus .GaugeValue , float64 (processes ), host )
223
227
}
224
228
}
225
229
226
- if * processesByUserFlag {
230
+ if s . ProcessesByUserFlag {
227
231
for user , processes := range userCount {
228
232
ch <- prometheus .MustNewConstMetric (processesByUserDesc , prometheus .GaugeValue , float64 (processes ), user )
229
233
}
0 commit comments