Skip to content

Commit bddb7fb

Browse files
committed
Apply configs to NewMonitor
1 parent 434eefe commit bddb7fb

File tree

6 files changed

+21
-18
lines changed

6 files changed

+21
-18
lines changed

integration/resource_based_limiter_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Test_ResourceBasedLimiter_shouldStartWithoutError(t *testing.T) {
1919
defer s.Close()
2020

2121
flags := mergeFlags(BlocksStorageFlags(), map[string]string{
22-
"-monitored.resources": "cpu,heap",
22+
"-resource-monitor.resources": "cpu,heap",
2323
})
2424

2525
// Start dependencies.

pkg/configs/resource_monitor.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func (cfg *ResourceMonitor) Validate() error {
4040
return fmt.Errorf("resource monitor interval must be greater than zero")
4141
}
4242

43-
if cfg.CPURateInterval <= 0 {
44-
return fmt.Errorf("resource monitor cpu rate interval must be greater than zero")
43+
if cfg.CPURateInterval < cfg.Interval {
44+
return fmt.Errorf("resource monitor cpu rate interval cannot be smaller than resource monitor interval")
4545
}
4646

4747
return nil

pkg/cortex/cortex_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,11 @@ func TestConfigValidation(t *testing.T) {
195195
configuration := newDefaultConfig()
196196
configuration.ResourceMonitor = configs.ResourceMonitor{
197197
Interval: time.Second,
198-
CPURateInterval: -1,
198+
CPURateInterval: time.Millisecond,
199199
}
200200
return configuration
201201
},
202-
expectedError: fmt.Errorf("resource monitor cpu rate interval must be greater than zero"),
202+
expectedError: fmt.Errorf("resource monitor cpu rate interval cannot be smaller than resource monitor interval"),
203203
},
204204
{
205205
name: "should not fail validation for valid resources to monitor",

pkg/cortex/modules.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ func (t *Cortex) initResourceMonitor() (services.Service, error) {
805805
}
806806

807807
var err error
808-
t.ResourceMonitor, err = resource.NewMonitor(containerLimits, prometheus.DefaultRegisterer)
808+
t.ResourceMonitor, err = resource.NewMonitor(containerLimits, t.Cfg.ResourceMonitor.Interval, t.Cfg.ResourceMonitor.CPURateInterval, prometheus.DefaultRegisterer)
809809
return t.ResourceMonitor, err
810810
}
811811

pkg/util/resource/monitor.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import (
1515
const (
1616
CPU Type = "cpu"
1717
Heap Type = "heap"
18-
19-
monitorInterval = 100 * time.Millisecond
20-
dataPointsToAvg = 50
2118
)
2219

2320
type Type string
@@ -33,31 +30,36 @@ type Monitor struct {
3330
scanners map[Type]scanner
3431
containerLimit map[Type]float64
3532
utilization map[Type]float64
33+
interval time.Duration
3634

3735
// Variables to calculate average CPU utilization
3836
index int
39-
cpuRates [dataPointsToAvg]float64
40-
cpuIntervals [dataPointsToAvg]float64
37+
cpuRates []float64
38+
cpuIntervals []float64
4139
totalCPU float64
4240
totalInterval float64
4341
lastCPU float64
4442
lastUpdate time.Time
43+
cpuDataPoints int
4544

4645
lock sync.RWMutex
4746
}
4847

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) {
5049
m := &Monitor{
5150
containerLimit: limits,
5251
scanners: make(map[Type]scanner),
5352
utilization: make(map[Type]float64),
54-
55-
cpuRates: [dataPointsToAvg]float64{},
56-
cpuIntervals: [dataPointsToAvg]float64{},
53+
interval: interval,
5754

5855
lock: sync.RWMutex{},
5956
}
6057

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+
6163
m.Service = services.NewBasicService(nil, m.running, nil)
6264

6365
for resType, limit := range limits {
@@ -92,7 +94,7 @@ func NewMonitor(limits map[Type]float64, registerer prometheus.Registerer) (*Mon
9294
}
9395

9496
func (m *Monitor) running(ctx context.Context) error {
95-
ticker := time.NewTicker(monitorInterval)
97+
ticker := time.NewTicker(m.interval)
9698
defer ticker.Stop()
9799

98100
for {
@@ -141,7 +143,7 @@ func (m *Monitor) storeCPUUtilization(cpuTime float64) {
141143

142144
m.lastCPU = cpuTime
143145
m.lastUpdate = now
144-
m.index = (m.index + 1) % dataPointsToAvg
146+
m.index = (m.index + 1) % m.cpuDataPoints
145147

146148
if m.totalInterval > 0 && m.containerLimit[CPU] > 0 {
147149
m.utilization[CPU] = m.totalCPU / m.totalInterval / m.containerLimit[CPU]

pkg/util/resource/monitor_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ package resource
22

33
import (
44
"testing"
5+
"time"
56

67
"github.com/prometheus/client_golang/prometheus"
78
"github.com/stretchr/testify/require"
89
)
910

1011
func Test_Monitor(t *testing.T) {
11-
m, err := NewMonitor(map[Type]float64{}, prometheus.DefaultRegisterer)
12+
m, err := NewMonitor(map[Type]float64{}, time.Second, time.Minute, prometheus.DefaultRegisterer)
1213

1314
m.scanners[CPU] = &noopScanner{}
1415
m.containerLimit[CPU] = 1

0 commit comments

Comments
 (0)