forked from juliusv/prometheus_demo_service
-
Notifications
You must be signed in to change notification settings - Fork 0
/
holiday.go
61 lines (52 loc) · 1.22 KB
/
holiday.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
package main
import (
"math"
"math/rand"
"sync/atomic"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
isHoliday = prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "is_holiday",
Help: "Set to 1 if it is currently a holiday, 0 otherwise.",
})
shippedItems = prometheus.NewCounter(
prometheus.CounterOpts{
Namespace: namespace,
Name: "items_shipped_total",
Help: "The total number of shipped items. Affected heavily by whether it's currently a holiday.",
})
isHolidayVar = uint64(0)
)
func init() {
prometheus.MustRegister(isHoliday)
prometheus.MustRegister(shippedItems)
}
func runHolidaySim(dayLength time.Duration, holidayRatio float64) {
start := time.Now()
go func() {
for {
shippedItems.Inc()
factor := 2 + math.Sin(1+2*math.Pi*float64(time.Since(start))/float64(dayLength))
d := 100 * factor
if isHolidayVar == 1 {
d *= 1.8
}
time.Sleep(time.Duration(d) * time.Millisecond)
}
}()
ticker := time.NewTicker(dayLength)
for {
if rand.Float64() > holidayRatio {
isHoliday.Set(0)
atomic.StoreUint64(&isHolidayVar, 0)
} else {
isHoliday.Set(1)
atomic.StoreUint64(&isHolidayVar, 1)
}
<-ticker.C
}
}