Skip to content

Commit 7e02d8e

Browse files
TylerHelmuthjmsnll
authored andcommitted
[receiver/kubeletstats] Add new CPU utilization metrics (open-telemetry#27276)
**Description:** Adds new CPU utilization metrics with respect to pod/container CPU limits and requests **Link to tracking Issue:** <Issue number if applicable> Closes open-telemetry#24905 **Testing:** <Describe what testing was performed and which tests were added.> Added new unit tests and tested locally
1 parent 78c0fec commit 7e02d8e

15 files changed

+476
-35
lines changed

.chloggen/kubeletstats-percentage-metrics.yaml renamed to .chloggen/kubeletstats-cpu-utilization.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ change_type: enhancement
77
component: kubeletstatsreceiver
88

99
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10-
note: Add new metrics for representing pod and memory consumption of pods and containers as a percentage of the defined resource limits.
10+
note: Adds new `k8s.pod.cpu_limit_utilization`, `k8s.pod.cpu_request_utilization`, `k8s.container.cpu_limit_utilization`, and `k8s.container.cpu_request_utilization` metrics that represent the ratio of cpu used vs set limits and requests.
1111

1212
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13-
issues: [25835]
13+
issues: [27276]
1414

1515
# (Optional) One or more lines of additional information to render under the primary note.
1616
# These lines will be padded with 2 spaces and then inserted directly into the document.
1717
# Use pipe (|) for multiline entries.
18-
subtext: These metrics represent how much of your resource limits a container or pod is consuming.
18+
subtext:
1919

2020
# If your change doesn't affect end users or the exported elements of any package,
2121
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

.chloggen/kubeletstats-memory-utilization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ change_type: enhancement
77
component: kubeletstatsreceiver
88

99
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10-
note: Adds new `k8s.pod.memory.utilization` and `container.memory.utilization` metrics that represent the ratio of memory used vs limits set.
10+
note: Adds new `k8s.pod.memory_limit_utilization`, `k8s.pod.memory_request_utilization`, `k8s.container.memory_limit_utilization`, and `k8s.container.memory_request_utilization` metrics that represent the ratio of memory used vs set limits and requests.
1111

1212
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
1313
issues: [25894]

receiver/kubeletstatsreceiver/documentation.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,22 @@ The time since the container started
394394
| ---- | ----------- | ---------- | ----------------------- | --------- |
395395
| s | Sum | Int | Cumulative | true |
396396
397+
### k8s.container.cpu_limit_utilization
398+
399+
Container cpu utilization as a ratio of the container's limits
400+
401+
| Unit | Metric Type | Value Type |
402+
| ---- | ----------- | ---------- |
403+
| 1 | Gauge | Double |
404+
405+
### k8s.container.cpu_request_utilization
406+
407+
Container cpu utilization as a ratio of the container's requests
408+
409+
| Unit | Metric Type | Value Type |
410+
| ---- | ----------- | ---------- |
411+
| 1 | Gauge | Double |
412+
397413
### k8s.container.memory_limit_utilization
398414
399415
Container memory utilization as a ratio of the container's limits
@@ -418,6 +434,22 @@ The time since the node started
418434
| ---- | ----------- | ---------- | ----------------------- | --------- |
419435
| s | Sum | Int | Cumulative | true |
420436
437+
### k8s.pod.cpu_limit_utilization
438+
439+
Pod cpu utilization as a ratio of the pod's total container limits. If any container is missing a limit the metric is not emitted.
440+
441+
| Unit | Metric Type | Value Type |
442+
| ---- | ----------- | ---------- |
443+
| 1 | Gauge | Double |
444+
445+
### k8s.pod.cpu_request_utilization
446+
447+
Pod cpu utilization as a ratio of the pod's total container requests. If any container is missing a request the metric is not emitted.
448+
449+
| Unit | Metric Type | Value Type |
450+
| ---- | ----------- | ---------- |
451+
| 1 | Gauge | Double |
452+
421453
### k8s.pod.memory_limit_utilization
422454
423455
Pod memory utilization as a ratio of the pod's total container limits. If any container is missing a limit the metric is not emitted.

receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (a *metricDataAccumulator) nodeStats(s stats.NodeStats) {
5656

5757
currentTime := pcommon.NewTimestampFromTime(a.time)
5858
addUptimeMetric(a.mbs.NodeMetricsBuilder, metadata.NodeUptimeMetrics.Uptime, s.StartTime, currentTime)
59-
addCPUMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeCPUMetrics, s.CPU, currentTime)
59+
addCPUMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeCPUMetrics, s.CPU, currentTime, resources{})
6060
addMemoryMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeMemoryMetrics, s.Memory, currentTime, resources{})
6161
addFilesystemMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeFilesystemMetrics, s.Fs, currentTime)
6262
addNetworkMetrics(a.mbs.NodeMetricsBuilder, metadata.NodeNetworkMetrics, s.Network, currentTime)
@@ -76,7 +76,7 @@ func (a *metricDataAccumulator) podStats(s stats.PodStats) {
7676

7777
currentTime := pcommon.NewTimestampFromTime(a.time)
7878
addUptimeMetric(a.mbs.PodMetricsBuilder, metadata.PodUptimeMetrics.Uptime, s.StartTime, currentTime)
79-
addCPUMetrics(a.mbs.PodMetricsBuilder, metadata.PodCPUMetrics, s.CPU, currentTime)
79+
addCPUMetrics(a.mbs.PodMetricsBuilder, metadata.PodCPUMetrics, s.CPU, currentTime, a.metadata.podResources[s.PodRef.UID])
8080
addMemoryMetrics(a.mbs.PodMetricsBuilder, metadata.PodMemoryMetrics, s.Memory, currentTime, a.metadata.podResources[s.PodRef.UID])
8181
addFilesystemMetrics(a.mbs.PodMetricsBuilder, metadata.PodFilesystemMetrics, s.EphemeralStorage, currentTime)
8282
addNetworkMetrics(a.mbs.PodMetricsBuilder, metadata.PodNetworkMetrics, s.Network, currentTime)
@@ -108,9 +108,10 @@ func (a *metricDataAccumulator) containerStats(sPod stats.PodStats, s stats.Cont
108108
}
109109

110110
currentTime := pcommon.NewTimestampFromTime(a.time)
111+
resourceKey := sPod.PodRef.UID + s.Name
111112
addUptimeMetric(a.mbs.ContainerMetricsBuilder, metadata.ContainerUptimeMetrics.Uptime, s.StartTime, currentTime)
112-
addCPUMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerCPUMetrics, s.CPU, currentTime)
113-
addMemoryMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerMemoryMetrics, s.Memory, currentTime, a.metadata.containerResources[sPod.PodRef.UID+s.Name])
113+
addCPUMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerCPUMetrics, s.CPU, currentTime, a.metadata.containerResources[resourceKey])
114+
addMemoryMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerMemoryMetrics, s.Memory, currentTime, a.metadata.containerResources[resourceKey])
114115
addFilesystemMetrics(a.mbs.ContainerMetricsBuilder, metadata.ContainerFilesystemMetrics, s.Rootfs, currentTime)
115116

