@@ -15,9 +15,6 @@ import (
15
15
const (
16
16
CPU Type = "cpu"
17
17
Heap Type = "heap"
18
-
19
- monitorInterval = 100 * time .Millisecond
20
- dataPointsToAvg = 50
21
18
)
22
19
23
20
type Type string
@@ -33,31 +30,36 @@ type Monitor struct {
33
30
scanners map [Type ]scanner
34
31
containerLimit map [Type ]float64
35
32
utilization map [Type ]float64
33
+ interval time.Duration
36
34
37
35
// Variables to calculate average CPU utilization
38
36
index int
39
- cpuRates [dataPointsToAvg ]float64
40
- cpuIntervals [dataPointsToAvg ]float64
37
+ cpuRates []float64
38
+ cpuIntervals []float64
41
39
totalCPU float64
42
40
totalInterval float64
43
41
lastCPU float64
44
42
lastUpdate time.Time
43
+ cpuDataPoints int
45
44
46
45
lock sync.RWMutex
47
46
}
48
47
49
- func NewMonitor (limits map [Type ]float64 , registerer prometheus.Registerer ) (* Monitor , error ) {
48
+ func NewMonitor (limits map [Type ]float64 , interval , cpuAverageInterval time. Duration , registerer prometheus.Registerer ) (* Monitor , error ) {
50
49
m := & Monitor {
51
50
containerLimit : limits ,
52
51
scanners : make (map [Type ]scanner ),
53
52
utilization : make (map [Type ]float64 ),
54
-
55
- cpuRates : [dataPointsToAvg ]float64 {},
56
- cpuIntervals : [dataPointsToAvg ]float64 {},
53
+ interval : interval ,
57
54
58
55
lock : sync.RWMutex {},
59
56
}
60
57
58
+ m .interval = interval
59
+ m .cpuDataPoints = int (cpuAverageInterval .Nanoseconds () / interval .Nanoseconds ())
60
+ m .cpuRates = make ([]float64 , m .cpuDataPoints )
61
+ m .cpuIntervals = make ([]float64 , m .cpuDataPoints )
62
+
61
63
m .Service = services .NewBasicService (nil , m .running , nil )
62
64
63
65
for resType , limit := range limits {
@@ -92,7 +94,7 @@ func NewMonitor(limits map[Type]float64, registerer prometheus.Registerer) (*Mon
92
94
}
93
95
94
96
func (m * Monitor ) running (ctx context.Context ) error {
95
- ticker := time .NewTicker (monitorInterval )
97
+ ticker := time .NewTicker (m . interval )
96
98
defer ticker .Stop ()
97
99
98
100
for {
@@ -141,7 +143,7 @@ func (m *Monitor) storeCPUUtilization(cpuTime float64) {
141
143
142
144
m .lastCPU = cpuTime
143
145
m .lastUpdate = now
144
- m .index = (m .index + 1 ) % dataPointsToAvg
146
+ m .index = (m .index + 1 ) % m . cpuDataPoints
145
147
146
148
if m .totalInterval > 0 && m .containerLimit [CPU ] > 0 {
147
149
m .utilization [CPU ] = m .totalCPU / m .totalInterval / m .containerLimit [CPU ]
0 commit comments