feat(inputs.mem): Add extended memory statistics#18532
Conversation
Add platform-specific extended memory fields behind a `collect_extended` config option (default: false). Linux provides active_file, inactive_file, active_anon, inactive_anon, unevictable, and percpu fields via /proc/meminfo. Windows provides commit, virtual, physical, and page file counters.
008ce25 to
51ca14c
Compare
| testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime()) | ||
| } | ||
|
|
||
| var linuxBaseFields = map[string]interface{}{ |
There was a problem hiding this comment.
Please define those in the test that uses the fields! This avoids potential side effects and it also makes the test more self-contained.
| }, | ||
| } | ||
|
|
||
| baseFields := linuxBaseFields |
There was a problem hiding this comment.
Please define this in the t.Run section to avoid side effects between tests!
|
|
||
| var mps psutil.MockPS | ||
| defer mps.AssertExpectations(t) | ||
| var acc testutil.Accumulator |
There was a problem hiding this comment.
Please define the accumulator directly before using it to minimize the scope of the variable!
| } | ||
|
|
||
| require.NoError(t, plugin.Init()) | ||
| plugin.platform = "linux" |
There was a problem hiding this comment.
Why do we need this? The test is only run on Linux, isn't it?
| } | ||
| } | ||
|
|
||
| func TestMemStatsCollectExtendedDisabled(t *testing.T) { |
|
|
||
| err := plugin.Init() | ||
| require.NoError(t, err) |
There was a problem hiding this comment.
| err := plugin.Init() | |
| require.NoError(t, err) | |
| require.NoError(t, plugin.Init()) |
| require.NoError(t, plugin.Gather(&acc)) | ||
|
|
||
| expected := []telegraf.Metric{ | ||
| testutil.MustMetric("mem", map[string]string{}, map[string]interface{}{ |
There was a problem hiding this comment.
Please use
| testutil.MustMetric("mem", map[string]string{}, map[string]interface{}{ | |
| metric.New( | |
| "mem", | |
| map[string]string{}, | |
| map[string]interface{}{ |
| "phys_avail": uint64(0), | ||
| "page_file_total": uint64(0), | ||
| "page_file_avail": uint64(0), | ||
| }, time.Unix(0, 0), telegraf.Gauge), |
There was a problem hiding this comment.
| }, time.Unix(0, 0), telegraf.Gauge), | |
| }, | |
| time.Unix(0, 0), telegraf.Gauge), |
| extendedFields: map[string]interface{}{ | ||
| "active_anon": uint64(5765169152), | ||
| "inactive_anon": uint64(1082245120), | ||
| "active_file": uint64(3535425536), | ||
| "inactive_file": uint64(4421992448), | ||
| "unevictable": uint64(143360), | ||
| "percpu": uint64(5767168), | ||
| }, |
There was a problem hiding this comment.
Please use
| extendedFields: map[string]interface{}{ | |
| "active_anon": uint64(5765169152), | |
| "inactive_anon": uint64(1082245120), | |
| "active_file": uint64(3535425536), | |
| "inactive_file": uint64(4421992448), | |
| "unevictable": uint64(143360), | |
| "percpu": uint64(5767168), | |
| }, | |
| extended: []telegraf.Metric{ | |
| metric.New( | |
| "mem", | |
| map[string]string{}, | |
| map[string]interface{}{ | |
| "total": uint64(12400), | |
| "available": uint64(7600), | |
| "used": uint64(5000), | |
| "used_percent": 100 * float64(5000) / float64(12400), | |
| "available_percent": 100 * float64(7600) / float64(12400), | |
| "free": uint64(1235), | |
| "active": uint64(0), | |
| "buffered": uint64(0), | |
| "cached": uint64(0), | |
| "commit_limit": uint64(0), | |
| "committed_as": uint64(0), | |
| "dirty": uint64(0), | |
| "high_free": uint64(0), | |
| "high_total": uint64(0), | |
| "huge_pages_free": uint64(0), | |
| "huge_page_size": uint64(0), | |
| "huge_pages_total": uint64(0), | |
| "inactive": uint64(0), | |
| "low_free": uint64(0), | |
| "low_total": uint64(0), | |
| "mapped": uint64(0), | |
| "page_tables": uint64(0), | |
| "shared": uint64(0), | |
| "slab": uint64(0), | |
| "sreclaimable": uint64(0), | |
| "sunreclaim": uint64(0), | |
| "swap_cached": uint64(0), | |
| "swap_free": uint64(0), | |
| "swap_total": uint64(0), | |
| "vmalloc_chunk": uint64(0), | |
| "vmalloc_total": uint64(0), | |
| "vmalloc_used": uint64(0), | |
| "write_back_tmp": uint64(0), | |
| "write_back": uint64(0), | |
| "active_anon": uint64(5765169152), | |
| "inactive_anon": uint64(1082245120), | |
| "active_file": uint64(3535425536), | |
| "inactive_file": uint64(4421992448), | |
| "unevictable": uint64(143360), | |
| "percpu": uint64(5767168), | |
| }, | |
| time.Unix(0, 0), | |
| telegraf.Gauge, | |
| ), | |
| }, |
and the same below.
Expand expected metrics and use metric.New instead of testutil.MustMetric
|
Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip. 🥳 This pull request decreases the Telegraf binary size by -1.69 % for linux amd64 (new size: 298.8 MB, nightly size 303.9 MB) 📦 Click here to get additional PR build artifactsArtifact URLs |
Summary
Add platform-specific extended memory fields behind a
collect_extendedconfig option (default: false). Linux provides active_file, inactive_file, active_anon, inactive_anon, unevictable, and percpu fields via /proc/meminfo. Windows provides commit, virtual, physical, and page file counters.This is a rewrite of the previous closed pull request, #18281, which takes into account all the comments provided: the code has been simplified, current tests have not changed, etc.
Checklist
Related issues
resolves #17925