Skip to content
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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ func NewNodeCollector(logger log.Logger, filters ...string) (*NodeCollector, err
collectors := make(map[string]Collector)
initiatedCollectorsMtx.Lock()
defer initiatedCollectorsMtx.Unlock()

// The meminfo collector is being deprecated in favor of the new
// meminfo_procfs collector. They are mutually exclusive and should not
// be enabled at the time same.
micEnabled, micExist := collectorState["meminfo"]
mipcEnabled, mipcExist := collectorState["meminfo_procfs"]
if (micExist && mipcExist) && (*micEnabled && *mipcEnabled) {
return nil, fmt.Errorf("'meminfo' and 'meminfo_procfs' collectors are mutually exclusive, please disable one")
}

Comment on lines +122 to +131
Copy link
Contributor Author

@tjhop tjhop Jun 8, 2024

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:

~/go/src/github.com/prometheus/node_exporter (feat/procfslib-memory-collector [ U ]) -> ./node_exporter --collector.meminfo_procfs --log.level="debug" --web.listen-address ":9101"
ts=2024-06-08T18:12:51.917Z caller=node_exporter.go:193 level=info msg="Starting node_exporter" version="(version=1.8.1, branch=feat/procfslib-memory-collector, revision=1bab3570959f98b7e5b833d94c7024c7658c63aa)"
ts=2024-06-08T18:12:51.917Z caller=node_exporter.go:194 level=info msg="Build context" build_context="(go=go1.22.3, platform=linux/amd64, user=tjhop@contraband, date=20240608-18:05:10, tags=unknown)"
ts=2024-06-08T18:12:51.917Z caller=node_exporter.go:199 level=debug msg="Go MAXPROCS" procs=1
panic: Couldn't create metrics handler: couldn't create collector: 'meminfo' and 'meminfo_procfs' collectors are mutually exclusive, please disable one

goroutine 1 [running]:
main.newHandler(0x1, 0x28, {0xcf2740, 0xc0000d0440})
        /home/tjhop/go/src/github.com/prometheus/node_exporter/node_exporter.go:69 +0x2ab
main.main()
        /home/tjhop/go/src/github.com/prometheus/node_exporter/node_exporter.go:201 +0x128d

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 the init() 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.

for key, enabled := range collectorState {
if !*enabled || (len(f) > 0 && !f[key]) {
continue
Expand Down Expand Up @@ -244,3 +254,11 @@ func pushMetric(ch chan<- prometheus.Metric, fieldDesc *prometheus.Desc, name st

ch <- prometheus.MustNewConstMetric(fieldDesc, valueType, fVal, labelValues...)
}

func uint64PtrToFloat(u *uint64) float64 {
if u == nil {
return float64(0)
}

return float64(*u)
}
2 changes: 2 additions & 0 deletions collector/meminfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func init() {

// NewMeminfoCollector returns a new Collector exposing memory stats.
func NewMeminfoCollector(logger log.Logger) (Collector, error) {
level.Warn(logger).Log("msg", "This collector is deprecated and will be removed in the next major version release.")

return &meminfoCollector{logger}, nil
}

Expand Down
77 changes: 77 additions & 0 deletions collector/meminfo_procfs.go
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
}
76 changes: 76 additions & 0 deletions collector/meminfo_procfs_linux.go
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
}
131 changes: 131 additions & 0 deletions collector/meminfo_procfs_linux_test.go
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)
}
}