Skip to content

metrics: add workload-level preemption count gauge#11252

Open
gyliu513 wants to merge 1 commit into
kubernetes-sigs:mainfrom
gyliu513:metric
Open

metrics: add workload-level preemption count gauge#11252
gyliu513 wants to merge 1 commit into
kubernetes-sigs:mainfrom
gyliu513:metric

Conversation

@gyliu513
Copy link
Copy Markdown
Member

What type of PR is this?

/kind bug

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #11171

Special notes for your reviewer:

Does this PR introduce a user-facing change?

metrics: add workload-level preemption count gauge

@k8s-ci-robot k8s-ci-robot added release-note Denotes a PR that will be considered when it comes time to generate release notes. kind/bug Categorizes issue or PR as related to a bug. labels May 15, 2026
@gyliu513 gyliu513 marked this pull request as draft May 15, 2026 20:55
@netlify
Copy link
Copy Markdown

netlify Bot commented May 15, 2026

Deploy Preview for kubernetes-sigs-kueue ready!

Name Link
🔨 Latest commit ac18d60
🔍 Latest deploy log https://app.netlify.com/projects/kubernetes-sigs-kueue/deploys/6a07beac3197b90009d606d7
😎 Deploy Preview https://deploy-preview-11252--kubernetes-sigs-kueue.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels May 15, 2026
@k8s-ci-robot
Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: gyliu513
Once this PR has been reviewed and has the lgtm label, please assign tenzen-y 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 size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 15, 2026
@gyliu513 gyliu513 marked this pull request as ready for review May 16, 2026 00:47
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 16, 2026
@k8s-ci-robot k8s-ci-robot requested a review from kshalot May 16, 2026 00:47
@mimowo
Copy link
Copy Markdown
Contributor

mimowo commented May 18, 2026

cc @amy ptal

Comment thread pkg/metrics/metrics.go

// +metricsdoc:group=workload
// +metricsdoc:labels=namespace="the namespace of the preempted workload",workload="the name of the preempted workload",cluster_queue="the ClusterQueue of the preempted workload",reason="eviction or preemption reason"
WorkloadPreemptionsCount *prometheus.GaugeVec
Copy link
Copy Markdown
Contributor

@amy amy May 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to model similar to PreemptedWorkloadsTotal, please rename count to total.

Comment thread pkg/metrics/metrics.go
}, append([]string{"preempting_cluster_queue", "reason", "replica_role"}, extraLabels...),
)

WorkloadPreemptionsCount = prometheus.NewGaugeVec(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Help text says: "Uses a Gauge (rather than a Counter) so that the time series can be deleted when the workload finishes or is removed, keeping cardinality bounded."That rationale is incorrect. CounterVec supports DeletePartialMatch exactly the same way. In fact, ClearClusterQueueMetrics at pkg/metrics/metrics.go:1038 already deletes PreemptedWorkloadsTotal (a CounterVec) via DeletePartialMatch.

Using a Gauge for a value that only ever increments is a Prometheus anti-pattern: it breaks rate()/increase() and the reset-detection semantics those functions rely on. Operators who try to chart preemption rate per workload will get wrong answers.

Please switch to CounterVec and update the help text. The variable should also be renamed accordingly (WorkloadPreemptionsTotal, with metric name workload_preemptions_total), matching both Prometheus naming convention and PreemptedWorkloadsTotal for ClusterQueue.

Comment thread pkg/metrics/metrics.go
Uses a Gauge (rather than a Counter) so that the time series can be deleted when the workload finishes or is removed, keeping cardinality bounded.
This gauge is deleted when the workload is completed or deleted.`,
}, []string{"namespace", "workload", "cluster_queue", "reason"},
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please mirror PreemptedWorkloadsTotal. Add replica_role and extraLabels.

Comment thread pkg/metrics/metrics.go
EvictedWorkloadsTotal,
EvictedWorkloadsOnceTotal,
PreemptedWorkloadsTotal,
WorkloadPreemptionsCount,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As an example, per-LocalQueue metrics are gated by LocalQueueMetrics: pkg/metrics/metrics.go:1344, RegisterLQMetrics().

Per workload has an even higher cardinality. Please add a featuregate for per workload metrics.


finishedCond := apimeta.FindStatusCondition(wl.Status.Conditions, kueue.WorkloadFinished)
if finishedCond != nil && finishedCond.Status == metav1.ConditionTrue {
metrics.ClearWorkloadPreemptionMetrics(wl.Namespace, wl.Name)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When workloadRetention.afterFinished is configured, the workload object is retained for some duration after it finishes, but this call wipes its preemption history immediately on the first finished reconcile. An operator inspecting a retained finished workload (which is the whole point of the retention feature) will see zero preemptions even if it was preempted many times.

Move the metrics.ClearWorkloadPreemptionMetrics(...) call into the branch where we actually delete the workload (line 268, just before/after r.client.Delete), and rely on the Delete event handler at line 1141 for the no-retention path. That way the metric lifetime matches the object lifetime.

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. kind/bug Categorizes issue or PR as related to a bug. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

metrics: add workload-level preemption count gauge

4 participants