Skip to content

remove global paths from metricbeat#51591

Merged
leehinman merged 8 commits into
elastic:mainfrom
leehinman:15154_metricbeat_paths
Jul 7, 2026
Merged

remove global paths from metricbeat#51591
leehinman merged 8 commits into
elastic:mainfrom
leehinman:15154_metricbeat_paths

Conversation

@leehinman

@leehinman leehinman commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Proposed commit message

This change removes the use of global/singleton path state from Metricbeat and replaces it with per-instance paths, fixing a bug where multiple Metricbeat receivers could share the same data store.

Problem

Metricbeat was using global paths.Paths (a package-level singleton) when resolving file paths like the SQL cursor data store. In receiver mode, multiple Metricbeat instances share the same process, so they would all resolve to the same data path, causing them to share state incorrectly.

Changes

  • metricbeat/mb/mb.go: Added a Paths *paths.Path field to BaseModule so each module carries its own path instance.
  • metricbeat/mb/builders.go: Threaded the *paths.Path parameter through newBaseModuleFromConfig so it is assigned to the BaseModule at construction time.
  • metricbeat/mb/lightmetricset.go: Updated LightMetricSet.baseModule() to pass the metric set's path to newBaseModuleFromConfig.
  • x-pack/libbeat/cmd/instance/beat.go: Simplified NewBeatForReceiver by removing the filebeat-specific branch for path initialization — both filebeat and metricbeat now use the same path initialization path, constructing a fresh paths.Path instance per beat.
  • x-pack/metricbeat/module/sql/module.go: Replaced paths.Resolve(paths.Data, "sql-cursor") (global) with m.Paths.Resolve(paths.Data, "sql-cursor") (per-instance), so each SQL module instance resolves its own data directory.

Checklist

  • My code follows the style guidelines of this project
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have made corresponding change to the default configuration files
  • I have added tests that prove my fix is effective or that my feature works. Where relevant, I have used the stresstest.sh script to run them under stress conditions and race detector to verify their stability.
  • I have added an entry in ./changelog/fragments using the changelog tool.

Disruptive User Impact

None. Stand alone Metricbeat will function as before, only metricbeat receiver will have multiple beat instances in the same process. This change will allow the sql module to have unique data path per beat instance.

How to test this PR locally

cd x-pack/metricbeat/module/sql
go test .

Related issues

Use cases

Screenshots

Logs

@botelastic botelastic Bot added the needs_team Indicates that the issue/PR needs a Team:* label label Jun 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🤖 GitHub comments

Just comment with:

  • run docs-build : Re-trigger the docs validation. (use unformatted text in the comment!)
  • /test : Run the Buildkite pipeline.

@mergify

mergify Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

