-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
feat!: new meminfo_procfs collector, deprecate meminfo collector #3043
Closed
Closed
Changes from all commits
Commits
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 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
This file contains 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
This file contains 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,77 @@ | ||
// Copyright 2024 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build linux && !nomeminfo_procfs | ||
// +build linux,!nomeminfo_procfs | ||
|
||
package collector | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/go-kit/log/level" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/procfs" | ||
) | ||
|
||
const ( | ||
memInfoProcfsSubsystem = "memory" | ||
) | ||
|
||
var ( | ||
memoryBytesDesc = prometheus.NewDesc( | ||
prometheus.BuildFQName(namespace, memInfoProcfsSubsystem, "bytes"), | ||
"Value in bytes for the labeled field in /proc/meminfo.", | ||
[]string{"field"}, nil, | ||
) | ||
) | ||
|
||
type meminfoProcfsCollector struct { | ||
memoryBytesDesc *prometheus.Desc | ||
fs procfs.FS | ||
logger log.Logger | ||
} | ||
|
||
func init() { | ||
registerCollector("meminfo_procfs", defaultDisabled, NewMeminfoProcfsCollector) | ||
} | ||
|
||
// NewMeminfoProcfsCollector returns a new Collector exposing memory stats. | ||
func NewMeminfoProcfsCollector(logger log.Logger) (Collector, error) { | ||
fs, err := procfs.NewFS(*procPath) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to open procfs: %w", err) | ||
} | ||
|
||
return &meminfoProcfsCollector{ | ||
fs: fs, | ||
logger: logger, | ||
memoryBytesDesc: memoryBytesDesc, | ||
}, nil | ||
} | ||
|
||
// Update calls (*meminfoProcfsCollector).getMemInfo to get the platform specific | ||
// memory metrics. | ||
func (c *meminfoProcfsCollector) Update(ch chan<- prometheus.Metric) error { | ||
memInfo, err := c.getMemInfo() | ||
if err != nil { | ||
return fmt.Errorf("couldn't get meminfo: %w", err) | ||
} | ||
|
||
level.Debug(c.logger).Log("msg", "Set node_mem", "memInfoProcfs", fmt.Sprintf("%v", memInfo)) | ||
for k, v := range memInfo { | ||
ch <- prometheus.MustNewConstMetric(c.memoryBytesDesc, prometheus.GaugeValue, v, k) | ||
} | ||
return nil | ||
} |
This file contains 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,76 @@ | ||
// Copyright 2024 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !nomeminfo_procfs | ||
// +build !nomeminfo_procfs | ||
|
||
package collector | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
func (c *meminfoProcfsCollector) getMemInfo() (map[string]float64, error) { | ||
meminfo, err := c.fs.Meminfo() | ||
if err != nil { | ||
return nil, fmt.Errorf("Failed to get memory info: %s", err) | ||
} | ||
|
||
return map[string]float64{ | ||
"MemTotal": uint64PtrToFloat(meminfo.MemTotalBytes), | ||
"MemFree": uint64PtrToFloat(meminfo.MemFreeBytes), | ||
"MemAvailable": uint64PtrToFloat(meminfo.MemAvailableBytes), | ||
"Buffers": uint64PtrToFloat(meminfo.BuffersBytes), | ||
"Cached": uint64PtrToFloat(meminfo.CachedBytes), | ||
"SwapCached": uint64PtrToFloat(meminfo.SwapCachedBytes), | ||
"Active": uint64PtrToFloat(meminfo.ActiveBytes), | ||
"Inactive": uint64PtrToFloat(meminfo.InactiveBytes), | ||
"ActiveAnon": uint64PtrToFloat(meminfo.ActiveAnonBytes), | ||
"InactiveAnon": uint64PtrToFloat(meminfo.InactiveAnonBytes), | ||
"ActiveFile": uint64PtrToFloat(meminfo.ActiveFileBytes), | ||
"InactiveFile": uint64PtrToFloat(meminfo.InactiveFileBytes), | ||
"Unevictable": uint64PtrToFloat(meminfo.UnevictableBytes), | ||
"Mlocked": uint64PtrToFloat(meminfo.MlockedBytes), | ||
"SwapTotal": uint64PtrToFloat(meminfo.SwapTotalBytes), | ||
"SwapFree": uint64PtrToFloat(meminfo.SwapFreeBytes), | ||
"Dirty": uint64PtrToFloat(meminfo.DirtyBytes), | ||
"Writeback": uint64PtrToFloat(meminfo.WritebackBytes), | ||
"AnonPages": uint64PtrToFloat(meminfo.AnonPagesBytes), | ||
"Mapped": uint64PtrToFloat(meminfo.MappedBytes), | ||
"Shmem": uint64PtrToFloat(meminfo.ShmemBytes), | ||
"Slab": uint64PtrToFloat(meminfo.SlabBytes), | ||
"SReclaimable": uint64PtrToFloat(meminfo.SReclaimableBytes), | ||
"SUnreclaim": uint64PtrToFloat(meminfo.SUnreclaimBytes), | ||
"KernelStack": uint64PtrToFloat(meminfo.KernelStackBytes), | ||
"PageTables": uint64PtrToFloat(meminfo.PageTablesBytes), | ||
"NFSUnstable": uint64PtrToFloat(meminfo.NFSUnstableBytes), | ||
"Bounce": uint64PtrToFloat(meminfo.BounceBytes), | ||
"WritebackTmp": uint64PtrToFloat(meminfo.WritebackTmpBytes), | ||
"CommitLimit": uint64PtrToFloat(meminfo.CommitLimitBytes), | ||
"CommittedAS": uint64PtrToFloat(meminfo.CommittedASBytes), | ||
"VmallocTotal": uint64PtrToFloat(meminfo.VmallocTotalBytes), | ||
"VmallocUsed": uint64PtrToFloat(meminfo.VmallocUsedBytes), | ||
"VmallocChunk": uint64PtrToFloat(meminfo.VmallocChunkBytes), | ||
"Percpu": uint64PtrToFloat(meminfo.PercpuBytes), | ||
"HardwareCorrupted": uint64PtrToFloat(meminfo.HardwareCorruptedBytes), | ||
"AnonHugePages": uint64PtrToFloat(meminfo.AnonHugePagesBytes), | ||
"ShmemHugePages": uint64PtrToFloat(meminfo.ShmemHugePagesBytes), | ||
"ShmemPmdMapped": uint64PtrToFloat(meminfo.ShmemPmdMappedBytes), | ||
"CmaTotal": uint64PtrToFloat(meminfo.CmaTotalBytes), | ||
"CmaFree": uint64PtrToFloat(meminfo.CmaFreeBytes), | ||
"Hugepagesize": uint64PtrToFloat(meminfo.HugepagesizeBytes), | ||
"DirectMap4k": uint64PtrToFloat(meminfo.DirectMap4kBytes), | ||
"DirectMap2M": uint64PtrToFloat(meminfo.DirectMap2MBytes), | ||
"DirectMap1G": uint64PtrToFloat(meminfo.DirectMap1GBytes), | ||
}, nil | ||
} |
This file contains 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,131 @@ | ||
// Copyright 2024 The Prometheus Authors | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//go:build !nomeminfo_procfs | ||
// +build !nomeminfo_procfs | ||
|
||
package collector | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-kit/log" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"github.com/prometheus/client_golang/prometheus/testutil" | ||
) | ||
|
||
const ( | ||
testMetrics = `# HELP node_memory_bytes Value in bytes for the labeled field in /proc/meminfo. | ||
# TYPE node_memory_bytes gauge | ||
node_memory_bytes{field="ActiveAnon"} 2.068484096e+09 | ||
node_memory_bytes{field="Active"} 2.287017984e+09 | ||
node_memory_bytes{field="ActiveFile"} 2.18533888e+08 | ||
node_memory_bytes{field="AnonHugePages"} 0 | ||
node_memory_bytes{field="AnonPages"} 2.298032128e+09 | ||
node_memory_bytes{field="Bounce"} 0 | ||
node_memory_bytes{field="Buffers"} 2.256896e+07 | ||
node_memory_bytes{field="Cached"} 9.53229312e+08 | ||
node_memory_bytes{field="CmaFree"} 0 | ||
node_memory_bytes{field="CmaTotal"} 0 | ||
node_memory_bytes{field="CommitLimit"} 6.210940928e+09 | ||
node_memory_bytes{field="CommittedAS"} 8.023486464e+09 | ||
node_memory_bytes{field="DirectMap1G"} 0 | ||
node_memory_bytes{field="DirectMap2M"} 3.787456512e+09 | ||
node_memory_bytes{field="DirectMap4k"} 1.9011584e+08 | ||
node_memory_bytes{field="Dirty"} 1.077248e+06 | ||
node_memory_bytes{field="HardwareCorrupted"} 0 | ||
node_memory_bytes{field="Hugepagesize"} 2.097152e+06 | ||
node_memory_bytes{field="InactiveAnon"} 9.04245248e+08 | ||
node_memory_bytes{field="Inactive"} 1.053417472e+09 | ||
node_memory_bytes{field="InactiveFile"} 1.49172224e+08 | ||
node_memory_bytes{field="KernelStack"} 5.9392e+06 | ||
node_memory_bytes{field="Mapped"} 2.4496128e+08 | ||
node_memory_bytes{field="MemAvailable"} 0 | ||
node_memory_bytes{field="MemFree"} 2.30883328e+08 | ||
node_memory_bytes{field="MemTotal"} 3.831959552e+09 | ||
node_memory_bytes{field="Mlocked"} 32768 | ||
node_memory_bytes{field="NFSUnstable"} 0 | ||
node_memory_bytes{field="PageTables"} 7.7017088e+07 | ||
node_memory_bytes{field="Percpu"} 0 | ||
node_memory_bytes{field="SReclaimable"} 4.5846528e+07 | ||
node_memory_bytes{field="SUnreclaim"} 5.545984e+07 | ||
node_memory_bytes{field="Shmem"} 6.0809216e+08 | ||
node_memory_bytes{field="ShmemHugePages"} 0 | ||
node_memory_bytes{field="ShmemPmdMapped"} 0 | ||
node_memory_bytes{field="Slab"} 1.01306368e+08 | ||
node_memory_bytes{field="SwapCached"} 1.97124096e+08 | ||
node_memory_bytes{field="SwapFree"} 3.23108864e+09 | ||
node_memory_bytes{field="SwapTotal"} 4.2949632e+09 | ||
node_memory_bytes{field="Unevictable"} 32768 | ||
node_memory_bytes{field="VmallocChunk"} 3.5183963009024e+13 | ||
node_memory_bytes{field="VmallocTotal"} 3.5184372087808e+13 | ||
node_memory_bytes{field="VmallocUsed"} 3.6130816e+08 | ||
node_memory_bytes{field="Writeback"} 0 | ||
node_memory_bytes{field="WritebackTmp"} 0 | ||
` | ||
) | ||
|
||
type testMeminfoProcfsCollector struct { | ||
mc Collector | ||
} | ||
|
||
func (c testMeminfoProcfsCollector) Collect(ch chan<- prometheus.Metric) { | ||
c.mc.Update(ch) | ||
} | ||
|
||
func (c testMeminfoProcfsCollector) Describe(ch chan<- *prometheus.Desc) { | ||
prometheus.DescribeByCollect(c, ch) | ||
} | ||
|
||
func NewTestMeminfoProcfsCollector(logger log.Logger) (prometheus.Collector, error) { | ||
mc, err := NewMeminfoProcfsCollector(logger) | ||
if err != nil { | ||
return testMeminfoProcfsCollector{}, err | ||
} | ||
return testMeminfoProcfsCollector{ | ||
mc: mc, | ||
}, err | ||
} | ||
|
||
func TestMemInfoProcfs(t *testing.T) { | ||
*procPath = "fixtures/proc" | ||
logger := log.NewLogfmtLogger(os.Stderr) | ||
|
||
collector, err := NewMeminfoProcfsCollector(logger) | ||
if err != nil { | ||
panic(err) | ||
} | ||
c, err := NewTestMeminfoProcfsCollector(logger) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
reg := prometheus.NewRegistry() | ||
reg.MustRegister(c) | ||
|
||
sink := make(chan prometheus.Metric) | ||
go func() { | ||
err = collector.Update(sink) | ||
if err != nil { | ||
panic(fmt.Errorf("failed to update collector: %s", err)) | ||
} | ||
close(sink) | ||
}() | ||
|
||
err = testutil.GatherAndCompare(reg, strings.NewReader(testMetrics)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
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.
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.
Currently, this results in a panic:
This definitely achieves the goal of "don't run the node exporter if both collectors are enabled", but is certainly not a graceful exit, either.
Is there a better place to check and ensure both collectors aren't enabled at the same time? I had considered doing so in
registerCollector()
, but it felt awkward trying to fatal out from there. I had also considered checking in theinit()
funcs of both collectors when they try to do their self registration to only register if $theOther isn't registered yet, but I wasn't sure about any ordering that may/may not exist in collector registration and didn't want to cause a race between the 2 to see which would successfully register.