forked from cwtch.im/cwtch
157 lines
3.6 KiB
Go
157 lines
3.6 KiB
Go
package metrics
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
type counter struct {
|
|
startTime time.Time
|
|
count uint64
|
|
}
|
|
|
|
// Counter providers a threadsafe counter to use for storing long running counts
|
|
type Counter interface {
|
|
Add(unit uint64)
|
|
|
|
Count() uint64
|
|
GetStarttime() time.Time
|
|
}
|
|
|
|
// NewCounter initializes a counter starting at time.Now() and a count of 0 and returns it
|
|
func NewCounter() Counter {
|
|
c := &counter{startTime: time.Now(), count: 0}
|
|
return c
|
|
}
|
|
|
|
// Add add a count of unit to the counter
|
|
func (c *counter) Add(unit uint64) {
|
|
atomic.AddUint64(&c.count, unit)
|
|
}
|
|
|
|
// Count returns the count since Start
|
|
func (c *counter) Count() uint64 {
|
|
return atomic.LoadUint64(&c.count)
|
|
}
|
|
|
|
// GetStarttime returns the starttime of the counter
|
|
func (c *counter) GetStarttime() time.Time {
|
|
return c.startTime
|
|
}
|
|
|
|
type monitorHistory struct {
|
|
starttime time.Time
|
|
perMinutePerHour [60]int
|
|
perHourForDay [24]int
|
|
perDayForWeek [7]int
|
|
perWeekForMonth [4]int
|
|
perMonthForYear [12]int
|
|
|
|
monitor func() int
|
|
|
|
breakChannel chan bool
|
|
lock sync.Mutex
|
|
}
|
|
|
|
// MonitorHistory runs a monitor every minute and rotates and averages the results out across time
|
|
type MonitorHistory interface {
|
|
Start()
|
|
Stop()
|
|
|
|
Minutes() []int
|
|
Hours() []int
|
|
Days() []int
|
|
Weeks() []int
|
|
Months() []int
|
|
}
|
|
|
|
// NewMonitorHistory returns a new MonitorHistory with starttime of time.Now and Started running with supplied monitor
|
|
func NewMonitorHistory(monitor func() int) MonitorHistory {
|
|
mh := &monitorHistory{starttime: time.Now(), monitor: monitor, breakChannel: make(chan bool)}
|
|
mh.Start()
|
|
return mh
|
|
}
|
|
|
|
// Start starts a monitorHistory go rountine to run the monitor at intervals and rotate history
|
|
func (mh *monitorHistory) Start() {
|
|
go mh.monitorThread()
|
|
}
|
|
|
|
// Stop stops a monitorHistory go routine
|
|
func (mh *monitorHistory) Stop() {
|
|
mh.breakChannel <- true
|
|
}
|
|
|
|
// Minutes returns the last 60 minute monitoring results
|
|
func (mh *monitorHistory) Minutes() []int {
|
|
return mh.returnCopy(mh.perMinutePerHour[:])
|
|
}
|
|
|
|
// Hours returns the last 24 hourly averages of monitor results
|
|
func (mh *monitorHistory) Hours() []int {
|
|
return mh.returnCopy(mh.perHourForDay[:])
|
|
}
|
|
|
|
// Days returns the last 7 day averages of monitor results
|
|
func (mh *monitorHistory) Days() []int {
|
|
return mh.returnCopy(mh.perDayForWeek[:])
|
|
}
|
|
|
|
// Weeks returns the last 4 weeks of averages of monitor results
|
|
func (mh *monitorHistory) Weeks() []int {
|
|
return mh.returnCopy(mh.perWeekForMonth[:])
|
|
}
|
|
|
|
// Months returns the last 12 months of averages of monitor results
|
|
func (mh *monitorHistory) Months() []int {
|
|
return mh.returnCopy(mh.perMonthForYear[:])
|
|
}
|
|
|
|
func (mh *monitorHistory) returnCopy(slice []int) []int {
|
|
retSlice := make([]int, len(slice))
|
|
mh.lock.Lock()
|
|
for i, v := range slice {
|
|
retSlice[i] = v
|
|
}
|
|
mh.lock.Unlock()
|
|
return retSlice
|
|
}
|
|
|
|
func rotateAndAvg(array []int, newVal int) int {
|
|
total := 0
|
|
for i := len(array) - 1; i > 0; i-- {
|
|
array[i] = array[i-1]
|
|
total += array[i]
|
|
}
|
|
array[0] = newVal
|
|
total += newVal
|
|
return total / len(array)
|
|
}
|
|
|
|
// monitorThread is the goroutine in a monitorHistory that does per minute monitoring and rotation
|
|
func (mh *monitorHistory) monitorThread() {
|
|
timeout := time.Duration(0) // first pass right away
|
|
|
|
for {
|
|
select {
|
|
case <-time.After(timeout):
|
|
mh.lock.Lock()
|
|
|
|
minuteAvg := rotateAndAvg(mh.perMinutePerHour[:], mh.monitor())
|
|
hourAvg := rotateAndAvg(mh.perHourForDay[:], minuteAvg)
|
|
dayAvg := rotateAndAvg(mh.perDayForWeek[:], hourAvg)
|
|
weekAvg := rotateAndAvg(mh.perWeekForMonth[:], dayAvg)
|
|
rotateAndAvg(mh.perMonthForYear[:], weekAvg)
|
|
|
|
mh.lock.Unlock()
|
|
|
|
// Repeat every minute
|
|
timeout = time.Duration(time.Minute)
|
|
|
|
case <-mh.breakChannel:
|
|
return
|
|
}
|
|
}
|
|
}
|