Skip to content

Conversation

@bxrne
Copy link

@bxrne bxrne commented Dec 28, 2025

What this PR does / why we need it:

Fixes duplicate kube_horizontalpodautoscaler_spec_target_metric and kube_horizontalpodautoscaler_status_target_metric metrics when multiple containers use the same resource (e.g., CPU) in a HorizontalPodAutoscaler. I also added a unit test case to verify this fix.

Previously, when an HPA specified multiple ContainerResource metrics with the same resource name but different containers, kube-state-metrics would generate metrics with identical label sets. This caused Prometheus 2.52.0+ to log duplicate metric errors and drop samples:

msg="Error on ingesting samples with different value but same timestamp" num_dropped=12

This PR adds a container label to ContainerResource metrics, making them distinguishable:

  • Before: kube_horizontalpodautoscaler_spec_target_metric{metric_name="cpu",metric_target_type="utilization"}
  • After: kube_horizontalpodautoscaler_spec_target_metric{metric_name="cpu",metric_target_type="utilization",container="main"}

For example, an HPA with three containers (main, sidecar1, sidecar2) all scaling on CPU now produces three distinct metrics instead of duplicates.

How does this change affect the cardinality of KSM: increases

Cardinality increases slightly for HPAs using ContainerResource metrics. Each container adds one additional metric cardinality dimension. This is the intended behavior to distinguish per-container resource targets.

Which issue(s) this PR fixes:

Fixes #2403

…rce metrics

Addressing kubernetes#2403 so that metrics are distinguishable via the container
label, stopping prometheus duplicate issues
@k8s-ci-robot k8s-ci-robot added do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Dec 28, 2025
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: bxrne
Once this PR has been reviewed and has the lgtm label, please assign dgrisonnet for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Dec 28, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot
Copy link
Contributor

Welcome @bxrne!

It looks like this is your first PR to kubernetes/kube-state-metrics 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes/kube-state-metrics has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@github-project-automation github-project-automation bot moved this to Needs Triage in SIG Instrumentation Dec 28, 2025
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Dec 28, 2025
@bxrne bxrne changed the title Fix #2403 Added container label to ContainerResource metrics Added container label to ContainerResource metrics for Prometheus duplicate issue Dec 28, 2025
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/invalid-commit-message Indicates that a PR should not merge because it has an invalid commit message. label Dec 28, 2025
@bxrne bxrne changed the title Added container label to ContainerResource metrics for Prometheus duplicate issue fix(horizontalpodautoscaler): Added container label to ContainerResource metrics Dec 28, 2025
@bxrne
Copy link
Author

bxrne commented Jan 29, 2026

@CatherineF-dev @mrueg should i mark this PR as stale or backlog, i understand its priority is low?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

Status: Needs Triage

Development

Successfully merging this pull request may close these issues.

Duplicate kube_horizontalpodautoscaler_spec_target_metric

2 participants