Prune stale per-container version metrics to stop unbounded Prometheus series growth#434
Open
Copilot wants to merge 3 commits into
Open
Prune stale per-container version metrics to stop unbounded Prometheus series growth#434Copilot wants to merge 3 commits into
Copilot wants to merge 3 commits into
Conversation
Agent-Logs-Url: https://github.com/jetstack/version-checker/sessions/f773d382-07fb-40b4-8c53-eec3cb652854 Co-authored-by: davidcollom <1504448+davidcollom@users.noreply.github.com>
Agent-Logs-Url: https://github.com/jetstack/version-checker/sessions/f773d382-07fb-40b4-8c53-eec3cb652854 Co-authored-by: davidcollom <1504448+davidcollom@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix memory leak in version-checker causing OOM kills
Prune stale per-container version metrics to stop unbounded Prometheus series growth
May 9, 2026
There was a problem hiding this comment.
Pull request overview
This PR addresses unbounded Prometheus series growth by pruning stale per-container “current state” gauge series when the observed image/version for a container changes, keeping metric cardinality bounded for long-lived clusters.
Changes:
- In
AddImage, delete existing per-container gauge series foris_latest_versionandlast_checkedbefore recording the newest values. - Add a regression test to ensure stale version-labeled
is_latest_versionseries are removed when the same container is updated multiple times.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pkg/metrics/metrics.go |
Prunes existing per-container gauge series prior to writing updated “current state” metrics to prevent series buildup. |
pkg/metrics/metrics_test.go |
Adds a regression test asserting stale version-labeled gauge series are removed after updates. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+121
to
+127
| labels := buildContainerPartialLabels(namespace, pod, container, containerType) | ||
|
|
||
| // Remove any existing "current state" gauge series for this container before | ||
| // registering the newest values. Otherwise each version change leaves behind | ||
| // a distinct Prometheus series due to the current/latest version labels. | ||
| m.containerImageVersion.DeletePartialMatch(labels) | ||
| m.containerImageChecked.DeletePartialMatch(labels) |
Comment on lines
+76
to
+88
| func TestAddImageReplacesExistingVersionMetrics(t *testing.T) { | ||
| reg := prometheus.NewRegistry() | ||
| m := New(logrus.NewEntry(logrus.New()), reg, fakek8s) | ||
|
|
||
| m.AddImage("namespace", "pod", "container", "container", "url", false, "1.0.0", "1.1.0") | ||
| m.AddImage("namespace", "pod", "container", "container", "url", true, "1.1.0", "1.1.0") | ||
|
|
||
| assert.Equal(t, 1, | ||
| testutil.CollectAndCount(m.containerImageVersion.MetricVec, MetricNamespace+"_is_latest_version"), | ||
| ) | ||
| assert.Equal(t, 1, | ||
| testutil.CollectAndCount(m.containerImageChecked.MetricVec, MetricNamespace+"_last_checked"), | ||
| ) |
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.
version-checkerwas retaining supersededversion_checker_is_latest_versionseries for active containers. In clusters that run long enough and observe image/version churn, this causes Prometheus label cardinality to grow over time and can present as steady memory growth/OOMs.Root cause
AddImagewritesversion_checker_is_latest_versionwithcurrent_versionandlatest_versionin the label set.Change
version_checker_is_latest_versionversion_checker_last_checkedResult
Regression coverage