-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcollector.go
300 lines (273 loc) · 10.1 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2020 Fandom, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package main
import (
"bufio"
"net"
"strconv"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
)
const (
namespace = "poolcounter"
totalAcquired = "total_acquired"
totalReleases = "total_releases"
hashTableEntries = "hashtable_entries"
processingWorkers = "processing_workers"
waitingWorkers = "waiting_workers"
connectErrors = "connect_errors"
fullQueues = "full_queues"
lockMismatch = "lock_mismatch"
releaseMismatch = "release_mismatch"
processedCount = "processed_count"
)
// PoolCounterCollector handles scraping metrics from a poolcounter instance.
type PoolCounterCollector struct {
poolCounterAddress string
collectorTimeoutSeconds int
up *prometheus.Desc
totalProcessingTimeSeconds *prometheus.Desc
averageProcessingTimeSeconds *prometheus.Desc
totalGainedTimeSeconds *prometheus.Desc
totalExclusiveWaitTimeSeconds *prometheus.Desc
totalSharedWaitTimeSeconds *prometheus.Desc
totalAcquired *prometheus.Desc
totalReleases *prometheus.Desc
hashTableEntries *prometheus.Desc
processingWorkers *prometheus.Desc
waitingWorkers *prometheus.Desc
connectErrors *prometheus.Desc
failedSends *prometheus.Desc
fullQueues *prometheus.Desc
lockMismatch *prometheus.Desc
releaseMismatch *prometheus.Desc
processedCount *prometheus.Desc
}
// newPoolCounterCollector initializes and returns a new PoolCounterCollector instance based on scraper configuration.
func newPoolCounterCollector(configuration PrometheusExporterConfiguration) *PoolCounterCollector {
return &PoolCounterCollector{
poolCounterAddress: configuration.PoolCounterAddress,
collectorTimeoutSeconds: configuration.CollectorTimeoutSeconds,
up: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "up"),
"Whether poolcounter is up and responding to the exporter",
nil,
nil,
),
totalProcessingTimeSeconds: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "total_processing_time_seconds"),
"Total processing time in seconds",
nil,
nil,
),
averageProcessingTimeSeconds: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "avg_processing_time_seconds"),
"Average processing time in seconds",
nil,
nil,
),
totalGainedTimeSeconds: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "total_gained_time_seconds"),
"Total processing time saved by the use of PoolCounter in seconds",
nil,
nil,
),
totalExclusiveWaitTimeSeconds: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "total_excl_wait_time_seconds"),
"Total waiting time for exclusive locks in seconds",
nil,
nil,
),
totalSharedWaitTimeSeconds: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "total_shared_wait_time_seconds"),
"Total waiting time for shared locks in seconds",
nil,
nil,
),
totalAcquired: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", totalAcquired),
"Total acquired locks count",
nil,
nil,
),
totalReleases: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", totalReleases),
"Total released locks count",
nil,
nil,
),
hashTableEntries: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", hashTableEntries),
"Number of entries in poolcounter hash table",
nil,
nil,
),
processingWorkers: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", processingWorkers),
"Number of workers busy processing tasks",
nil,
nil,
),
waitingWorkers: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", waitingWorkers),
"Number of workers waiting for tasks to be completed",
nil,
nil,
),
connectErrors: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", connectErrors),
"Total count of client connection errors",
nil,
nil,
),
fullQueues: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", fullQueues),
"Number of queues full of waiting workers",
nil,
nil,
),
lockMismatch: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", lockMismatch),
"Total count of mismatched lock requests",
nil,
nil,
),
releaseMismatch: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", releaseMismatch),
"Total count of mismatched release requests",
nil,
nil,
),
processedCount: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", processedCount),
"Total count of processed tasks",
nil,
nil,
),
}
}
// parseTimeDescription takes a poolcounter time string (e.g. 389 days 9343h 3m 28.000000s)
// and returns its duration in seconds.
func parseTimeDescription(description string) float64 {
segments := strings.Split(description, " days ")
if len(segments) > 1 {
days, _ := strconv.ParseInt(segments[0], 10, 0)
duration, _ := time.ParseDuration(strings.Replace(segments[1], " ", "", -1))
return float64(24*60*60*days) + duration.Seconds()
}
duration, _ := time.ParseDuration(strings.Replace(segments[0], " ", "", -1))
return duration.Seconds()
}
func (collector *PoolCounterCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- collector.totalProcessingTimeSeconds
ch <- collector.averageProcessingTimeSeconds
ch <- collector.up
ch <- collector.totalAcquired
ch <- collector.totalReleases
ch <- collector.hashTableEntries
ch <- collector.processingWorkers
ch <- collector.waitingWorkers
ch <- collector.connectErrors
ch <- collector.fullQueues
ch <- collector.lockMismatch
ch <- collector.releaseMismatch
ch <- collector.processedCount
}
func (collector *PoolCounterCollector) Collect(ch chan<- prometheus.Metric) {
var finalErr error = nil
var upValue float64 = 1 // 1 or 0
defer func() {
ch <- prometheus.MustNewConstMetric(collector.up, prometheus.GaugeValue, upValue)
if finalErr != nil {
log.Error(finalErr)
}
}()
conn, finalErr := net.Dial("tcp", collector.poolCounterAddress)
if finalErr != nil {
upValue = 0
return
}
defer conn.Close()
conn.SetDeadline(time.Now().Add(time.Duration(collector.collectorTimeoutSeconds) * time.Second))
_, finalErr = conn.Write([]byte("STATS FULL\n"))
if finalErr != nil {
upValue = 0
return
}
scanner := bufio.NewScanner(conn)
for scanner.Scan() {
parts := strings.Split(scanner.Text(), ": ")
if len(parts) < 2 {
break // sanity
}
name, value := parts[0], parts[1]
switch name {
case "total processing time":
value := parseTimeDescription(value)
ch <- prometheus.MustNewConstMetric(collector.totalProcessingTimeSeconds, prometheus.CounterValue, value)
case "average processing time":
value := parseTimeDescription(value)
ch <- prometheus.MustNewConstMetric(collector.averageProcessingTimeSeconds, prometheus.GaugeValue, value)
case "gained time":
value := parseTimeDescription(value)
ch <- prometheus.MustNewConstMetric(collector.totalGainedTimeSeconds, prometheus.CounterValue, value)
case "waiting time for me":
value := parseTimeDescription(value)
ch <- prometheus.MustNewConstMetric(collector.totalExclusiveWaitTimeSeconds, prometheus.CounterValue, value)
case "waiting time for anyone":
value := parseTimeDescription(value)
ch <- prometheus.MustNewConstMetric(collector.totalSharedWaitTimeSeconds, prometheus.CounterValue, value)
case totalAcquired:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.totalAcquired, prometheus.CounterValue, value)
case totalReleases:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.totalReleases, prometheus.CounterValue, value)
case hashTableEntries:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.hashTableEntries, prometheus.GaugeValue, value)
case processingWorkers:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.processingWorkers, prometheus.GaugeValue, value)
case waitingWorkers:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.waitingWorkers, prometheus.GaugeValue, value)
case connectErrors:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.connectErrors, prometheus.CounterValue, value)
case fullQueues:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.fullQueues, prometheus.CounterValue, value)
case lockMismatch:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.lockMismatch, prometheus.CounterValue, value)
case releaseMismatch:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.releaseMismatch, prometheus.CounterValue, value)
case processedCount:
value, _ := strconv.ParseFloat(value, 0)
ch <- prometheus.MustNewConstMetric(collector.processedCount, prometheus.CounterValue, value)
}
}
if finalErr = scanner.Err(); finalErr != nil {
upValue = 0
}
}