Repository navigation
Expand file tree
/
Copy pathcollector.go
More file actions
456 lines (360 loc) · 16 KB
/
Copy pathcollector.go
File metadata and controls
456 lines (360 loc) · 16 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package main
import (
"fmt"
jp "github.com/buger/jsonparser"
"github.com/prometheus/client_golang/prometheus"
)
const (
namespace = "eventstore"
subsystem = ""
)
type exporter struct {
up prometheus.Gauge
processCPU prometheus.Gauge
processCPUScaled prometheus.Gauge
processMemoryBytes prometheus.Gauge
diskIoReadBytes prometheus.Counter
diskIoWrittenBytes prometheus.Counter
diskIoReadOps prometheus.Counter
diskIoWriteOps prometheus.Counter
uptimeSeconds prometheus.Counter
tcpSentBytes prometheus.Counter
tcpReceivedBytes prometheus.Counter
tcpConnections prometheus.Gauge
queueLength *prometheus.GaugeVec
queueItemsProcessed *prometheus.CounterVec
driveTotalBytes *prometheus.GaugeVec
driveAvailableBytes *prometheus.GaugeVec
projectionRunning *prometheus.GaugeVec
projectionProgress *prometheus.GaugeVec
projectionEventsProcessedAfterRestart *prometheus.CounterVec
clusterMemberAlive *prometheus.GaugeVec
clusterMemberIsMaster prometheus.Gauge
clusterMemberIsSlave prometheus.Gauge
clusterMemberIsClone prometheus.Gauge
subscriptionTotalItemsProcessed *prometheus.CounterVec
subscriptionLastProcessedEventNumber *prometheus.GaugeVec
subscriptionLastKnownEventNumber *prometheus.GaugeVec
subscriptionConnectionCount *prometheus.GaugeVec
subscriptionTotalInFlightMessages *prometheus.GaugeVec
}
func newExporter() *exporter {
return &exporter{
up: createGauge("up", "Whether the EventStore scrape was successful"),
processCPU: createGauge("process_cpu", "Process CPU usage, 0 - number of cores"),
processCPUScaled: createGauge("process_cpu_scaled", "Process CPU usage scaled to number of cores, 0 - 1, 1 = full load on all cores"),
processMemoryBytes: createGauge("process_memory_bytes", "Process memory usage, as reported by EventStore"),
diskIoReadBytes: createCounter("disk_io_read_bytes", "Total number of disk IO read bytes"),
diskIoWrittenBytes: createCounter("disk_io_written_bytes", "Total number of disk IO written bytes"),
diskIoReadOps: createCounter("disk_io_read_ops", "Total number of disk IO read operations"),
diskIoWriteOps: createCounter("disk_io_write_ops", "Total number of disk IO write operations"),
uptimeSeconds: createCounter("uptime_seconds", "Total uptime seconds"),
tcpSentBytes: createCounter("tcp_sent_bytes", "TCP sent bytes"),
tcpReceivedBytes: createCounter("tcp_received_bytes", "TCP received bytes"),
tcpConnections: createGauge("tcp_connections", "Current number of TCP connections"),
queueLength: createItemGaugeVec("queue_length", "Queue length", []string{"queue"}),
queueItemsProcessed: createItemCounterVec("queue_items_processed_total", "Total number items processed by queue", []string{"queue"}),
driveTotalBytes: createItemGaugeVec("drive_total_bytes", "Drive total size in bytes", []string{"drive"}),
driveAvailableBytes: createItemGaugeVec("drive_available_bytes", "Drive available bytes", []string{"drive"}),
projectionRunning: createItemGaugeVec("projection_running", "If 1, projection is in 'Running' state", []string{"projection"}),
projectionProgress: createItemGaugeVec("projection_progress", "Projection progress 0 - 1, where 1 = projection progress at 100%", []string{"projection"}),
projectionEventsProcessedAfterRestart: createItemCounterVec("projection_events_processed_after_restart_total", "Projection event processed count", []string{"projection"}),
clusterMemberAlive: createItemGaugeVec("cluster_member_alive", "If 1, cluster member is alive, as seen from current cluster member", []string{"member"}),
clusterMemberIsMaster: createGauge("cluster_member_is_master", "If 1, current cluster member is the master"),
clusterMemberIsSlave: createGauge("cluster_member_is_slave", "If 1, current cluster member is a slave"),
clusterMemberIsClone: createGauge("cluster_member_is_clone", "If 1, current cluster member is a clone"),
subscriptionTotalItemsProcessed: createItemCounterVec("subscription_items_processed_total", "Total items processed by subscription", []string{"event_stream_id", "group_name"}),
subscriptionLastProcessedEventNumber: createItemGaugeVec("subscription_last_processed_event_number", "Last event number processed by subscription", []string{"event_stream_id", "group_name"}),
subscriptionLastKnownEventNumber: createItemGaugeVec("subscription_last_known_event_number", "Last known event number in subscription", []string{"event_stream_id", "group_name"}),
subscriptionConnectionCount: createItemGaugeVec("subscription_connections", "Number of connections to subscription", []string{"event_stream_id", "group_name"}),
subscriptionTotalInFlightMessages: createItemGaugeVec("subscription_messages_in_flight", "Number of messages in flight for subscription", []string{"event_stream_id", "group_name"}),
}
}
func (e *exporter) Describe(ch chan<- *prometheus.Desc) {
ch <- e.up.Desc()
ch <- e.processCPU.Desc()
ch <- e.processMemoryBytes.Desc()
ch <- e.diskIoReadBytes.Desc()
ch <- e.diskIoWrittenBytes.Desc()
ch <- e.diskIoReadOps.Desc()
ch <- e.diskIoWriteOps.Desc()
ch <- e.uptimeSeconds.Desc()
ch <- e.tcpSentBytes.Desc()
ch <- e.tcpReceivedBytes.Desc()
ch <- e.tcpConnections.Desc()
e.queueLength.Describe(ch)
e.queueItemsProcessed.Describe(ch)
e.driveTotalBytes.Describe(ch)
e.driveAvailableBytes.Describe(ch)
e.projectionRunning.Describe(ch)
e.projectionProgress.Describe(ch)
e.projectionEventsProcessedAfterRestart.Describe(ch)
if isInClusterMode() {
e.clusterMemberAlive.Describe(ch)
ch <- e.clusterMemberIsMaster.Desc()
ch <- e.clusterMemberIsSlave.Desc()
ch <- e.clusterMemberIsClone.Desc()
}
}
func (e *exporter) Collect(ch chan<- prometheus.Metric) {
log.Info("Running scrape")
if stats, err := getStats(); err != nil {
log.WithError(err).Error("Error while getting data from EventStore")
e.up.Set(0)
ch <- e.up
} else {
e.up.Set(1)
ch <- e.up
e.processCPU.Set(getProcessCPU(stats))
ch <- e.processCPU
e.processCPUScaled.Set(getProcessCPUScaled(stats))
ch <- e.processCPUScaled
e.processMemoryBytes.Set(getProcessMemory(stats))
ch <- e.processMemoryBytes
e.diskIoReadBytes.Set(getDiskIoReadBytes(stats))
ch <- e.diskIoReadBytes
e.diskIoWrittenBytes.Set(getDiskIoWrittenBytes(stats))
ch <- e.diskIoWrittenBytes
e.diskIoReadOps.Set(getDiskIoReadOps(stats))
ch <- e.diskIoReadOps
e.diskIoWriteOps.Set(getDiskIoWriteOps(stats))
ch <- e.diskIoWriteOps
e.tcpConnections.Set(getTCPConnections(stats))
ch <- e.tcpConnections
e.tcpReceivedBytes.Set(getTCPReceivedBytes(stats))
ch <- e.tcpReceivedBytes
e.tcpSentBytes.Set(getTCPSentBytes(stats))
ch <- e.tcpSentBytes
e.clusterMemberIsMaster.Set(getIsMaster(stats))
ch <- e.clusterMemberIsMaster
e.clusterMemberIsSlave.Set(getIsSlave(stats))
ch <- e.clusterMemberIsSlave
e.clusterMemberIsClone.Set(getIsClone(stats))
ch <- e.clusterMemberIsClone
collectPerQueueGauge(stats, e.queueLength, getQueueLength, ch)
collectPerQueueCounter(stats, e.queueItemsProcessed, getQueueItemsProcessed, ch)
collectPerDriveGauge(stats, e.driveTotalBytes, getDriveTotalBytes, ch)
collectPerDriveGauge(stats, e.driveAvailableBytes, getDriveAvailableBytes, ch)
collectPerProjectionGauge(stats, e.projectionRunning, getProjectionIsRunning, ch)
collectPerProjectionGauge(stats, e.projectionProgress, getProjectionProgress, ch)
collectPerProjectionCounter(stats, e.projectionEventsProcessedAfterRestart, getProjectionEventsProcessedAfterRestart, ch)
collectPerMemberGauge(stats, e.clusterMemberAlive, getMemberIsAlive, ch)
collectPerSubscriptionCounter(stats, e.subscriptionTotalItemsProcessed, getSubscriptionTotalItemsProcessed, ch)
collectPerSubscriptionGauge(stats, e.subscriptionConnectionCount, getSubscriptionConnectionCount, ch)
collectPerSubscriptionGauge(stats, e.subscriptionLastKnownEventNumber, getSubscriptionLastKnownEventNumber, ch)
collectPerSubscriptionGauge(stats, e.subscriptionLastProcessedEventNumber, getSubscriptionLastProcessedEventNumber, ch)
collectPerSubscriptionGauge(stats, e.subscriptionTotalInFlightMessages, getSubscriptionTotalInFlightMessages, ch)
}
}
func collectPerMemberGauge(stats *stats, vec *prometheus.GaugeVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ArrayEach(stats.gossipStats, func(value []byte, dataType jp.ValueType, offset int, err error) {
externalHTTPIp, _ := jp.GetString(value, "externalHttpIp")
externalHTTPPort, _ := jp.GetInt(value, "externalHttpPort")
memberName := fmt.Sprintf("%s:%d", externalHTTPIp, externalHTTPPort)
vec.WithLabelValues(memberName).Set(collectFunc(value))
}, "members")
vec.Collect(ch)
}
func getMemberIsAlive(member []byte) float64 {
alive, _ := jp.GetBoolean(member, "isAlive")
if alive {
return 1
}
return 0
}
func collectPerProjectionGauge(stats *stats, vec *prometheus.GaugeVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ArrayEach(stats.projectionStats, func(value []byte, dataType jp.ValueType, offset int, err error) {
projectionName, _ := jp.GetString(value, "effectiveName")
vec.WithLabelValues(projectionName).Set(collectFunc(value))
}, "projections")
vec.Collect(ch)
}
func collectPerProjectionCounter(stats *stats, vec *prometheus.CounterVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ArrayEach(stats.projectionStats, func(value []byte, dataType jp.ValueType, offset int, err error) {
projectionName, _ := jp.GetString(value, "effectiveName")
vec.WithLabelValues(projectionName).Set(collectFunc(value))
}, "projections")
vec.Collect(ch)
}
func getProjectionIsRunning(projection []byte) float64 {
status, _ := jp.GetString(projection, "status")
if status == "Running" {
return 1
}
return 0
}
func getProjectionProgress(projection []byte) float64 {
progress, _ := jp.GetFloat(projection, "progress")
return progress / 100.0 // scale to 0-1
}
func getProjectionEventsProcessedAfterRestart(projection []byte) float64 {
processed, _ := jp.GetFloat(projection, "eventsProcessedAfterRestart")
return processed
}
func collectPerQueueGauge(stats *stats, vec *prometheus.GaugeVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ObjectEach(stats.serverStats, func(key []byte, value []byte, dataType jp.ValueType, offset int) error {
queueName := string(key)
vec.WithLabelValues(queueName).Set(collectFunc(value))
return nil
}, "es", "queue")
vec.Collect(ch)
}
func collectPerQueueCounter(stats *stats, vec *prometheus.CounterVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ObjectEach(stats.serverStats, func(key []byte, value []byte, dataType jp.ValueType, offset int) error {
queueName := string(key)
vec.WithLabelValues(queueName).Set(collectFunc(value))
return nil
}, "es", "queue")
vec.Collect(ch)
}
func getQueueLength(queue []byte) float64 {
value, _ := jp.GetFloat(queue, "length")
return value
}
func getQueueItemsProcessed(queue []byte) float64 {
value, _ := jp.GetFloat(queue, "totalItemsProcessed")
return value
}
func collectPerDriveGauge(stats *stats, vec *prometheus.GaugeVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ObjectEach(stats.serverStats, func(key []byte, value []byte, dataType jp.ValueType, offset int) error {
drive := string(key)
vec.WithLabelValues(drive).Set(collectFunc(value))
return nil
}, "sys", "drive")
vec.Collect(ch)
}
func getDriveTotalBytes(drive []byte) float64 {
value, _ := jp.GetFloat(drive, "totalBytes")
return value
}
func getDriveAvailableBytes(drive []byte) float64 {
value, _ := jp.GetFloat(drive, "availableBytes")
return value
}
func collectPerSubscriptionCounter(stats *stats, vec *prometheus.CounterVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ArrayEach(stats.subscriptionsStats, func(value []byte, dataType jp.ValueType, offset int, err error) {
eventStreamId, _ := jp.GetString(value, "eventStreamId")
groupName, _ := jp.GetString(value, "groupName")
vec.WithLabelValues(eventStreamId, groupName).Set(collectFunc(value))
})
vec.Collect(ch)
}
func collectPerSubscriptionGauge(stats *stats, vec *prometheus.GaugeVec, collectFunc func([]byte) float64, ch chan<- prometheus.Metric) {
jp.ArrayEach(stats.subscriptionsStats, func(value []byte, dataType jp.ValueType, offset int, err error) {
eventStreamId, _ := jp.GetString(value, "eventStreamId")
groupName, _ := jp.GetString(value, "groupName")
vec.WithLabelValues(eventStreamId, groupName).Set(collectFunc(value))
})
vec.Collect(ch)
}
func getSubscriptionTotalItemsProcessed(subscription []byte) float64 {
value, _ := jp.GetFloat(subscription, "totalItemsProcessed")
return value
}
func getSubscriptionConnectionCount(subscription []byte) float64 {
value, _ := jp.GetFloat(subscription, "connectionCount")
return value
}
func getSubscriptionLastProcessedEventNumber(subscription []byte) float64 {
value, _ := jp.GetFloat(subscription, "lastProcessedEventNumber")
return value
}
func getSubscriptionLastKnownEventNumber(subscription []byte) float64 {
value, _ := jp.GetFloat(subscription, "lastKnownEventNumber")
return value
}
func getSubscriptionTotalInFlightMessages(subscription []byte) float64 {
value, _ := jp.GetFloat(subscription, "totalInFlightMessages")
return value
}
func getProcessCPU(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "cpu")
return value / 100.0
}
func getProcessCPUScaled(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "cpuScaled")
return value / 100.0
}
func getProcessMemory(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "mem")
return value
}
func getDiskIoReadBytes(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "diskIo", "readBytes")
return value
}
func getDiskIoWrittenBytes(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "diskIo", "writtenBytes")
return value
}
func getDiskIoReadOps(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "diskIo", "readOps")
return value
}
func getDiskIoWriteOps(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "diskIo", "writeOps")
return value
}
func getTCPSentBytes(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "tcp", "sentBytesTotal")
return value
}
func getTCPReceivedBytes(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "tcp", "receivedBytesTotal")
return value
}
func getTCPConnections(stats *stats) float64 {
value, _ := jp.GetFloat(stats.serverStats, "proc", "tcp", "connections")
return value
}
func getIsMaster(stats *stats) float64 {
return getIs("master", stats)
}
func getIsSlave(stats *stats) float64 {
return getIs("slave", stats)
}
func getIsClone(stats *stats) float64 {
return getIs("clone", stats)
}
func getIs(status string, stats *stats) float64 {
value, _ := jp.GetString(stats.info, "state")
if value == status {
return 1
}
return 0
}
func createGauge(name string, help string) prometheus.Gauge {
return prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
})
}
func createItemGaugeVec(name string, help string, itemLabels []string) *prometheus.GaugeVec {
return prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
}, itemLabels)
}
func createCounter(name string, help string) prometheus.Counter {
return prometheus.NewCounter(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
})
}
func createItemCounterVec(name string, help string, itemLabels []string) *prometheus.CounterVec {
return prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Subsystem: subsystem,
Name: name,
Help: help,
}, itemLabels)
}
func isInClusterMode() bool {
return clusterMode == "cluster"
}