Skip to content

Commit ee575f9

Browse files
committed
Add default metrics grouped_process_num_grouped ( Number of grouped )
1 parent 0de49ad commit ee575f9

File tree

10 files changed

+143
-42
lines changed

10 files changed

+143
-42
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: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -109,46 +108,49 @@ func (c *Cgroup) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[metric.M
109108
if cPath == "" {
110109
return nil
111110
}
112-
f, err := os.Open(filepath.Clean(filepath.Join(path, "cgroup.procs")))
113-
if err != nil {
114-
_ = f.Close()
115-
return nil
116-
}
117-
var (
118-
gproc *grouped_proc.GroupedProc
119-
ok bool
120-
)
121-
gproc, ok = gprocs.Load(cPath)
122-
if !ok {
123-
gproc = grouped_proc.NewGroupedProc(enabled)
124-
gprocs.Store(cPath, gproc)
125-
}
126-
gproc.Exists = true
127-
reader := bufio.NewReaderSize(f, 1028)
128-
for {
129-
line, _, err := reader.ReadLine()
130-
if err == io.EOF {
131-
break
132-
} else if err != nil {
133-
_ = f.Close()
134-
return err
135-
}
136-
pid, err := strconv.Atoi(string(line))
111+
{
112+
f, err := os.Open(filepath.Clean(filepath.Join(path, "cgroup.procs")))
137113
if err != nil {
138114
_ = f.Close()
139-
return err
115+
return nil
140116
}
141-
err = sem.Acquire(ctx, gproc.RequiredWeight)
142-
if err != nil {
143-
_ = f.Close()
144-
return err
117+
var (
118+
gproc *grouped_proc.GroupedProc
119+
ok bool
120+
)
121+
gproc, ok = gprocs.Load(cPath)
122+
if !ok {
123+
gproc = grouped_proc.NewGroupedProc(enabled)
124+
gprocs.Store(cPath, gproc)
125+
}
126+
gproc.Collect(cPath)
127+
gproc.Exists = true
128+
reader := bufio.NewReaderSize(f, 1028)
129+
for {
130+
line, _, err := reader.ReadLine()
131+
if err == io.EOF {
132+
break
133+
} else if err != nil {
134+
_ = f.Close()
135+
return err
136+
}
137+
pid, err := strconv.Atoi(string(line))
138+
if err != nil {
139+
_ = f.Close()
140+
return err
141+
}
142+
err = sem.Acquire(ctx, gproc.RequiredWeight)
143+
if err != nil {
144+
_ = f.Close()
145+
return err
146+
}
147+
wg.Add(1)
148+
go func(wg *sync.WaitGroup, pid int, gproc *grouped_proc.GroupedProc) {
149+
_ = gproc.AppendProcAndCollect(pid)
150+
sem.Release(gproc.RequiredWeight)
151+
wg.Done()
152+
}(wg, pid, gproc)
145153
}
146-
wg.Add(1)
147-
go func(wg *sync.WaitGroup, pid int, gproc *grouped_proc.GroupedProc) {
148-
_ = gproc.AppendProcAndCollect(pid)
149-
sem.Release(gproc.RequiredWeight)
150-
wg.Done()
151-
}(wg, pid, gproc)
152154
}
153155
return nil
154156
})

grouper/proc_status_name/proc_status_name.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func (g *ProcStatusName) Collect(gprocs *grouped_proc.GroupedProcs, enabled map[
7777
gproc = grouped_proc.NewGroupedProc(enabled)
7878
gprocs.Store(name, gproc)
7979
}
80+
gproc.Collect(name)
8081
gproc.Exists = true
8182
wg.Add(1)
8283
_ = 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)