2018-06-21 18:29:02 +00:00
|
|
|
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 {
|
2018-06-23 16:04:05 +00:00
|
|
|
Add(unit int)
|
|
|
|
Reset()
|
2018-06-21 18:29:02 +00:00
|
|
|
|
2018-06-23 16:04:05 +00:00
|
|
|
Count() int
|
2018-06-21 18:29:02 +00:00
|
|
|
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
|
2018-06-23 16:04:05 +00:00
|
|
|
func (c *counter) Add(unit int) {
|
|
|
|
atomic.AddUint64(&c.count, uint64(unit))
|
2018-06-21 18:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Count returns the count since Start
|
2018-06-23 16:04:05 +00:00
|
|
|
func (c *counter) Count() int {
|
|
|
|
return int(atomic.LoadUint64(&c.count))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *counter) Reset() {
|
|
|
|
atomic.StoreUint64(&c.count, 0)
|
|
|
|
c.startTime = time.Now()
|
2018-06-21 18:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetStarttime returns the starttime of the counter
|
|
|
|
func (c *counter) GetStarttime() time.Time {
|
|
|
|
return c.startTime
|
|
|
|
}
|
|
|
|
|
|
|
|
type monitorHistory struct {
|
2018-06-23 16:04:05 +00:00
|
|
|
starttime time.Time
|
|
|
|
perMinutePerHour [60]float64
|
|
|
|
timeLastHourRotate time.Time
|
|
|
|
perHourForDay [24]float64
|
|
|
|
timeLastDayRotate time.Time
|
|
|
|
perDayForWeek [7]float64
|
|
|
|
timeLastWeekRotate time.Time
|
|
|
|
perWeekForMonth [4]float64
|
|
|
|
timeLastMonthRotate time.Time
|
|
|
|
perMonthForYear [12]float64
|
|
|
|
|
|
|
|
monitor func() float64
|
2018-06-21 18:29:02 +00:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2018-06-23 16:04:05 +00:00
|
|
|
Minutes() []float64
|
|
|
|
Hours() []float64
|
|
|
|
Days() []float64
|
|
|
|
Weeks() []float64
|
|
|
|
Months() []float64
|
2018-06-21 18:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMonitorHistory returns a new MonitorHistory with starttime of time.Now and Started running with supplied monitor
|
2018-06-26 18:24:16 +00:00
|
|
|
func NewMonitorHistory(monitor func() float64) MonitorHistory {
|
|
|
|
mh := &monitorHistory{starttime: time.Now(), monitor: monitor, breakChannel: make(chan bool)}
|
2018-06-21 18:29:02 +00:00
|
|
|
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
|
2018-06-23 16:04:05 +00:00
|
|
|
func (mh *monitorHistory) Minutes() []float64 {
|
2018-06-21 18:29:02 +00:00
|
|
|
return mh.returnCopy(mh.perMinutePerHour[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hours returns the last 24 hourly averages of monitor results
|
2018-06-23 16:04:05 +00:00
|
|
|
func (mh *monitorHistory) Hours() []float64 {
|
2018-06-21 18:29:02 +00:00
|
|
|
return mh.returnCopy(mh.perHourForDay[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Days returns the last 7 day averages of monitor results
|
2018-06-23 16:04:05 +00:00
|
|
|
func (mh *monitorHistory) Days() []float64 {
|
2018-06-21 18:29:02 +00:00
|
|
|
return mh.returnCopy(mh.perDayForWeek[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Weeks returns the last 4 weeks of averages of monitor results
|
2018-06-23 16:04:05 +00:00
|
|
|
func (mh *monitorHistory) Weeks() []float64 {
|
2018-06-21 18:29:02 +00:00
|
|
|
return mh.returnCopy(mh.perWeekForMonth[:])
|
|
|
|
}
|
|
|
|
|
|
|
|
// Months returns the last 12 months of averages of monitor results
|
2018-06-23 16:04:05 +00:00
|
|
|
func (mh *monitorHistory) Months() []float64 {
|
2018-06-21 18:29:02 +00:00
|
|
|
return mh.returnCopy(mh.perMonthForYear[:])
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:04:05 +00:00
|
|
|
func (mh *monitorHistory) returnCopy(slice []float64) []float64 {
|
|
|
|
retSlice := make([]float64, len(slice))
|
2018-06-21 18:29:02 +00:00
|
|
|
mh.lock.Lock()
|
|
|
|
for i, v := range slice {
|
|
|
|
retSlice[i] = v
|
|
|
|
}
|
|
|
|
mh.lock.Unlock()
|
|
|
|
return retSlice
|
|
|
|
}
|
|
|
|
|
2018-06-23 16:04:05 +00:00
|
|
|
func rotateAndAvg(array []float64, newVal float64) float64 {
|
|
|
|
total := float64(0.0)
|
2018-06-21 18:29:02 +00:00
|
|
|
for i := len(array) - 1; i > 0; i-- {
|
|
|
|
array[i] = array[i-1]
|
|
|
|
total += array[i]
|
|
|
|
}
|
|
|
|
array[0] = newVal
|
|
|
|
total += newVal
|
2018-06-23 16:04:05 +00:00
|
|
|
return total / float64(len(array))
|
|
|
|
}
|
|
|
|
func average(array []float64) float64 {
|
|
|
|
total := float64(0)
|
|
|
|
for _, x := range array {
|
|
|
|
total += x
|
|
|
|
}
|
|
|
|
return total / float64(len(array))
|
2018-06-21 18:29:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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())
|
2018-06-23 16:04:05 +00:00
|
|
|
|
|
|
|
if time.Now().Sub(mh.timeLastHourRotate) > time.Hour {
|
|
|
|
rotateAndAvg(mh.perHourForDay[:], minuteAvg)
|
|
|
|
mh.timeLastHourRotate = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Sub(mh.timeLastDayRotate) > time.Hour*24 {
|
|
|
|
rotateAndAvg(mh.perDayForWeek[:], average(mh.perHourForDay[:]))
|
|
|
|
mh.timeLastDayRotate = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Sub(mh.timeLastWeekRotate) > time.Hour*24*7 {
|
|
|
|
rotateAndAvg(mh.perWeekForMonth[:], average(mh.perDayForWeek[:]))
|
|
|
|
mh.timeLastWeekRotate = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
if time.Now().Sub(mh.timeLastMonthRotate) > time.Hour*24*7*4 {
|
|
|
|
rotateAndAvg(mh.perMonthForYear[:], average(mh.perWeekForMonth[:]))
|
|
|
|
mh.timeLastMonthRotate = time.Now()
|
|
|
|
}
|
2018-06-21 18:29:02 +00:00
|
|
|
|
|
|
|
mh.lock.Unlock()
|
|
|
|
|
|
|
|
// Repeat every minute
|
|
|
|
timeout = time.Duration(time.Minute)
|
|
|
|
|
|
|
|
case <-mh.breakChannel:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|