Skip to content

Commit d30446a

Browse files
authored
Merge pull request #28 from k1LoW/group_count
Add default metrics `grouped_process_num_grouped` ( Number of grouped )
2 parents 16485e0 + b4f406c commit d30446a

File tree

10 files changed

+117
-11
lines changed

10 files changed

+117
-11
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ For example, by setting `--group.exclude='user.\slice'`, the exporter excludes t
3232
| Name | Type | Description |
3333
| --- | --- | --- |
3434
| grouped_process_num_procs | Gauge | Number of processes in the group |
35+
| grouped_process_num_grouped | Gauge | Number of grouped |
3536

3637
### Grouped /proc/[PID]/stat ( `--collector.stat` )
3738

grouped_proc/grouped_proc.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,20 @@ func NewGroupedProc(enabled map[metric.MetricKey]bool) *GroupedProc {
4040
return &g
4141
}
4242

43+
func (g *GroupedProc) Collect(key string) error {
44+
for _, k := range metric.MetricKeys {
45+
if g.Enabled[k] {
46+
g.Lock()
47+
err := g.Metrics[k].Collect(key)
48+
g.Unlock()
49+
if err != nil {
50+
return err
51+
}
52+
}
53+
}
54+
return nil
55+
}
56+
4357
func (g *GroupedProc) AppendProcAndCollect(pid int) error {
4458
fs, err := procfs.NewFS(g.ProcMountPoint)
4559
if err != nil {

grouper/cgroup/cgroup.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ var DefaultSubsystems = []string{
3838
type Cgroup struct {
3939
fsPath string
4040
subsystems []string
41-
nRe *regexp.Regexp
42-
eRe *regexp.Regexp
41+
nRe *regexp.Regexp // normalize regexp
42+
eRe *regexp.Regexp // exclude regexp
4343
}
4444

4545
func (c *Cgroup) Name() string {
@@ -86,8 +86,7 @@ func (c *Cgroup) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[metric.M
8686
if f == nil {
8787
return nil
8888
}
89-
err = sem.Acquire(ctx, 2)
90-
if err != nil {
89+
if err := sem.Acquire(ctx, 2); err != nil {
9190
return err
9291
}
9392
defer sem.Release(2)
@@ -106,7 +105,10 @@ func (c *Cgroup) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[metric.M
106105
cPath = matches[1]
107106
}
108107
}
109-
if cPath != "" {
108+
if cPath == "" {
109+
return nil
110+
}
111+
{
110112
f, err := os.Open(filepath.Clean(filepath.Join(path, "cgroup.procs")))
111113
if err != nil {
112114
_ = f.Close()
@@ -121,6 +123,9 @@ func (c *Cgroup) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[metric.M
121123
gproc = grouped_proc.NewGroupedProc(enabled)
122124
gprocs.Store(cPath, gproc)
123125
}
126+
if err := gproc.Collect(cPath); err != nil {
127+
return err
128+
}
124129
gproc.Exists = true
125130
reader := bufio.NewReaderSize(f, 1028)
126131
for {

grouper/proc_status_name/proc_status_name.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414
)
1515

1616
type ProcStatusName struct {
17-
nRe *regexp.Regexp
18-
eRe *regexp.Regexp
17+
nRe *regexp.Regexp // normalize regexp
18+
eRe *regexp.Regexp // exclude regexp
1919
procMountPoint string
2020
}
2121

@@ -77,6 +77,9 @@ func (g *ProcStatusName) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[
7777
gproc = grouped_proc.NewGroupedProc(enabled)
7878
gprocs.Store(name, gproc)
7979
}
80+
if err := gproc.Collect(name); err != nil {
81+
return err
82+
}
8083
gproc.Exists = true
8184
wg.Add(1)
8285
_ = sem.Acquire(ctx, gproc.RequiredWeight)

metric/metric.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
type Metric interface {
99
Describe() map[string]*prometheus.Desc
1010
String() string
11+
Collect(k string) error
1112
CollectFromProc(proc procfs.Proc) error
1213
PushCollected(ch chan<- prometheus.Metric, descs map[string]*prometheus.Desc, grouper string, group string) error
1314
RequiredWeight() int64
@@ -16,14 +17,16 @@ type Metric interface {
1617
type MetricKey string
1718

1819
var (
19-
ProcProcs MetricKey = "proc_procs"
20-
ProcStat MetricKey = "proc_stat"
21-
ProcIO MetricKey = "proc_io"
22-
ProcStatus MetricKey = "proc_status"
20+
ProcProcs MetricKey = "proc_procs"
21+
ProcGrouped MetricKey = "proc_grouped"
22+
ProcStat MetricKey = "proc_stat"
23+
ProcIO MetricKey = "proc_io"
24+
ProcStatus MetricKey = "proc_status"
2325
)
2426

2527
var MetricKeys = []MetricKey{
2628
ProcProcs,
29+
ProcGrouped,
2730
ProcStat,
2831
ProcIO,
2932
ProcStatus,
@@ -35,6 +38,9 @@ func AvairableMetrics() map[MetricKey]Metric {
3538
// procs
3639
metrics[ProcProcs] = NewProcProcsMetric()
3740

41+
// grouped
42+
metrics[ProcGrouped] = NewProcGroupedMetric()
43+
3844
// stat
3945
metrics[ProcStat] = NewProcStatMetric()
4046

@@ -53,5 +59,6 @@ func DefaultEnabledMetrics() map[MetricKey]bool {
5359
enabled[k] = false
5460
}
5561
enabled[ProcProcs] = true
62+
enabled[ProcGrouped] = true
5663
return enabled
5764
}

metric/proc_grouped.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package metric
2+
3+
import (
4+
"sync"
5+
6+
"github.com/prometheus/client_golang/prometheus"
7+
"github.com/prometheus/procfs"
8+
)
9+
10+
// ProcGroupedMetric is metric
11+
type ProcGroupedMetric struct {
12+
sync.Mutex
13+
count float64
14+
}
15+
16+
func (m *ProcGroupedMetric) Describe() map[string]*prometheus.Desc {
17+
descs := map[string]*prometheus.Desc{
18+
"grouped_process_num_grouped": prometheus.NewDesc(
19+
"grouped_process_num_grouped",
20+
"Number of grouped",
21+
[]string{"grouper", "group"}, nil,
22+
),
23+
}
24+
return descs
25+
}
26+
27+
func (m *ProcGroupedMetric) String() string {
28+
return "grouped_count"
29+
}
30+
31+
func (m *ProcGroupedMetric) Collect(k string) error {
32+
m.Lock()
33+
m.count = m.count + 1
34+
m.Unlock()
35+
return nil
36+
}
37+
38+
func (m *ProcGroupedMetric) CollectFromProc(proc procfs.Proc) error {
39+
return nil
40+
}
41+
42+
func (m *ProcGroupedMetric) PushCollected(ch chan<- prometheus.Metric, descs map[string]*prometheus.Desc, grouper string, group string) error {
43+
m.Lock()
44+
if d, ok := descs["grouped_process_num_grouped"]; ok {
45+
ch <- prometheus.MustNewConstMetric(d, prometheus.GaugeValue, m.count, grouper, group)
46+
}
47+
m.count = 0 // clear
48+
m.Unlock()
49+
return nil
50+
}
51+
52+
func (m *ProcGroupedMetric) RequiredWeight() int64 {
53+
return 0
54+
}
55+
56+
func NewProcGroupedMetric() *ProcGroupedMetric {
57+
return &ProcGroupedMetric{
58+
count: 0,
59+
}
60+
}

metric/proc_io.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func (m *ProcIOMetric) String() string {
5858
return "proc_io"
5959
}
6060

61+
func (m *ProcIOMetric) Collect(k string) error {
62+
return nil
63+
}
64+
6165
func (m *ProcIOMetric) CollectFromProc(proc procfs.Proc) error {
6266
pio, err := proc.IO()
6367
if err != nil {

metric/proc_procs.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func (m *ProcProcsMetric) String() string {
2828
return "proc_count"
2929
}
3030

31+
func (m *ProcProcsMetric) Collect(k string) error {
32+
return nil
33+
}
34+
3135
func (m *ProcProcsMetric) CollectFromProc(proc procfs.Proc) error {
3236
m.Lock()
3337
m.metrics[proc.PID] = struct{}{}

metric/proc_stat.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ func (m *ProcStatMetric) String() string {
8787
return "proc_stat"
8888
}
8989

90+
func (m *ProcStatMetric) Collect(k string) error {
91+
return nil
92+
}
93+
9094
func (m *ProcStatMetric) CollectFromProc(proc procfs.Proc) error {
9195
stat, err := proc.Stat()
9296
if err != nil {

metric/proc_status.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ func (m *ProcStatusMetric) String() string {
118118
return "proc_status"
119119
}
120120

121+
func (m *ProcStatusMetric) Collect(k string) error {
122+
return nil
123+
}
124+
121125
func (m *ProcStatusMetric) CollectFromProc(proc procfs.Proc) error {
122126
status, err := proc.NewStatus()
123127
if err != nil {

0 commit comments

Comments
 (0)