From a516d4de4ad254ebbbccd1540eb509621b1cf682 Mon Sep 17 00:00:00 2001 From: Ben Kochie Date: Fri, 24 Jun 2022 17:15:31 +0200 Subject: [PATCH] Cleanup cgroups collector (#2414) * Correctly name collector file. * Fix cgroup summary type as gauge. * Use a boolean metric rather than a label for enabled. Signed-off-by: Ben Kochie --- README.md | 1 + collector/cgroups_linux.go | 18 +++++++++---- collector/fixtures/e2e-output.txt | 44 ++++++++++++++++++++----------- end-to-end-test.sh | 1 + 4 files changed, 44 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 675a43d7..0b7d643f 100644 --- a/README.md +++ b/README.md @@ -193,6 +193,7 @@ from debugfs. And example usage of this would be Name | Description | OS ---------|-------------|---- buddyinfo | Exposes statistics of memory fragments as reported by /proc/buddyinfo. | Linux +cgroups | A summary of the number of active and enabled cgroups | Linux devstat | Exposes device statistics | Dragonfly, FreeBSD drbd | Exposes Distributed Replicated Block Device statistics (to version 8.4) | Linux ethtool | Exposes network interface information and network driver statistics equivalent to `ethtool`, `ethtool -S`, and `ethtool -i`. | Linux diff --git a/collector/cgroups_linux.go b/collector/cgroups_linux.go index 80ae7da1..9f6d01a4 100644 --- a/collector/cgroups_linux.go +++ b/collector/cgroups_linux.go @@ -18,21 +18,23 @@ package collector import ( "fmt" - "strconv" "github.com/go-kit/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/procfs" ) +const cgroupsCollectorSubsystem = "cgroups" + type cgroupSummaryCollector struct { fs procfs.FS cgroups *prometheus.Desc + enabled *prometheus.Desc logger log.Logger } func init() { - registerCollector("cgroupSummary", defaultEnabled, NewCgroupSummaryCollector) + registerCollector(cgroupsCollectorSubsystem, defaultDisabled, NewCgroupSummaryCollector) } // NewCgroupSummaryCollector returns a new Collector exposing a summary of cgroups. @@ -44,9 +46,14 @@ func NewCgroupSummaryCollector(logger log.Logger) (Collector, error) { return &cgroupSummaryCollector{ fs: fs, cgroups: prometheus.NewDesc( - prometheus.BuildFQName(namespace, "", "cgroups_total"), + prometheus.BuildFQName(namespace, cgroupsCollectorSubsystem, "cgroups"), "Current cgroup number of the subsystem.", - []string{"subsys_name", "enabled"}, nil, + []string{"subsys_name"}, nil, + ), + enabled: prometheus.NewDesc( + prometheus.BuildFQName(namespace, cgroupsCollectorSubsystem, "enabled"), + "Current cgroup number of the subsystem.", + []string{"subsys_name"}, nil, ), logger: logger, }, nil @@ -59,7 +66,8 @@ func (c *cgroupSummaryCollector) Update(ch chan<- prometheus.Metric) error { return err } for _, cs := range cgroupSummarys { - ch <- prometheus.MustNewConstMetric(c.cgroups, prometheus.CounterValue, float64(cs.Cgroups), cs.SubsysName, strconv.Itoa(cs.Enabled)) + ch <- prometheus.MustNewConstMetric(c.cgroups, prometheus.GaugeValue, float64(cs.Cgroups), cs.SubsysName) + ch <- prometheus.MustNewConstMetric(c.enabled, prometheus.GaugeValue, float64(cs.Enabled), cs.SubsysName) } return nil } diff --git a/collector/fixtures/e2e-output.txt b/collector/fixtures/e2e-output.txt index 09523012..1095c4b0 100644 --- a/collector/fixtures/e2e-output.txt +++ b/collector/fixtures/e2e-output.txt @@ -296,20 +296,34 @@ node_buddyinfo_blocks{node="0",size="8",zone="Normal"} 0 node_buddyinfo_blocks{node="0",size="9",zone="DMA"} 1 node_buddyinfo_blocks{node="0",size="9",zone="DMA32"} 0 node_buddyinfo_blocks{node="0",size="9",zone="Normal"} 0 -# HELP node_cgroups_total Current cgroup number of the subsystem. -# TYPE node_cgroups_total counter -node_cgroups_total{enabled="1",subsys_name="blkio"} 170 -node_cgroups_total{enabled="1",subsys_name="cpu"} 172 -node_cgroups_total{enabled="1",subsys_name="cpuacct"} 172 -node_cgroups_total{enabled="1",subsys_name="cpuset"} 47 -node_cgroups_total{enabled="1",subsys_name="devices"} 170 -node_cgroups_total{enabled="1",subsys_name="freezer"} 47 -node_cgroups_total{enabled="1",subsys_name="hugetlb"} 47 -node_cgroups_total{enabled="1",subsys_name="memory"} 234 -node_cgroups_total{enabled="1",subsys_name="net_cls"} 47 -node_cgroups_total{enabled="1",subsys_name="perf_event"} 47 -node_cgroups_total{enabled="1",subsys_name="pids"} 170 -node_cgroups_total{enabled="1",subsys_name="rdma"} 1 +# HELP node_cgroups_cgroups Current cgroup number of the subsystem. +# TYPE node_cgroups_cgroups gauge +node_cgroups_cgroups{subsys_name="blkio"} 170 +node_cgroups_cgroups{subsys_name="cpu"} 172 +node_cgroups_cgroups{subsys_name="cpuacct"} 172 +node_cgroups_cgroups{subsys_name="cpuset"} 47 +node_cgroups_cgroups{subsys_name="devices"} 170 +node_cgroups_cgroups{subsys_name="freezer"} 47 +node_cgroups_cgroups{subsys_name="hugetlb"} 47 +node_cgroups_cgroups{subsys_name="memory"} 234 +node_cgroups_cgroups{subsys_name="net_cls"} 47 +node_cgroups_cgroups{subsys_name="perf_event"} 47 +node_cgroups_cgroups{subsys_name="pids"} 170 +node_cgroups_cgroups{subsys_name="rdma"} 1 +# HELP node_cgroups_enabled Current cgroup number of the subsystem. +# TYPE node_cgroups_enabled gauge +node_cgroups_enabled{subsys_name="blkio"} 1 +node_cgroups_enabled{subsys_name="cpu"} 1 +node_cgroups_enabled{subsys_name="cpuacct"} 1 +node_cgroups_enabled{subsys_name="cpuset"} 1 +node_cgroups_enabled{subsys_name="devices"} 1 +node_cgroups_enabled{subsys_name="freezer"} 1 +node_cgroups_enabled{subsys_name="hugetlb"} 1 +node_cgroups_enabled{subsys_name="memory"} 1 +node_cgroups_enabled{subsys_name="net_cls"} 1 +node_cgroups_enabled{subsys_name="perf_event"} 1 +node_cgroups_enabled{subsys_name="pids"} 1 +node_cgroups_enabled{subsys_name="rdma"} 1 # HELP node_context_switches_total Total number of context switches. # TYPE node_context_switches_total counter node_context_switches_total 3.8014093e+07 @@ -3045,7 +3059,7 @@ node_scrape_collector_success{collector="bcache"} 1 node_scrape_collector_success{collector="bonding"} 1 node_scrape_collector_success{collector="btrfs"} 1 node_scrape_collector_success{collector="buddyinfo"} 1 -node_scrape_collector_success{collector="cgroupSummary"} 1 +node_scrape_collector_success{collector="cgroups"} 1 node_scrape_collector_success{collector="conntrack"} 1 node_scrape_collector_success{collector="cpu"} 1 node_scrape_collector_success{collector="cpufreq"} 1 diff --git a/end-to-end-test.sh b/end-to-end-test.sh index 8c1060e3..0a1b730f 100755 --- a/end-to-end-test.sh +++ b/end-to-end-test.sh @@ -8,6 +8,7 @@ enabled_collectors=$(cat << COLLECTORS btrfs buddyinfo conntrack + cgroups cpu cpufreq diskstats