Skip to content

Commit

Permalink
Add fake memory metrics
Browse files Browse the repository at this point in the history
Signed-off-by: Julius Volz <[email protected]>
  • Loading branch information
juliusv committed Nov 3, 2020
1 parent 5dbdd16 commit 640b6d0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func main() {
go runCPUSim(4, 0.3, 0.2)
go runDiskSim(160*1e9, 0.5*1e6)
go runHolidaySim(5*time.Minute, 0.2)
go runMemorySim(8*1024*1024*1024, 1200*1024*1024, 2500*1024*1024, 165*1024*1024, 0.5)
prometheus.MustRegister(intermittentMetric{})

select {}
Expand Down
49 changes: 49 additions & 0 deletions memory.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"math/rand"
"time"

"github.com/prometheus/client_golang/prometheus"
)

var memoryUsage = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: namespace,
Name: "memory_usage_bytes",
Help: "The memory usage by type.",
},
[]string{"type"},
)

func init() {
prometheus.MustRegister(memoryUsage)
}

func runMemorySim(total, usedBase, cachedBase, buffersBase, maxDeviation float64) {
var used, cached, buffers = usedBase, cachedBase, buffersBase

randomStep := func(current, base float64) float64 {
current += (rand.Float64() - 0.5) * 60 * 1024 * 1024
if current < base-base*maxDeviation {
current = base
}
if current > base+base*maxDeviation {
current = base
}
return current
}

for {
used = randomStep(used, usedBase)
cached = randomStep(cached, cachedBase)
buffers = randomStep(buffers, buffersBase)

memoryUsage.WithLabelValues("used").Set(used)
memoryUsage.WithLabelValues("cached").Set(cached)
memoryUsage.WithLabelValues("buffers").Set(buffers)
memoryUsage.WithLabelValues("free").Set(total - used - cached - buffers)

time.Sleep(100 * time.Millisecond)
}
}

0 comments on commit 640b6d0

Please sign in to comment.