[kubeletstats] Calculate cpu usage on the fly behind feature gate#49499
Open
ChrsMark wants to merge 1 commit into
Open
[kubeletstats] Calculate cpu usage on the fly behind feature gate#49499ChrsMark wants to merge 1 commit into
ChrsMark wants to merge 1 commit into
Conversation
71fcdf4 to
a39853e
Compare
Member
Author
a39853e to
889c58d
Compare
Signed-off-by: ChrsMark <chrismarkou92@gmail.com>
889c58d to
da2385c
Compare
TylerHelmuth
approved these changes
Jul 7, 2026
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an alpha feature-gated path in kubeletstatsreceiver to compute *.cpu.usage metrics (and derived utilization metrics) as a scrape-to-scrape rate of *.cpu.time, rather than using Kubelet’s UsageNanoCores, aligning behavior with the semconv discussion and reducing scrape-interval mismatch issues.
Changes:
- Introduces
receiver.kubeletstats.cpuUsageScrapeBasedfeature gate and generated gate registration. - Adds a per-scraper
CPUUsageCalculatorand wires it through the metrics pipeline to compute usage fromUsageCoreNanoSecondsdeltas and Kubelet-provided sample times. - Adds/updates unit tests and fixtures (including a second-scrape summary) and documents the new feature gate.
Reviewed changes
Copilot reviewed 14 out of 17 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| receiver/kubeletstatsreceiver/scraper.go | Stores and passes a shared CPUUsageCalculator into metrics production. |
| receiver/kubeletstatsreceiver/internal/kubelet/metrics.go | Adds scrape lifecycle hooks (startScrape/endScrape) and propagates the calculator to the accumulator. |
| receiver/kubeletstatsreceiver/internal/kubelet/accumulator.go | Passes stable per-resource keys into CPU metric emission so usage can be computed per resource across scrapes. |
| receiver/kubeletstatsreceiver/internal/kubelet/cpu.go | Adds feature-gated scrape-based CPU usage computation from cumulative CPU time. |
| receiver/kubeletstatsreceiver/internal/kubelet/cpu_usage.go | New calculator for rate computation and pruning of stale resource keys. |
| receiver/kubeletstatsreceiver/internal/kubelet/cpu_usage_test.go | New unit coverage for calculator behavior (first sample, rate, stale/negative cases, pruning, multi-key). |
| receiver/kubeletstatsreceiver/internal/kubelet/cpu_test.go | New tests validating feature-gated behavior and legacy path when gate is disabled. |
| receiver/kubeletstatsreceiver/scraper_test.go | Adds end-to-end scraper tests for the feature gate, including a controlled two-scrape scenario. |
| receiver/kubeletstatsreceiver/testdata/stats-summary-second-scrape.json | New fixture enabling a deterministic “second scrape” CPU usage rate validation. |
| receiver/kubeletstatsreceiver/testdata/scraper/test_scraper_cpu_usage_scrape_based_expected.yaml | New golden expected output for the first-scrape (no usage yet) gated scenario. |
| receiver/kubeletstatsreceiver/metadata.yaml | Declares the new feature gate in component metadata. |
| receiver/kubeletstatsreceiver/internal/metadata/generated_feature_gates.go | Generated feature gate registration for the receiver. |
| receiver/kubeletstatsreceiver/documentation.md | Documents the feature gate in the receiver docs. |
| receiver/kubeletstatsreceiver/go.mod | Adds direct deps needed for feature gates and test helper usage. |
| .chloggen/kubeletstats-cpu-usage-scrape-based.yaml | Adds a changelog entry for the new feature gate. |
Files not reviewed (1)
- receiver/kubeletstatsreceiver/internal/metadata/generated_feature_gates.go: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Description
Note: The big diff is because of the updated expected test files and the added stats' summary sample.
This PR introduces a new way for calculating the
*.cpu.usagemetrics based on the discussions from open-telemetry/semantic-conventions#2418.It adds the
receiver.kubeletstats.cpuUsageScrapeBasedfeature gate which when enabled,container.cpu.usage,k8s.pod.cpu.usageandk8s.node.cpu.usage(and the cpu utilization metrics derived from them) are calculated as the rate of the corresponding*.cpu.timecounter between consecutive scrapes, instead of being read directly from the kubelet'sUsageNanoCoresvalue.The reasoning for this change is that (as discussed in open-telemetry/semantic-conventions#2418), the derived Kubelet metric (
UsageNanoCores) can be inaccurate (see open-telemetry/semantic-conventions#2418 (comment)) given that it's calculated over a fixed interval that Kubelet sets and is not always in alignment with the scrape interval of the Collector. This patch gives better control on this rate calculation following a similar approach with hostmetrics receiver.The time diff is calculated based on
CPUStats.Timethat Kubelet's API provides (The time at which these CPU stats were updated).Link to tracking issue
Fixes #49477
Testing
Added unit tests
Documentation
Tuned
Authorship