From 849bdc9d337a749b2ff667bc2854edd499597007 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Mon, 26 Aug 2019 19:45:14 +0200 Subject: [PATCH] Add holiday-based metrics Signed-off-by: Julius Volz --- holiday.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 1 + 2 files changed, 62 insertions(+) create mode 100644 holiday.go diff --git a/holiday.go b/holiday.go new file mode 100644 index 0000000..bd2efbc --- /dev/null +++ b/holiday.go @@ -0,0 +1,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 + } +} diff --git a/main.go b/main.go index 09c702c..e7f14bf 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ func main() { go runBatchJobs(time.Minute, 10*time.Second, 0.25) go runCPUSim(4, 0.3, 0.2) go runDiskSim(160*1e9, 0.5*1e6) + go runHolidaySim(5*time.Minute, 0.2) select {} }