Skip to content

feat(inputs.mem): Add extended memory statistics#18532

Merged
skartikey merged 5 commits into
influxdata:masterfrom
pvlltvk:mem-extended-stats
Apr 24, 2026
Merged

feat(inputs.mem): Add extended memory statistics#18532
skartikey merged 5 commits into
influxdata:masterfrom
pvlltvk:mem-extended-stats

Conversation

@pvlltvk

@pvlltvk pvlltvk commented Mar 15, 2026

Copy link
Copy Markdown
Contributor

Summary

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.

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

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.
@telegraf-tiger telegraf-tiger Bot added feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins labels Mar 15, 2026

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for your contribution @pvlltvk! Please find my comments in the code...

Comment thread plugins/inputs/mem/mem_linux.go
Comment thread plugins/inputs/mem/mem.go
Comment thread plugins/inputs/mem/mem_test.go Outdated
@srebhan srebhan self-assigned this Mar 20, 2026
@pvlltvk pvlltvk force-pushed the mem-extended-stats branch from 008ce25 to 51ca14c Compare March 27, 2026 19:39

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @pvlltvk! Some more comments regarding unit-tests...

Comment thread plugins/inputs/mem/mem_test.go Outdated
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
}

var linuxBaseFields = map[string]interface{}{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please define those in the test that uses the fields! This avoids potential side effects and it also makes the test more self-contained.

Comment thread plugins/inputs/mem/mem_test.go Outdated
},
}

baseFields := linuxBaseFields

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please define this in the t.Run section to avoid side effects between tests!

Comment thread plugins/inputs/mem/mem_test.go Outdated

var mps psutil.MockPS
defer mps.AssertExpectations(t)
var acc testutil.Accumulator

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please define the accumulator directly before using it to minimize the scope of the variable!

Comment thread plugins/inputs/mem/mem_test.go Outdated
}

require.NoError(t, plugin.Init())
plugin.platform = "linux"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need this? The test is only run on Linux, isn't it?

Comment thread plugins/inputs/mem/mem_test.go
Comment thread plugins/inputs/mem/mem_test.go Outdated
}
}

func TestMemStatsCollectExtendedDisabled(t *testing.T) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See my comments above!

Comment thread plugins/inputs/mem/mem_test.go Outdated
Comment on lines +257 to +259

err := plugin.Init()
require.NoError(t, err)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
err := plugin.Init()
require.NoError(t, err)
require.NoError(t, plugin.Init())

Comment thread plugins/inputs/mem/mem_test.go Outdated
Comment thread plugins/inputs/mem/mem_test.go

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @pvlltvk! Please expand the expected metrics (see my comment) and also avoid testutil.MustMetric as this will be gone soon.

Comment thread plugins/inputs/mem/mem_test.go Outdated
require.NoError(t, plugin.Gather(&acc))

expected := []telegraf.Metric{
testutil.MustMetric("mem", map[string]string{}, map[string]interface{}{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use

Suggested change
testutil.MustMetric("mem", map[string]string{}, map[string]interface{}{
metric.New(
"mem",
map[string]string{},
map[string]interface{}{

Comment thread plugins/inputs/mem/mem_test.go Outdated
"phys_avail": uint64(0),
"page_file_total": uint64(0),
"page_file_avail": uint64(0),
}, time.Unix(0, 0), telegraf.Gauge),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
}, time.Unix(0, 0), telegraf.Gauge),
},
time.Unix(0, 0), telegraf.Gauge),

Comment thread plugins/inputs/mem/mem_test.go Outdated
Comment on lines +134 to +141
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),
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please use

Suggested change
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
@telegraf-tiger

Copy link
Copy Markdown
Contributor

Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip.
Downloads for additional architectures and packages are available below.

🥳 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 artifacts

Artifact URLs

. DEB . RPM . TAR . GZ . ZIP
amd64.deb aarch64.rpm darwin_amd64.tar.gz windows_amd64.zip
arm64.deb armel.rpm darwin_arm64.tar.gz windows_arm64.zip
armel.deb armv6hl.rpm freebsd_amd64.tar.gz windows_i386.zip
armhf.deb i386.rpm freebsd_armv7.tar.gz
i386.deb ppc64le.rpm freebsd_i386.tar.gz
mips.deb riscv64.rpm linux_amd64.tar.gz
mipsel.deb s390x.rpm linux_arm64.tar.gz
ppc64el.deb x86_64.rpm linux_armel.tar.gz
riscv64.deb linux_armhf.tar.gz
s390x.deb linux_i386.tar.gz
linux_mips.tar.gz
linux_mipsel.tar.gz
linux_ppc64le.tar.gz
linux_riscv64.tar.gz
linux_s390x.tar.gz

@srebhan srebhan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Thanks @pvlltvk!

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label Apr 22, 2026
@srebhan srebhan assigned skartikey and unassigned srebhan Apr 22, 2026

@skartikey skartikey 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.

@pvlltvk Thanks for the contribution!

@skartikey skartikey merged commit d378008 into influxdata:master Apr 24, 2026
27 checks passed
@github-actions github-actions Bot added this to the v1.39.0 milestone Apr 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[inputs.mem] Add percpu memory statistic

3 participants