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

Add cgroup summary collector #2408

Merged
merged 9 commits into from
Jun 24, 2022
Merged
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
65 changes: 65 additions & 0 deletions collector/cgroups_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2022 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 !nostat
// +build !nostat

package collector

import (
"fmt"
"strconv"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/procfs"
)

type cgroupSummaryCollector struct {
fs procfs.FS
cgroups *prometheus.Desc
logger log.Logger
}

func init() {
registerCollector("cgroupSummary", defaultEnabled, NewCgroupSummaryCollector)
}

// NewCgroupSummaryCollector returns a new Collector exposing a summary of cgroups.
func NewCgroupSummaryCollector(logger log.Logger) (Collector, error) {
fs, err := procfs.NewFS(*procPath)
if err != nil {
return nil, fmt.Errorf("failed to open procfs: %w", err)
}
return &cgroupSummaryCollector{
fs: fs,
cgroups: prometheus.NewDesc(
prometheus.BuildFQName(namespace, "", "cgroups_total"),
"Current cgroup number of the subsystem.",
[]string{"subsys_name", "enabled"}, nil,
),
logger: logger,
}, nil
}

// Update implements Collector and exposes cgroup statistics.
func (c *cgroupSummaryCollector) Update(ch chan<- prometheus.Metric) error {
cgroupSummarys, err := c.fs.CgroupSummarys()
if err != nil {
return err
}
for _, cs := range cgroupSummarys {
ch <- prometheus.MustNewConstMetric(c.cgroups, prometheus.CounterValue, float64(cs.Cgroups), cs.SubsysName, strconv.Itoa(cs.Enabled))
}
return nil
}
15 changes: 15 additions & 0 deletions collector/fixtures/e2e-output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,20 @@ 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_context_switches_total Total number of context switches.
# TYPE node_context_switches_total counter
node_context_switches_total 3.8014093e+07
Expand Down Expand Up @@ -3031,6 +3045,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="conntrack"} 1
node_scrape_collector_success{collector="cpu"} 1
node_scrape_collector_success{collector="cpufreq"} 1
Expand Down
13 changes: 13 additions & 0 deletions collector/fixtures/proc/cgroups
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#subsys_name hierarchy num_cgroups enabled
cpuset 5 47 1
cpu 3 172 1
cpuacct 3 172 1
blkio 6 170 1
memory 7 234 1
devices 11 170 1
freezer 9 47 1
net_cls 2 47 1
perf_event 8 47 1
hugetlb 12 47 1
pids 10 170 1
rdma 4 1 1
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ require (
github.com/prometheus/client_model v0.2.0
github.com/prometheus/common v0.33.0
github.com/prometheus/exporter-toolkit v0.7.1
github.com/prometheus/procfs v0.7.4-0.20211011103944-1a7a2bd3279f
github.com/prometheus/procfs v0.7.4-0.20211209105546-0f8a320e1e1f
github.com/safchain/ethtool v0.2.0
github.com/siebenmann/go-kstat v0.0.0-20210513183136-173c9b0a9973 // indirect
github.com/soundcloud/go-runit v0.0.0-20150630195641-06ad41a06c4a
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.4-0.20211011103944-1a7a2bd3279f h1:ncXqc93eJV1Ncr3f6GA3MrIDNkNHvcPonRC2QgZaVkQ=
github.com/prometheus/procfs v0.7.4-0.20211011103944-1a7a2bd3279f/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/prometheus/procfs v0.7.4-0.20211209105546-0f8a320e1e1f h1:tg+5gOV1AkhTfWNi94UA5rvn+Mat/thyB2WASYAzPZo=
github.com/prometheus/procfs v0.7.4-0.20211209105546-0f8a320e1e1f/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBOAvL+k=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
Expand Down