Skip to content

Commit 8522b4e

Browse files
ChrsMarktiffany76TylerHelmuth
authored
[receiver/kubeletstats] Add k8s.pod.cpu.node.utilization metric (#33390)
**Description:** <Describe what has changed.> <!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> This PR adds the `k8s.pod.cpu.node.utilization` metric. Follow up from #32295 (comment) (cc @TylerHelmuth) . **Link to tracking Issue:** <Issue number if applicable> Related to #27885. **Testing:** <Describe what testing was performed and which tests were added.> Adjusted the respective unit test to cover this metric as well. **Documentation:** <Describe the documentation added.> Added Tested with a single container Pod: ![podCpu](https://github.com/open-telemetry/opentelemetry-collector-contrib/assets/11754898/9a0069c2-7077-4944-93b6-2dde00979bf3) --------- Signed-off-by: ChrsMark <[email protected]> Co-authored-by: Tiffany Hrabusa <[email protected]> Co-authored-by: Tyler Helmuth <[email protected]>
1 parent e89af1a commit 8522b4e

17 files changed

+390
-6
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: kubeletstats
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Add k8s.pod.cpu.node.utilization metric"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [33390]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/kubeletstatsreceiver/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,10 @@ receivers:
218218
- pod
219219
```
220220

221-
### Collect k8s.container.cpu.node.utilization as ratio of total node's capacity
221+
### Collect k8s.container.cpu.node.utilization, `k8s.pod.cpu.node.utilization` as ratio of total node's capacity
222222

223-
In order to calculate the `k8s.container.cpu.node.utilization` metric, the information of the node's capacity
224-
must be retrieved from the k8s API. In this, the `k8s_api_config` needs to be set.
223+
In order to calculate the `k8s.container.cpu.node.utilization` or `k8s.pod.cpu.node.utilization` metrics, the
224+
information of the node's capacity must be retrieved from the k8s API. In this, the `k8s_api_config` needs to be set.
225225
In addition, the node name must be identified properly. The `K8S_NODE_NAME` env var can be set using the
226226
downward API inside the collector pod spec as follows:
227227

@@ -246,6 +246,8 @@ receivers:
246246
metrics:
247247
k8s.container.cpu.node.utilization:
248248
enabled: true
249+
k8s.pod.cpu.node.utilization:
250+
enabled: true
249251
```
250252

251253
### Optional parameters

receiver/kubeletstatsreceiver/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ func (cfg *Config) Unmarshal(componentParser *confmap.Conf) error {
122122
func (cfg *Config) Validate() error {
123123
if cfg.Metrics.K8sContainerCPUNodeUtilization.Enabled && cfg.NodeName == "" {
124124
return errors.New("for k8s.container.cpu.node.utilization node setting is required. Check the readme on how to set the required setting")
125+
} else if cfg.Metrics.K8sPodCPUNodeUtilization.Enabled && cfg.NodeName == "" {
126+
return errors.New("for k8s.pod.cpu.node.utilization node setting is required. Check the readme on how to set the required setting")
125127
}
126128
return nil
127129
}

receiver/kubeletstatsreceiver/config_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,34 @@ func TestLoadConfig(t *testing.T) {
201201
},
202202
expectedValidationErr: "for k8s.container.cpu.node.utilization node setting is required. Check the readme on how to set the required setting",
203203
},
204+
{
205+
id: component.NewIDWithName(metadata.Type, "pod_cpu_node_utilization"),
206+
expected: &Config{
207+
ControllerConfig: scraperhelper.ControllerConfig{
208+
CollectionInterval: duration,
209+
InitialDelay: time.Second,
210+
},
211+
ClientConfig: kube.ClientConfig{
212+
APIConfig: k8sconfig.APIConfig{
213+
AuthType: "tls",
214+
},
215+
},
216+
MetricGroupsToCollect: []kubelet.MetricGroup{
217+
kubelet.ContainerMetricGroup,
218+
kubelet.PodMetricGroup,
219+
kubelet.NodeMetricGroup,
220+
},
221+
MetricsBuilderConfig: metadata.MetricsBuilderConfig{
222+
Metrics: metadata.MetricsConfig{
223+
K8sPodCPUNodeUtilization: metadata.MetricConfig{
224+
Enabled: true,
225+
},
226+
},
227+
ResourceAttributes: metadata.DefaultResourceAttributesConfig(),
228+
},
229+
},
230+
expectedValidationErr: "for k8s.pod.cpu.node.utilization node setting is required. Check the readme on how to set the required setting",
231+
},
204232
}
205233

206234
for _, tt := range tests {

receiver/kubeletstatsreceiver/documentation.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,14 @@ The time since the node started
458458
| ---- | ----------- | ---------- | ----------------------- | --------- |
459459
| s | Sum | Int | Cumulative | true |
460460
461+
### k8s.pod.cpu.node.utilization
462+
463+
Pod cpu utilization as a ratio of the node's capacity
464+
465+
| Unit | Metric Type | Value Type |
466+
| ---- | ----------- | ---------- |
467+
| 1 | Gauge | Double |
468+
461469
### k8s.pod.cpu.usage
462470
463471
Total CPU usage (sum of all cores per second) averaged over the sample window

receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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, a.metadata.podResources[s.PodRef.UID], 0)
79+
addCPUMetrics(a.mbs.PodMetricsBuilder, metadata.PodCPUMetrics, s.CPU, currentTime, a.metadata.podResources[s.PodRef.UID], a.metadata.cpuNodeLimit)
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)

