Skip to content

Commit 846c656

Browse files
committed
fix the cpu usage and memory promql query
1 parent 31ef455 commit 846c656

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

vertical-pod-autoscaler/pkg/recommender/input/history/history_provider.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package history
1919
import (
2020
"context"
2121
"fmt"
22+
"k8s.io/klog"
2223
"sort"
2324
"strings"
2425
"time"
@@ -260,12 +261,16 @@ func (p *prometheusHistoryProvider) GetClusterHistory() (map[model.PodID]*PodHis
260261
p.config.CadvisorMetricsJobName, p.config.CtrPodNameLabel,
261262
p.config.CtrNameLabel, p.config.CtrNameLabel)
262263

263-
// This query uses Prometheus Subquery notation, to gives us a result of a five minute cpu rate by default evaluated every 1minute for last config.HistoryLength days/hours/minutes. In order to change the evaluation step, you need change Prometheus global.evaluation_interval configuration parameter.
264-
err := p.readResourceHistory(res, fmt.Sprintf("rate(container_cpu_usage_seconds_total{%s}[5m])[%s:]", podSelector, p.config.HistoryLength), model.ResourceCPU)
264+
historicalCpuQuery := fmt.Sprintf("rate(container_cpu_usage_seconds_total{%s}[%s])", podSelector, p.config.HistoryResolution)
265+
klog.V(4).Infof("Historical CPU usage query used: %s", historicalCpuQuery)
266+
err := p.readResourceHistory(res, historicalCpuQuery, model.ResourceCPU)
265267
if err != nil {
266268
return nil, fmt.Errorf("cannot get usage history: %v", err)
267269
}
268-
err = p.readResourceHistory(res, fmt.Sprintf("container_memory_working_set_bytes{%s}[%s]", podSelector, p.config.HistoryLength), model.ResourceMemory)
270+
271+
historicalMemoryQuery := fmt.Sprintf("container_memory_working_set_bytes{%s}", podSelector)
272+
klog.V(4).Infof("Historical memory usage query used: %s", historicalMemoryQuery)
273+
err = p.readResourceHistory(res, historicalMemoryQuery, model.ResourceMemory)
269274
if err != nil {
270275
return nil, fmt.Errorf("cannot get usage history: %v", err)
271276
}
@@ -274,6 +279,6 @@ func (p *prometheusHistoryProvider) GetClusterHistory() (map[model.PodID]*PodHis
274279
sort.Slice(samples, func(i, j int) bool { return samples[i].MeasureStart.Before(samples[j].MeasureStart) })
275280
}
276281
}
277-
p.readLastLabels(res, fmt.Sprintf("%s[%s]", p.config.PodLabelsMetricName, p.config.HistoryLength))
282+
p.readLastLabels(res, p.config.PodLabelsMetricName)
278283
return res, nil
279284
}

vertical-pod-autoscaler/pkg/recommender/input/history/history_provider_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ import (
3131
)
3232

3333
const (
34-
cpuQuery = "rate(container_cpu_usage_seconds_total{job=\"kubernetes-cadvisor\", pod_name=~\".+\", name!=\"POD\", name!=\"\"}[5m])[8d:]"
35-
memoryQuery = "container_memory_working_set_bytes{job=\"kubernetes-cadvisor\", pod_name=~\".+\", name!=\"POD\", name!=\"\"}[8d]"
36-
labelsQuery = "up{job=\"kubernetes-pods\"}[8d]"
34+
cpuQuery = "rate(container_cpu_usage_seconds_total{job=\"kubernetes-cadvisor\", pod_name=~\".+\", name!=\"POD\", name!=\"\"}[30s])"
35+
memoryQuery = "container_memory_working_set_bytes{job=\"kubernetes-cadvisor\", pod_name=~\".+\", name!=\"POD\", name!=\"\"}"
36+
labelsQuery = "up{job=\"kubernetes-pods\"}"
3737
)
3838

3939
func getDefaultPrometheusHistoryProviderConfigForTest() PrometheusHistoryProviderConfig {

0 commit comments

Comments
 (0)