forked from MaibornWolff/elcep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogcountermonitor.go
60 lines (48 loc) · 1.71 KB
/
logcountermonitor.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
package main
import (
"time"
"github.com/MaibornWolff/elcep/monitor"
"github.com/prometheus/client_golang/prometheus"
)
//LogCounterMonitor for all Monitor based on Counter
type LogCounterMonitor struct {
Query monitor.Query
LastCount *float64
metrics struct {
matchCounter prometheus.Counter
rpcDurationHistogram prometheus.Histogram
}
}
//BuildMetrics should setup the Prometheus metrics
func (logMon *LogCounterMonitor) BuildMetrics(query monitor.Query) []prometheus.Collector {
logMon.LastCount = new(float64)
logMon.Query = query
logMon.metrics.matchCounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "elcep_logs_matched_" + logMon.Query.Name + "_total",
Help: "Counts number of matched logs for " + logMon.Query.Name,
})
logMon.metrics.rpcDurationHistogram = prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "elcep_logs_matched_rpc_durations_" + logMon.Query.Name + "_histogram_seconds",
Help: "Logs matched RPC latency distributions for " + logMon.Query.Name,
Buckets: prometheus.DefBuckets,
})
return []prometheus.Collector{logMon.metrics.matchCounter, logMon.metrics.rpcDurationHistogram}
}
//Perform executes the query for this monitor
func (logMon *LogCounterMonitor) Perform() {
increment := logMon.countLogs()
if increment < 0 {
increment = 0
}
logMon.metrics.matchCounter.Add(increment)
}
func (logMon *LogCounterMonitor) countLogs() float64 {
start := time.Now()
response, _ := logMon.Query.Exec(logMon.Query.BuildBody("0", startup_time))
end := time.Now()
duration := end.Sub(start).Seconds()
logMon.metrics.rpcDurationHistogram.Observe(duration)
increment := response.Total - *logMon.LastCount
*logMon.LastCount = response.Total
return increment
}