receiver/kubeletstatsreceiver/internal/metadata/generated_config.go

Lines changed: 4 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: 2 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_metrics.go

Lines changed: 57 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_metrics_test.go

Lines changed: 15 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/metrics.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ var PodCPUMetrics = CPUMetrics{
3737
Time: (*MetricsBuilder).RecordK8sPodCPUTimeDataPoint,
3838
Usage: (*MetricsBuilder).RecordK8sPodCPUUsageDataPoint,
3939
Utilization: (*MetricsBuilder).RecordK8sPodCPUUtilizationDataPoint,
40+
NodeUtilization: (*MetricsBuilder).RecordK8sPodCPUNodeUtilizationDataPoint,
4041
LimitUtilization: (*MetricsBuilder).RecordK8sPodCPULimitUtilizationDataPoint,
4142
RequestUtilization: (*MetricsBuilder).RecordK8sPodCPURequestUtilizationDataPoint,
4243
}

receiver/kubeletstatsreceiver/internal/metadata/testdata/config.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ all_set:
6767
enabled: true
6868
k8s.node.uptime:
6969
enabled: true
70+
k8s.pod.cpu.node.utilization:
71+
enabled: true
7072
k8s.pod.cpu.time:
7173
enabled: true
7274
k8s.pod.cpu.usage:
@@ -214,6 +216,8 @@ none_set:
214216
enabled: false
215217
k8s.node.uptime:
216218
enabled: false
219+
k8s.pod.cpu.node.utilization:
220+
enabled: false
217221
k8s.pod.cpu.time:
218222
enabled: false
219223
k8s.pod.cpu.usage:

receiver/kubeletstatsreceiver/metadata.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,13 @@ metrics:
236236
gauge:
237237
value_type: int
238238
attributes: []
239+
k8s.pod.cpu.node.utilization:
240+
enabled: false
241+
description: "Pod cpu utilization as a ratio of the node's capacity"
242+
unit: 1
243+
gauge:
244+
value_type: double
245+
attributes: [ ]
239246
k8s.pod.cpu_limit_utilization:
240247
enabled: false
241248
description: "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."

receiver/kubeletstatsreceiver/scraper.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ func newKubletScraper(
8383
nodeLimits: &kubelet.NodeLimits{},
8484
}
8585

86-
if metricsConfig.Metrics.K8sContainerCPUNodeUtilization.Enabled {
86+
if metricsConfig.Metrics.K8sContainerCPUNodeUtilization.Enabled ||
87+
metricsConfig.Metrics.K8sPodCPUNodeUtilization.Enabled {
8788
ks.nodeInformer = k8sconfig.NewNodeSharedInformer(rOptions.k8sAPIClient, nodeName, 5*time.Minute)
8889
}
8990

receiver/kubeletstatsreceiver/scraper_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ func TestScraperWithNodeUtilization(t *testing.T) {
9999
options := &scraperOptions{
100100
metricGroupsToCollect: map[kubelet.MetricGroup]bool{
101101
kubelet.ContainerMetricGroup: true,
102+
kubelet.PodMetricGroup: true,
102103
},
103104
k8sAPIClient: client,
104105
}
@@ -111,6 +112,9 @@ func TestScraperWithNodeUtilization(t *testing.T) {
111112
K8sContainerCPUNodeUtilization: metadata.MetricConfig{
112113
Enabled: true,
113114
},
115+
K8sPodCPUNodeUtilization: metadata.MetricConfig{
116+
Enabled: true,
117+
},
114118
},
115119
ResourceAttributes: metadata.DefaultResourceAttributesConfig(),
116120
},
@@ -132,7 +136,7 @@ func TestScraperWithNodeUtilization(t *testing.T) {
132136

133137
md, err := r.Scrape(context.Background())
134138
require.NoError(t, err)
135-
require.Equal(t, numContainers, md.DataPointCount())
139+
require.Equal(t, numContainers+numPods, md.DataPointCount())
136140
expectedFile := filepath.Join("testdata", "scraper", "test_scraper_cpu_util_nodelimit_expected.yaml")
137141

138142
// Uncomment to regenerate '*_expected.yaml' files

receiver/kubeletstatsreceiver/testdata/config.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,9 @@ kubeletstats/container_cpu_node_utilization:
3434
metrics:
3535
k8s.container.cpu.node.utilization:
3636
enabled: true
37+
kubeletstats/pod_cpu_node_utilization:
38+
collection_interval: 10s
39+
metric_groups: [ container, pod, node ]
40+
metrics:
41+
k8s.pod.cpu.node.utilization:
42+
enabled: true

0 commit comments

Comments
 (0)