Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.16](backport #41216) Fix Metricbeat k8s metadata sometimes not being present at startup #41285

Merged
merged 1 commit into from
Oct 18, 2024

Conversation

mergify[bot]
Copy link
Contributor

@mergify mergify bot commented Oct 17, 2024

Proposed commit message

Fix Metricbeat k8s metadata sometimes not being present at startup

In the metricbeat k8s module, each metricset has an enricher, which is used to add k8s metadata to events. Enrichers use watchers to watch k8s resource changes, and these watchers can be shared between enrichers. This is implemented by each enricher maintaining a writethrough cache of metadata - when a K8s event indicating a change to a resource is received, the metadata cache is updated.

The problem with this is that a watcher is only started once, so enrichers which are created after it was started, have to catch up on the resource change events. This is a great opportunity for race conditions, and is in general unnecessarily complicated.

Instead, we turn the writethrough cache into a readthrough cache. The change event handlers now only invalidate the cache, and the enricher computes the metadata when it is first requested. This completely sidesteps the catch up problem, as we only compute the metadata lazily.

I've also refactored the code a bit:

  • Resource event handlers are now added to a watcher when it is created, as opposed to when an enricher is created. They're always the same, so there's no reason to do it multiple times.
  • Removed TestBuildMetadataEnricher_EventHandler_PastObjects, as it doesn't seem to check anything not covered by other tests.
  • Rearranged the createWatcher function, so actual watcher creation (as opposed to skipping because it's already there) happens on the main path.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I have added an entry in CHANGELOG.next.asciidoc or CHANGELOG-developer.next.asciidoc.

How to test this PR locally

Install elastic-agent in K8s using the standalone manifests and track the metadata.

Related issues


This is an automatic backport of pull request #41216 done by [Mergify](https://mergify.com).

…41216)

* Fix Metricbeat k8s metadata sometimes not being present at startup

* Clone the whole metadata map when fetching

(cherry picked from commit 4e62fa5)
@mergify mergify bot requested a review from a team as a code owner October 17, 2024 13:44
@mergify mergify bot added the backport label Oct 17, 2024
@mergify mergify bot requested review from gizas and constanca-m and removed request for a team October 17, 2024 13:44
@botelastic botelastic bot added the needs_team Indicates that the issue/PR needs a Team:* label label Oct 17, 2024
@botelastic
Copy link

botelastic bot commented Oct 17, 2024

This pull request doesn't have a Team:<team> label.

@MichaelKatsoulis MichaelKatsoulis merged commit 91d920d into 8.16 Oct 18, 2024
38 checks passed
@MichaelKatsoulis MichaelKatsoulis deleted the mergify/bp/8.16/pr-41216 branch October 18, 2024 06:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport needs_team Indicates that the issue/PR needs a Team:* label
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants