Skip to content

Conversation

@Rishab-And-Abhisekh-joint

Description:
Fixes #41123. Supersedes #43896.

Previously, the Prometheus exporter only cleaned up stale metrics during a scrape (Collect). If scrapes stopped, the metric map would grow indefinitely, leading to a memory leak.

This PR adds a background goroutine that cleans up stale metrics every metric_expiration interval, ensuring memory is freed even without active scraping.

Testing:

  • Added TestBackgroundCleanup to verify metrics are removed without a scrape call.
  • Verified that active metrics (with future timestamps) are preserved.

Credit:
Original implementation by @jelly-afk.
I have revived this PR and added the missing unit tests requested by maintainers.

@github-actions
Copy link
Contributor

Welcome, contributor! Thank you for your contribution to opentelemetry-collector-contrib.

Important reminders:

A maintainer will review your pull request soon. Thank you for helping make OpenTelemetry better!

@@ -0,0 +1,5 @@
change_type: bug_fix
Copy link
Contributor

Choose a reason for hiding this comment

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

Please consolidate to a single changelog file (probably the other one).

Copy link
Contributor

Choose a reason for hiding this comment

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

This is still relevant

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package k8sattributesprocessor
Copy link
Contributor

Choose a reason for hiding this comment

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

There are still k8sattributesprocessor changes in this PR

return nil, multiErrs
}

// Map service.name + service.namespace to job
Copy link
Contributor

Choose a reason for hiding this comment

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

Please don't remove all of the comments from the file

Choose a reason for hiding this comment

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

Apologies for the earlier confusion with the git history! I have performed a hard reset and force-pushed a clean state.

Summary of changes addressing your review:

1.Refactor: Moved the background cleanup goroutine to the Start() lifecycle method (added to collector.go) and wired it up in prometheus.go.

2.Cleanup: Removed all accidental k8sattributesprocessor files from this branch.

3.Polish: Restored the original documentation comments in collector.go and removed my temporary comments.

4.Changelog: Consolidated to a single file (.chloggen/prometheus-cleanup.yaml) with the correct component name.

Ready for re-review!

@@ -0,0 +1,5 @@
change_type: bug_fix
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still relevant

@ArthurSens
Copy link
Member

Hey @Rishab-And-Abhisekh-joint, we're still seeing duplicated changelog entries and one extra file in the k8sprocessor. Could you delete those files?

@Rishab-And-Abhisekh-joint
Copy link
Author

Rishab-And-Abhisekh-joint commented Jan 14, 2026

Hey @Rishab-And-Abhisekh-joint, we're still seeing duplicated changelog entries and one extra file in the k8sprocessor. Could you delete those files?

Done. I've removed the duplicate changelog entry and the misplaced client_test.go.

I also updated the reproduction test to use t.Context() instead of context.Background() to fix the linting failure. The CI should pass now.

@ArthurSens ArthurSens removed the processor/k8sattributes k8s Attributes processor label Jan 14, 2026
@github-actions github-actions bot added the processor/k8sattributes k8s Attributes processor label Jan 14, 2026
@ArthurSens
Copy link
Member

@Rishab-And-Abhisekh-joint, if you go to the page with changed files, you'll notice that we're still seeing changes in the k8sprocessor.

I'm taking a guess here that Generative AI is being used to assist with your contribution. My advice here is to take things slowly, understand the changes you're making before adding another commit that goes in the opposite direction of what is being asked.

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

Labels

exporter/prometheus first-time contributor PRs made by new contributors processor/k8sattributes k8s Attributes processor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exporter/prometheus: Metrics clean up should be independent of Collect()

4 participants