Add cgroup summary collector (#2408)

* add cgroups summary collector

Signed-off-by: biello <bellusa@qq.com>
Co-authored-by: bielu <bielu@zuoyebang.com>
This commit is contained in:
Kobe Biello 2022-06-24 18:05:13 +08:00 committed by GitHub
parent 279ac3ada2
commit 45c75f1dbc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 96 additions and 3 deletions

View File

@ -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
}

View File

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

View File

@ -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
go.mod
View File

@ -20,7 +20,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/soundcloud/go-runit v0.0.0-20150630195641-06ad41a06c4a
golang.org/x/sys v0.0.0-20220330033206-e17cdc41300f

4
go.sum
View File

@ -235,8 +235,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=