@@ -24,7 +24,7 @@ func (e *ExhaustedError) Error() string {
24
24
return "resource exhausted"
25
25
}
26
26
27
- const heapMetricName = "/memory/classes/heap /objects:bytes"
27
+ const heapMetricName = "/memory/classes/Heap /objects:bytes"
28
28
const monitorInterval = time .Second
29
29
const dataPointsToAvg = 30
30
30
@@ -38,8 +38,8 @@ type Scanner struct {
38
38
}
39
39
40
40
type Stats struct {
41
- cpu float64
42
- heap uint64
41
+ CPU float64
42
+ Heap uint64
43
43
}
44
44
45
45
func NewScanner () (* Scanner , error ) {
@@ -73,17 +73,11 @@ func (s *Scanner) Scan() (Stats, error) {
73
73
metrics .Read (s .metricSamples )
74
74
75
75
return Stats {
76
- cpu : stat .CPUTime (),
77
- heap : s .metricSamples [0 ].Value .Uint64 (),
76
+ CPU : stat .CPUTime (),
77
+ Heap : s .metricSamples [0 ].Value .Uint64 (),
78
78
}, nil
79
79
}
80
80
81
- type IMonitor interface {
82
- GetCPUUtilization () float64
83
- GetHeapUtilization () float64
84
- CheckResourceUtilization () (string , float64 , float64 , error )
85
- }
86
-
87
81
type Monitor struct {
88
82
services.Service
89
83
@@ -121,19 +115,19 @@ func NewMonitor(thresholds configs.Resources, limits configs.Resources, scanner
121
115
122
116
promauto .With (registerer ).NewGaugeFunc (prometheus.GaugeOpts {
123
117
Name : "cortex_resource_utilization" ,
124
- ConstLabels : map [string ]string {"resource" : "cpu " },
118
+ ConstLabels : map [string ]string {"resource" : "CPU " },
125
119
}, m .GetCPUUtilization )
126
120
promauto .With (registerer ).NewGaugeFunc (prometheus.GaugeOpts {
127
121
Name : "cortex_resource_utilization" ,
128
- ConstLabels : map [string ]string {"resource" : "heap " },
122
+ ConstLabels : map [string ]string {"resource" : "Heap " },
129
123
}, m .GetHeapUtilization )
130
124
promauto .With (registerer ).NewGauge (prometheus.GaugeOpts {
131
125
Name : "cortex_resource_threshold" ,
132
- ConstLabels : map [string ]string {"resource" : "cpu " },
126
+ ConstLabels : map [string ]string {"resource" : "CPU " },
133
127
}).Set (thresholds .CPU )
134
128
promauto .With (registerer ).NewGauge (prometheus.GaugeOpts {
135
129
Name : "cortex_resource_threshold" ,
136
- ConstLabels : map [string ]string {"resource" : "heap " },
130
+ ConstLabels : map [string ]string {"resource" : "Heap " },
137
131
}).Set (thresholds .Heap )
138
132
139
133
return m , nil
@@ -167,21 +161,21 @@ func (m *Monitor) storeCPUUtilization(stats Stats) {
167
161
now := time .Now ()
168
162
169
163
if m .lastUpdate .IsZero () {
170
- m .lastCPU = stats .cpu
164
+ m .lastCPU = stats .CPU
171
165
m .lastUpdate = now
172
166
return
173
167
}
174
168
175
169
m .totalCPU -= m .cpuRates [m .index ]
176
170
m .totalInterval -= m .cpuIntervals [m .index ]
177
171
178
- m .cpuRates [m .index ] = stats .cpu - m .lastCPU
172
+ m .cpuRates [m .index ] = stats .CPU - m .lastCPU
179
173
m .cpuIntervals [m .index ] = now .Sub (m .lastUpdate ).Seconds ()
180
174
181
175
m .totalCPU += m .cpuRates [m .index ]
182
176
m .totalInterval += m .cpuIntervals [m .index ]
183
177
184
- m .lastCPU = stats .cpu
178
+ m .lastCPU = stats .CPU
185
179
m .lastUpdate = now
186
180
m .index = (m .index + 1 ) % dataPointsToAvg
187
181
@@ -195,7 +189,7 @@ func (m *Monitor) storeHeapUtilization(stats Stats) {
195
189
defer m .lock .Unlock ()
196
190
197
191
if m .containerLimit .Heap > 0 {
198
- m .utilization .Heap = float64 (stats .heap ) / m .containerLimit .Heap
192
+ m .utilization .Heap = float64 (stats .Heap ) / m .containerLimit .Heap
199
193
}
200
194
}
201
195
@@ -219,12 +213,12 @@ func (m *Monitor) CheckResourceUtilization() (string, float64, float64, error) {
219
213
220
214
if m .thresholds .CPU > 0 && cpu > m .thresholds .CPU {
221
215
err := ExhaustedError {}
222
- return "cpu " , m .thresholds .CPU , cpu , httpgrpc .Errorf (http .StatusTooManyRequests , "%s" , err .Error ())
216
+ return "CPU " , m .thresholds .CPU , cpu , httpgrpc .Errorf (http .StatusTooManyRequests , "%s" , err .Error ())
223
217
}
224
218
225
219
if m .thresholds .Heap > 0 && heap > m .thresholds .Heap {
226
220
err := ExhaustedError {}
227
- return "heap " , m .thresholds .Heap , heap , httpgrpc .Errorf (http .StatusTooManyRequests , "%s" , err .Error ())
221
+ return "Heap " , m .thresholds .Heap , heap , httpgrpc .Errorf (http .StatusTooManyRequests , "%s" , err .Error ())
228
222
}
229
223
230
224
return "" , 0 , 0 , nil
0 commit comments