Skip to content

Commit 203b1dd

Browse files
committed
fix cpu resource metric type by changing to counter
1 parent 93e62e6 commit 203b1dd

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

pkg/kubelet/metrics/collectors/resource_metrics.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626
)
2727

2828
var (
29-
nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds",
29+
nodeCPUUsageDesc = metrics.NewDesc("node_cpu_usage_seconds_total",
3030
"Cumulative cpu time consumed by the node in core-seconds",
3131
nil,
3232
nil,
@@ -40,7 +40,7 @@ var (
4040
metrics.ALPHA,
4141
"")
4242

43-
containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds",
43+
containerCPUUsageDesc = metrics.NewDesc("container_cpu_usage_seconds_total",
4444
"Cumulative cpu time consumed by the container in core-seconds",
4545
[]string{"container", "pod", "namespace"},
4646
nil,
@@ -120,7 +120,7 @@ func (rc *resourceMetricsCollector) collectNodeCPUMetrics(ch chan<- metrics.Metr
120120
}
121121

122122
ch <- metrics.NewLazyMetricWithTimestamp(s.CPU.Time.Time,
123-
metrics.NewLazyConstMetric(nodeCPUUsageDesc, metrics.GaugeValue, float64(*s.CPU.UsageCoreNanoSeconds)/float64(time.Second)))
123+
metrics.NewLazyConstMetric(nodeCPUUsageDesc, metrics.CounterValue, float64(*s.CPU.UsageCoreNanoSeconds)/float64(time.Second)))
124124
}
125125

126126
func (rc *resourceMetricsCollector) collectNodeMemoryMetrics(ch chan<- metrics.Metric, s summary.NodeStats) {
@@ -138,7 +138,7 @@ func (rc *resourceMetricsCollector) collectContainerCPUMetrics(ch chan<- metrics
138138
}
139139

140140
ch <- metrics.NewLazyMetricWithTimestamp(s.CPU.Time.Time,
141-
metrics.NewLazyConstMetric(containerCPUUsageDesc, metrics.GaugeValue,
141+
metrics.NewLazyConstMetric(containerCPUUsageDesc, metrics.CounterValue,
142142
float64(*s.CPU.UsageCoreNanoSeconds)/float64(time.Second), s.Name, pod.PodRef.Name, pod.PodRef.Namespace))
143143
}
144144

pkg/kubelet/metrics/collectors/resource_metrics_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func TestCollectResourceMetrics(t *testing.T) {
4747
testTime := metav1.NewTime(time.Unix(2, 0)) // a static timestamp: 2000
4848
interestedMetrics := []string{
4949
"scrape_error",
50-
"node_cpu_usage_seconds",
50+
"node_cpu_usage_seconds_total",
5151
"node_memory_working_set_bytes",
52-
"container_cpu_usage_seconds",
52+
"container_cpu_usage_seconds_total",
5353
"container_memory_working_set_bytes",
5454
}
5555

@@ -85,9 +85,9 @@ func TestCollectResourceMetrics(t *testing.T) {
8585
},
8686
summaryErr: nil,
8787
expectedMetrics: `
88-
# HELP node_cpu_usage_seconds [ALPHA] Cumulative cpu time consumed by the node in core-seconds
89-
# TYPE node_cpu_usage_seconds gauge
90-
node_cpu_usage_seconds 10 2000
88+
# HELP node_cpu_usage_seconds_total [ALPHA] Cumulative cpu time consumed by the node in core-seconds
89+
# TYPE node_cpu_usage_seconds_total counter
90+
node_cpu_usage_seconds_total 10 2000
9191
# HELP node_memory_working_set_bytes [ALPHA] Current working set of the node in bytes
9292
# TYPE node_memory_working_set_bytes gauge
9393
node_memory_working_set_bytes 1000 2000
@@ -156,11 +156,11 @@ func TestCollectResourceMetrics(t *testing.T) {
156156
# HELP scrape_error [ALPHA] 1 if there was an error while getting container metrics, 0 otherwise
157157
# TYPE scrape_error gauge
158158
scrape_error 0
159-
# HELP container_cpu_usage_seconds [ALPHA] Cumulative cpu time consumed by the container in core-seconds
160-
# TYPE container_cpu_usage_seconds gauge
161-
container_cpu_usage_seconds{container="container_a",namespace="namespace_a",pod="pod_a"} 10 2000
162-
container_cpu_usage_seconds{container="container_a",namespace="namespace_b",pod="pod_b"} 10 2000
163-
container_cpu_usage_seconds{container="container_b",namespace="namespace_a",pod="pod_a"} 10 2000
159+
# HELP container_cpu_usage_seconds_total [ALPHA] Cumulative cpu time consumed by the container in core-seconds
160+
# TYPE container_cpu_usage_seconds_total counter
161+
container_cpu_usage_seconds_total{container="container_a",namespace="namespace_a",pod="pod_a"} 10 2000
162+
container_cpu_usage_seconds_total{container="container_a",namespace="namespace_b",pod="pod_b"} 10 2000
163+
container_cpu_usage_seconds_total{container="container_b",namespace="namespace_a",pod="pod_a"} 10 2000
164164
# HELP container_memory_working_set_bytes [ALPHA] Current working set of the container in bytes
165165
# TYPE container_memory_working_set_bytes gauge
166166
container_memory_working_set_bytes{container="container_a",namespace="namespace_a",pod="pod_a"} 1000 2000

0 commit comments

Comments
 (0)