Skip to content

Commit

Permalink
Add holiday-based metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Volz <[email protected]>
  • Loading branch information
juliusv committed Aug 26, 2019
1 parent 3f1ef5c commit 849bdc9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
61 changes: 61 additions & 0 deletions holiday.go
Original file line number Diff line number Diff line change
@@ -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
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}

0 comments on commit 849bdc9

Please sign in to comment.