Skip to content

Always use the local filesystem for self-monitoring metrics#282

Merged
belimawr merged 11 commits intoelastic:mainfrom
belimawr:fix-self-monitoring-metrics
Feb 12, 2026
Merged

Always use the local filesystem for self-monitoring metrics#282
belimawr merged 11 commits intoelastic:mainfrom
belimawr:fix-self-monitoring-metrics

Conversation

@belimawr
Copy link
Copy Markdown
Contributor

@belimawr belimawr commented Feb 10, 2026

What does this PR do?

Always use the local filesystem for self-monitoring metrics.
Metricbeat will use the hostfs to set the environment variables:

  • HOST_PROC
  • HOST_SYS
  • HOST_ETC

which are read by github.com/shirou/gopsutil/v4 and used when fetching metrics. However, the hostfs is not available by the time self monitoring metrics are initialised, so when running in a container environment, we cannot resolve the PID relative to the hostfs PID namespace.

This causes elastic-agent-system-metrics to get the PID using os.Getpid, and gopsutil uses the hostfs to fetch all metrics, causing us to get metrics from the wrong process or getting an error because /hostfs/proc/<PID>/fd does not exist.

Always using the local filesystem for self-monitoring metrics solves this problem.

Why is it important?

It allows a Beat to correctly get metrics from its own process.

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

## Author's Checklist

Related issues

@belimawr belimawr self-assigned this Feb 10, 2026
@belimawr belimawr added the Team:Elastic-Agent-Data-Plane Label for the Agent Data Plane team label Feb 10, 2026
@belimawr belimawr marked this pull request as ready for review February 11, 2026 20:47
@belimawr belimawr requested a review from a team as a code owner February 11, 2026 20:47
@belimawr belimawr requested review from AndersonQ and khushijain21 and removed request for a team February 11, 2026 20:47
@belimawr belimawr force-pushed the fix-self-monitoring-metrics branch from 28fadb2 to 6378f81 Compare February 11, 2026 23:03
@mauri870 mauri870 requested a review from kruskall February 12, 2026 11:22
belimawr and others added 2 commits February 12, 2026 08:37
Co-authored-by: Mauri de Souza Meneguzzo <mauri870@gmail.com>
@AndersonQ
Copy link
Copy Markdown
Member

one last question. the report package is only used for self monitoring. Is it?

@belimawr
Copy link
Copy Markdown
Contributor Author

one last question. the report package is only used for self monitoring. Is it?

Yes, that's my understanding. Also everything there collect metrics only for the process own PID.

Copy link
Copy Markdown
Member

@mauri870 mauri870 left a comment

Choose a reason for hiding this comment

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

From a code perspective the fix makes sense, but I'm concerned it could break projects that import it.

@belimawr
Copy link
Copy Markdown
Contributor Author

From a code perspective the fix makes sense, but I'm concerned it could break projects that import it.

Yeah, that risk crossed my mind, but the changes only force the metrics to be collected from the root filesystem, which works in any environment.

The existing tests are passing, and I even added a new one that sets the env vars like Metricbeat to an nonexistent path to ensure we never use it.

So I'm pretty confident that this won't break anything else.

@AndersonQ
Copy link
Copy Markdown
Member

From a code perspective the fix makes sense, but I'm concerned it could break projects that import it.

yeah, I checked the report and it indeed only collect metrics. So yeah, it seems fine.

@belimawr belimawr merged commit d791f15 into elastic:main Feb 12, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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 container throws errors when fetching metrics with hostfs set

3 participants