This pull request does not have a backport label.
If this is a bug or security fix, could you label this PR @leehinman? 🙏.
For such, you'll need to label your PR with:

  • The upcoming major version of the Elastic Stack
  • The upcoming minor version of the Elastic Stack (if you're not pushing a breaking change)

To fixup this pull request, you need to add the backport labels for the needed
branches, such as:

  • backport-8./d is the label to automatically backport to the 8./d branch. /d is the digit
  • backport-active-all is the label that automatically backports to all active branches.
  • backport-active-8 is the label that automatically backports to all active minor branches for the 8 major.
  • backport-active-9 is the label that automatically backports to all active minor branches for the 9 major.

@leehinman leehinman added Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team backport-active-all Automated backport with mergify to all the active branches labels Jun 26, 2026
@botelastic botelastic Bot removed the needs_team Indicates that the issue/PR needs a Team:* label label Jun 26, 2026
@leehinman leehinman force-pushed the 15154_metricbeat_paths branch 2 times, most recently from 3d91b63 to 32663e6 Compare June 26, 2026 20:56
@leehinman leehinman marked this pull request as ready for review June 26, 2026 22:33
@leehinman leehinman requested review from a team as code owners June 26, 2026 22:33
@leehinman leehinman requested review from mauri870 and rdner June 26, 2026 22:33
@infra-vault-gh-plugin-prod

Copy link
Copy Markdown

Pinging @elastic/elastic-agent-data-plane (Team:Elastic-Agent-Data-Plane)

@leehinman leehinman requested a review from orestisfl June 26, 2026 22:33
@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

This PR adds a changelog fragment and threads per-instance paths through Metricbeat construction, receiver startup, and SQL cursor registry code. BaseModule now stores Paths, constructor helpers pass path values through, and tests use the updated signatures and injected paths. NewBeatForReceiver now initializes paths from config without a beat-name branch. The SQL module resolves cursor-registry data paths from the module instance and uses the module logger, and its tests avoid mutating global path state.

Suggested labels: bugfix

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The PR threads per-instance paths through Metricbeat and SQL cursor state, matching the issue’s requirement to isolate receiver stores.
Out of Scope Changes check ✅ Passed All code changes support the per-instance path fix or its tests/changelog; no unrelated functionality was introduced.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • 🛠️ Update Documentation

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@orestisfl orestisfl left a comment

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.

LGTM other than the changelog fragment

Comment thread changelog/fragments/1782499306-remove-global-paths-from-metricbeat.yaml Outdated
@leehinman

Copy link
Copy Markdown
Contributor Author

@elastic/obs-infraobs-integrations any chance you can review?

@github-actions

This comment has been minimized.

@orestisfl

Copy link
Copy Markdown
Contributor

@leehinman While we are at it, do you think we can migrate off global paths in the tests as well?
0001-x-pack-metricbeat-sql-migrate-cursor-tests-off-the-g.patch

@leehinman

leehinman commented Jul 1, 2026

Copy link
Copy Markdown
Contributor Author

@leehinman While we are at it, do you think we can migrate off global paths in the tests as well?
0001-x-pack-metricbeat-sql-migrate-cursor-tests-off-the-g.patch

Originally I was trying to "fix" quickly since this is a bug affecting users, but since we haven't gotten the external reviews yet and you have such a nice patch, I'll add it.

Added in 48fc971

@coderabbitai coderabbitai Bot left a comment

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.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@x-pack/metricbeat/module/sql/query/cursor_integration_test.go`:
- Line 10: The test file has an unused context import that causes the build to
fail; remove the import from the import block in cursor_integration_test.go, or
if it is meant to be used, add it to the relevant test setup or helper functions
so the compiler sees an actual reference.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Enterprise

Run ID: daf634f9-9bc9-4b66-b0dd-96fc624c61e4

📥 Commits

Reviewing files that changed from the base of the PR and between c6ed610 and 7ba85af.

📒 Files selected for processing (1)
  • x-pack/metricbeat/module/sql/query/cursor_integration_test.go

Comment thread x-pack/metricbeat/module/sql/query/cursor_integration_test.go Outdated
@leehinman leehinman force-pushed the 15154_metricbeat_paths branch 2 times, most recently from c72ff75 to d7c6ff1 Compare July 1, 2026 17:03
@mergify

mergify Bot commented Jul 3, 2026

Copy link
Copy Markdown
Contributor

This pull request is now in conflicts. Could you fix it? 🙏
To fixup this pull request, you can check out it locally. See documentation: https://help.github.com/articles/checking-out-pull-requests-locally/

git fetch upstream
git checkout -b 15154_metricbeat_paths upstream/15154_metricbeat_paths
git merge upstream/main
git push upstream 15154_metricbeat_paths

@leehinman leehinman force-pushed the 15154_metricbeat_paths branch from d7c6ff1 to f12b77f Compare July 6, 2026 14:12
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

TL;DR

x-pack/metricbeat: Run check/update is failing in go vet because x-pack/metricbeat/module/sql/query/query_test.go:113 still calls mb.NewModule with the old four-argument form. Update that call to pass a beat.Info, matching the new mb.NewModule(config, register, beat.Info) signature.

Remediation

  • Change x-pack/metricbeat/module/sql/query/query_test.go:113 from mb.NewModule(c, mb.Registry, beatPaths, logptest.NewTestingLogger(t, "")) to mb.NewModule(c, mb.Registry, beat.Info{Paths: beatPaths, Logger: logptest.NewTestingLogger(t, "")}).
  • Validate with make -C x-pack/metricbeat check update && make check-no-changes.
Investigation details

Root Cause

This is a code bug: one test helper was migrated from mbtest.NewMetricSet to mb.NewModule, but it kept the previous path/logger argument style. In the PR head, metricbeat/mb/builders.go:54 defines:

func NewModule(config *conf.C, r *Register, info beat.Info) (Module, []MetricSet, error)

However, x-pack/metricbeat/module/sql/query/query_test.go:113 calls:

_, metricsets, err := mb.NewModule(c, mb.Registry, beatPaths, logptest.NewTestingLogger(t, ""))

The same file already imports github.com/elastic/beats/v7/libbeat/beat at x-pack/metricbeat/module/sql/query/query_test.go:19, and another helper already uses the correct beat.Info{Paths: beatPaths, Logger: ...} form at x-pack/metricbeat/module/sql/query/query_test.go:172.

Evidence

# github.com/elastic/beats/v7/x-pack/metricbeat/module/sql/query
vet: module/sql/query/query_test.go:113:64: too many arguments in call to mb.NewModule
	have (*config.C, *mb.Register, *paths.Path, *logp.Logger)
	want (*config.C, *mb.Register, beat.Info)
Error: failed running go vet, please fix the issues reported: running "go vet ./..." failed with exit code 1

Verification

  • Not run locally: the workspace checkout is main (ccdb808067f5db2ab06512ab32350a15de018905), not the PR head, so the exact failing state is only available from the Buildkite log and GitHub PR-head file contents.
  • Checked existing PR comments; the prior detective comment covered a different x-pack/filebeat packaging failure, so this is not a duplicate.

What is this? | From workflow: PR Buildkite Detective

Give us feedback! React with 🚀 if perfect, 👍 if helpful, 👎 if not.

@mergify

mergify Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

Tick the box to add this pull request to the merge queue (same as @mergifyio queue).

  • Queue this pull request

@leehinman leehinman merged commit 37f3d26 into elastic:main Jul 7, 2026
137 of 138 checks passed
@github-actions

github-actions Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

@Mergifyio backport 9.5 9.4 9.3 8.19

@mergify

mergify Bot commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

backport 9.5 9.4 9.3 8.19

❌ No backport have been created

Details
  • Backport to branch 9.5 failed

GitHub error: Branch not found

Cherry-pick of 37f3d26 has failed:

On branch mergify/bp/9.4/pr-51591
Your branch is up to date with 'origin/9.4'.

You are currently cherry-picking commit 37f3d269d.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	new file:   changelog/fragments/1782499306-remove-global-paths-from-metricbeat.yaml
	modified:   metricbeat/mb/lightmetricset.go
	modified:   metricbeat/mb/lightmetricset_test.go
	modified:   metricbeat/mb/mb.go
	modified:   metricbeat/mb/mb_test.go
	modified:   x-pack/metricbeat/module/sql/module.go
	modified:   x-pack/metricbeat/module/sql/module_test.go
	modified:   x-pack/metricbeat/module/sql/query/cursor/store_test.go
	modified:   x-pack/metricbeat/module/sql/query/cursor_integration_test.go
	modified:   x-pack/metricbeat/module/sql/query/query_integration_test.go

Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   metricbeat/mb/builders.go
	both modified:   x-pack/libbeat/cmd/instance/beat.go
	both modified:   x-pack/metricbeat/module/sql/query/query_test.go

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Cherry-pick of 37f3d26 has failed:

On branch mergify/bp/9.3/pr-51591
Your branch is up to date with 'origin/9.3'.

You are currently cherry-picking commit 37f3d269d.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	new file:   changelog/fragments/1782499306-remove-global-paths-from-metricbeat.yaml
	modified:   metricbeat/mb/lightmetricset.go
	modified:   metricbeat/mb/lightmetricset_test.go
	modified:   metricbeat/mb/mb.go
	modified:   metricbeat/mb/mb_test.go

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	both modified:   metricbeat/mb/builders.go
	both modified:   x-pack/libbeat/cmd/instance/beat.go
	deleted by us:   x-pack/metricbeat/module/sql/module.go
	deleted by us:   x-pack/metricbeat/module/sql/module_test.go
	deleted by us:   x-pack/metricbeat/module/sql/query/cursor/store_test.go
	deleted by us:   x-pack/metricbeat/module/sql/query/cursor_integration_test.go
	both modified:   x-pack/metricbeat/module/sql/query/query_integration_test.go
	deleted by us:   x-pack/metricbeat/module/sql/query/query_test.go

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

Cherry-pick of 37f3d26 has failed:

On branch mergify/bp/8.19/pr-51591
Your branch is up to date with 'origin/8.19'.

You are currently cherry-picking commit 37f3d269d.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --skip" to skip this patch)
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Changes to be committed:
	new file:   changelog/fragments/1782499306-remove-global-paths-from-metricbeat.yaml
	modified:   metricbeat/mb/lightmetricset.go
	modified:   metricbeat/mb/lightmetricset_test.go
	modified:   metricbeat/mb/mb.go
	modified:   metricbeat/mb/mb_test.go

Unmerged paths:
  (use "git add/rm <file>..." as appropriate to mark resolution)
	both modified:   metricbeat/mb/builders.go
	both modified:   x-pack/libbeat/cmd/instance/beat.go
	deleted by us:   x-pack/metricbeat/module/sql/module.go
	deleted by us:   x-pack/metricbeat/module/sql/module_test.go
	deleted by us:   x-pack/metricbeat/module/sql/query/cursor/store_test.go
	deleted by us:   x-pack/metricbeat/module/sql/query/cursor_integration_test.go
	both modified:   x-pack/metricbeat/module/sql/query/query_integration_test.go
	deleted by us:   x-pack/metricbeat/module/sql/query/query_test.go

To fix up this pull request, you can check it out locally. See documentation: https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/reviewing-changes-in-pull-requests/checking-out-pull-requests-locally

leehinman added a commit that referenced this pull request Jul 9, 2026
* remove global paths from metricbeat (#51591)

* remove global paths from metricbeat

* x-pack/metricbeat/sql: migrate cursor tests off the global paths singleton

---------

Co-authored-by: Orestis Floros <orestis.floros@elastic.co>
(cherry picked from commit 37f3d26)

# Conflicts:
#	metricbeat/mb/builders.go
#	x-pack/libbeat/cmd/instance/beat.go
#	x-pack/metricbeat/module/sql/query/query_test.go

* fix merge conflicts

---------

Co-authored-by: Lee E Hinman <57081003+leehinman@users.noreply.github.com>
Co-authored-by: Lee E. Hinman <lee.e.hinman@elastic.co>
leehinman added a commit that referenced this pull request Jul 9, 2026
* remove global paths from metricbeat (#51591)

* remove global paths from metricbeat

* x-pack/metricbeat/sql: migrate cursor tests off the global paths singleton

---------

Co-authored-by: Orestis Floros <orestis.floros@elastic.co>
(cherry picked from commit 37f3d26)

# Conflicts:
#	metricbeat/mb/builders.go
#	x-pack/libbeat/cmd/instance/beat.go
#	x-pack/metricbeat/module/sql/module.go
#	x-pack/metricbeat/module/sql/module_test.go
#	x-pack/metricbeat/module/sql/query/cursor/store_test.go
#	x-pack/metricbeat/module/sql/query/cursor_integration_test.go
#	x-pack/metricbeat/module/sql/query/query_integration_test.go
#	x-pack/metricbeat/module/sql/query/query_test.go

* fix merge conflicts

* remove files that shouldn't have been backported

* remove files that shouldn't have been backported

---------

Co-authored-by: Lee E Hinman <57081003+leehinman@users.noreply.github.com>
Co-authored-by: Lee E. Hinman <lee.e.hinman@elastic.co>
leehinman added a commit that referenced this pull request Jul 13, 2026
* remove global paths from metricbeat (#51591)

* remove global paths from metricbeat


Co-authored-by: Lee E Hinman <57081003+leehinman@users.noreply.github.com>
Co-authored-by: Lee E. Hinman <lee.e.hinman@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-active-all Automated backport with mergify to all the active branches Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Metricbeat receivers share the same store and "corrupt" data

4 participants