116117
a.m = append(a.m, a.mbs.ContainerMetricsBuilder.Emit(

receiver/kubeletstatsreceiver/internal/kubelet/cpu.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,27 @@ import (
1010
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/kubeletstatsreceiver/internal/metadata"
1111
)
1212

13-
func addCPUMetrics(mb *metadata.MetricsBuilder, cpuMetrics metadata.CPUMetrics, s *stats.CPUStats, currentTime pcommon.Timestamp) {
13+
func addCPUMetrics(mb *metadata.MetricsBuilder, cpuMetrics metadata.CPUMetrics, s *stats.CPUStats, currentTime pcommon.Timestamp, r resources) {
1414
if s == nil {
1515
return
1616
}
17-
addCPUUsageMetric(mb, cpuMetrics.Utilization, s, currentTime)
17+
addCPUUsageMetric(mb, cpuMetrics, s, currentTime, r)
1818
addCPUTimeMetric(mb, cpuMetrics.Time, s, currentTime)
1919
}
2020

21-
func addCPUUsageMetric(mb *metadata.MetricsBuilder, recordDataPoint metadata.RecordDoubleDataPointFunc, s *stats.CPUStats, currentTime pcommon.Timestamp) {
21+
func addCPUUsageMetric(mb *metadata.MetricsBuilder, cpuMetrics metadata.CPUMetrics, s *stats.CPUStats, currentTime pcommon.Timestamp, r resources) {
2222
if s.UsageNanoCores == nil {
2323
return
2424
}
2525
value := float64(*s.UsageNanoCores) / 1_000_000_000
26-
recordDataPoint(mb, currentTime, value)
26+
cpuMetrics.Utilization(mb, currentTime, value)
27+
28+
if r.cpuLimit > 0 {
29+
cpuMetrics.LimitUtilization(mb, currentTime, value/r.cpuLimit)
30+
}
31+
if r.cpuRequest > 0 {
32+
cpuMetrics.RequestUtilization(mb, currentTime, value/r.cpuRequest)
33+
}
2734
}
2835

2936
func addCPUTimeMetric(mb *metadata.MetricsBuilder, recordDataPoint metadata.RecordDoubleDataPointFunc, s *stats.CPUStats, currentTime pcommon.Timestamp) {

receiver/kubeletstatsreceiver/internal/metadata/generated_config.go

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/kubeletstatsreceiver/internal/metadata/generated_config_test.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)