-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat: add useMemAvailable feature gate #42223
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
Merged
dmitryax
merged 15 commits into
open-telemetry:main
from
rogercoll:mem_used_feature_gate
Sep 23, 2025
Merged
Changes from 5 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
6ea1feb
feat: add useMemAvailable feature gate
rogercoll 83e9ab3
fix linter
rogercoll 4e31718
chore: add changelog entry
rogercoll cb0d6a2
refactor: move feature gate to only Linux files
rogercoll 0d740bf
fix formatting
rogercoll 73e2fa3
Merge branch 'main' into mem_used_feature_gate
ChrsMark f9d8a31
chore: add gopsutil reference formula
rogercoll eaaaeae
Merge branch 'main' into mem_used_feature_gate
ChrsMark 4de7065
Merge branch 'main' into mem_used_feature_gate
rogercoll a96c4fd
Merge branch 'main' into mem_used_feature_gate
rogercoll 98d31ef
Update receiver/hostmetricsreceiver/internal/scraper/memoryscraper/me…
dmitryax ff010f5
set feature gate as beta
rogercoll fe01dfd
Merge branch 'main' into mem_used_feature_gate
rogercoll b59b03f
enable feature gate on test cleanup
rogercoll b3d4ac8
Update receiver/hostmetricsreceiver/internal/scraper/memoryscraper/me…
dmitryax File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # Use this changelog template to create an entry for release notes. | ||
|
|
||
| # One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
| change_type: enhancement | ||
|
|
||
| # The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
| component: hostmetricsreceiver | ||
|
|
||
| # A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
| note: Add useMemAvailable feature gate to use the MemAvailable kernel's statistic to compute the "used" memory usage | ||
|
|
||
| # Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
| issues: [42221] | ||
|
|
||
| # (Optional) One or more lines of additional information to render under the primary note. | ||
| # These lines will be padded with 2 spaces and then inserted directly into the document. | ||
| # Use pipe (|) for multiline entries. | ||
| subtext: | ||
|
|
||
| # If your change doesn't affect end users or the exported elements of any package, | ||
| # you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
| # Optional: The change log or logs in which this entry should be included. | ||
| # e.g. '[user]' or '[user, api]' | ||
| # Include 'user' if the change is relevant to end users. | ||
| # Include 'api' if there is a change to a library API. | ||
| # Default: '[user]' | ||
| change_logs: [] |
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
55 changes: 55 additions & 0 deletions
55
receiver/hostmetricsreceiver/internal/scraper/memoryscraper/memory_scraper_linux_test.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| // Copyright The OpenTelemetry Authors | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package memoryscraper | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| "go.opentelemetry.io/collector/component/componenttest" | ||
| "go.opentelemetry.io/collector/featuregate" | ||
| "go.opentelemetry.io/collector/pdata/pcommon" | ||
| "go.opentelemetry.io/collector/scraper/scrapertest" | ||
|
|
||
| "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/hostmetricsreceiver/internal/scraper/memoryscraper/internal/metadata" | ||
| ) | ||
|
|
||
| func TestScrape_UseMemAvailable(t *testing.T) { | ||
| mbc := metadata.DefaultMetricsBuilderConfig() | ||
| mbc.Metrics.SystemMemoryUtilization.Enabled = true | ||
| mbc.Metrics.SystemMemoryUsage.Enabled = true | ||
| scraperConfig := Config{ | ||
| MetricsBuilderConfig: mbc, | ||
| } | ||
| scraper := newMemoryScraper(t.Context(), scrapertest.NewNopSettings(metadata.Type), &scraperConfig) | ||
|
|
||
| err := scraper.start(t.Context(), componenttest.NewNopHost()) | ||
| require.NoError(t, err, "Failed to initialize memory scraper: %v", err) | ||
|
|
||
| memInfo, err := scraper.virtualMemory(t.Context()) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, memInfo) | ||
|
|
||
| scraper.recordMemoryUsageMetric(pcommon.NewTimestampFromTime(time.Now()), memInfo) | ||
| scraper.recordMemoryUtilizationMetric(pcommon.NewTimestampFromTime(time.Now()), memInfo) | ||
| legacyMd := scraper.mb.Emit() | ||
|
|
||
| // enable feature gate | ||
| _ = featuregate.GlobalRegistry().Set( | ||
| "receiver.hostmetricsreceiver.UseLinuxMemAvailable", true) | ||
| t.Cleanup(func() { | ||
| _ = featuregate.GlobalRegistry().Set("receiver.hostmetricsreceiver.UseLinuxMemAvailable", false) | ||
| }) | ||
| scraper.recordMemoryUsageMetric(pcommon.NewTimestampFromTime(time.Now()), memInfo) | ||
| scraper.recordMemoryUtilizationMetric(pcommon.NewTimestampFromTime(time.Now()), memInfo) | ||
| memUsedMd := scraper.mb.Emit() | ||
|
|
||
| // Used memory calculation based on MemAvailable is greater than "Total | ||
| // - Free - Buffers - Cache" as it takes into account the amount of | ||
| // Cached memory that is not freeable. | ||
| assert.Greater(t, memUsedMd.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).IntValue(), legacyMd.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(0).Sum().DataPoints().At(0).IntValue(), "system.memory.usage for the used state should be greater when computed using memAvailable") | ||
| assert.Greater(t, memUsedMd.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(1).Gauge().DataPoints().At(0).DoubleValue(), legacyMd.ResourceMetrics().At(0).ScopeMetrics().At(0).Metrics().At(1).Gauge().DataPoints().At(0).DoubleValue(), "system.memory.utilization for the used state should be greater when computed using memAvailable") | ||
| } |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we already moved to the updated Used from gopsutil. So we don't need this PR anymore. Or we can make it beta to not get back
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, moved to beta so users can still roll out to the previous formula