forked from juliusv/prometheus_demo_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
batch.go
71 lines (61 loc) · 1.75 KB
/
batch.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
package main
import (
"math/rand"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
lastSuccess = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "batch",
Name: "last_success_timestamp_seconds",
Help: "The Unix timestamp in seconds since the last successful demo batch job completion.",
},
)
lastRun = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "batch",
Name: "last_run_timestamp_seconds",
Help: "The Unix timestamp in seconds since the last demo batch job run.",
},
)
lastDuration = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "batch",
Name: "last_run_duration_seconds",
Help: "The duration in seconds of the last batch job run.",
},
)
processedBytes = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "batch",
Name: "last_run_processed_bytes",
Help: "The number of bytes processed by the demo batch job in the last run.",
})
)
func init() {
prometheus.MustRegister(lastSuccess)
prometheus.MustRegister(lastRun)
prometheus.MustRegister(lastDuration)
prometheus.MustRegister(processedBytes)
}
func runBatchJobs(interval time.Duration, duration time.Duration, failureRatio float64) {
lastTime := float64(time.Now().UnixNano()) / 1e9
ticker := time.NewTicker(interval)
for {
time.Sleep(duration + time.Second - time.Duration((rand.Int()%2000))*time.Microsecond)
now := float64(time.Now().UnixNano()) / 1e9
if rand.Float64() > failureRatio {
lastSuccess.Set(now)
processedBytes.Set(float64(1e6 + 1e5 - rand.Int()%1e5))
}
lastRun.Set(now)
lastDuration.Set(float64(now - lastTime))
lastTime = now
<-ticker.C
}
}