Skip to content

Commit 971836e

Browse files
committed
Add --metric.desc to filter metric descriptor
1 parent 7ef2e24 commit 971836e

File tree

6 files changed

+112
-56
lines changed

6 files changed

+112
-56
lines changed

cmd/root.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,16 @@ const (
4949
)
5050

5151
var (
52-
address string
53-
endpoint string
54-
groupType string
55-
nReStr string
56-
eReStr string
57-
collectStat bool
58-
collectIO bool
59-
collectStatus bool
60-
subsystems []string
52+
address string
53+
endpoint string
54+
groupType string
55+
nReStr string
56+
eReStr string
57+
collectStat bool
58+
collectIO bool
59+
collectStatus bool
60+
subsystems []string
61+
enableMetricDescName string
6162

6263
format string
6364
level string
@@ -138,6 +139,9 @@ func runRoot(args []string, address, endpoint, groupType, nReStr, eReStr string,
138139
collector.EnableMetric(metric.ProcStatus)
139140
log.Infoln("Enable collecting /proc/[PID]/status.")
140141
}
142+
if err := collector.SetEnableMetricDescNameRegexp(enableMetricDescName); err != nil {
143+
return 1, err
144+
}
141145

142146
r := prometheus.NewRegistry()
143147
r.MustRegister(promver.NewCollector("grouped_process_collector"))
@@ -187,6 +191,7 @@ func init() {
187191
rootCmd.Flags().BoolVarP(&collectIO, "collector.io", "", false, "Enable collecting /proc/[PID]/io.")
188192
rootCmd.Flags().BoolVarP(&collectStatus, "collector.status", "", false, "Enable collecting /proc/[PID]/status.")
189193
rootCmd.Flags().StringArrayVarP(&subsystems, "cgroup.subsystem", "", []string{}, fmt.Sprintf("Cgroup subsystem to scan. (default %s)", cgroup.DefaultSubsystems))
194+
rootCmd.Flags().StringVarP(&enableMetricDescName, "metric.desc", "", ".+", "Regexp for enable metric descriptor.")
190195

191196
// copy from https://github.com/prometheus/common/blob/master/log/log.go#L57
192197
rootCmd.Flags().StringVarP(&level, "log.level", "", logrus.New().Level.String(), "Only log messages with the given severity or above. Valid levels: [debug, info, warn, error, fatal]")

collector/collector.go

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package collector
22

33
import (
44
"os"
5+
"regexp"
56
"runtime"
67
"sync"
78

@@ -18,21 +19,24 @@ const openFileBuffer = 50
1819

1920
type GroupedProcCollector struct {
2021
sync.Mutex
21-
GroupedProcs *grouped_proc.GroupedProcs
22-
Metrics map[metric.MetricKey]metric.Metric
23-
Enabled map[metric.MetricKey]bool
24-
Grouper grouper.Grouper
25-
descs map[string]*prometheus.Desc
26-
sem *semaphore.Weighted
22+
GroupedProcs *grouped_proc.GroupedProcs
23+
Metrics map[metric.MetricKey]metric.Metric
24+
Enabled map[metric.MetricKey]bool
25+
Grouper grouper.Grouper
26+
enableMetricDescNameRe *regexp.Regexp
27+
descs map[string]*prometheus.Desc
28+
sem *semaphore.Weighted
2729
}
2830

2931
func (c *GroupedProcCollector) Describe(ch chan<- *prometheus.Desc) {
3032
for _, key := range metric.MetricKeys {
3133
if c.Enabled[key] {
3234
descs := c.Metrics[key].Describe()
3335
for name, desc := range descs {
34-
c.descs[name] = desc
35-
ch <- desc
36+
if c.enableMetricDescNameRe.MatchString(name) {
37+
c.descs[name] = desc
38+
ch <- desc
39+
}
3640
}
3741
}
3842
}
@@ -74,6 +78,15 @@ func (c *GroupedProcCollector) DisableMetric(metric metric.MetricKey) {
7478
c.Enabled[metric] = false
7579
}
7680

81+
func (c *GroupedProcCollector) SetEnableMetricDescNameRegexp(s string) error {
82+
re, err := regexp.Compile(s)
83+
if err != nil {
84+
return err
85+
}
86+
c.enableMetricDescNameRe = re
87+
return nil
88+
}
89+
7790
// NewGroupedProcCollector
7891
func NewGroupedProcCollector(g grouper.Grouper) (*GroupedProcCollector, error) {
7992
openFileLimit, err := detectOpenFileLimit()

metric/proc_io.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,25 @@ func (m *ProcIOMetric) PushCollected(ch chan<- prometheus.Metric, descs map[stri
9292
}
9393
m.Unlock()
9494

95-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_io_r_char_total"], prometheus.CounterValue, rChar, grouper, group)
96-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_io_w_char_total"], prometheus.CounterValue, wChar, grouper, group)
97-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_io_sysc_r_total"], prometheus.CounterValue, syscR, grouper, group)
98-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_io_sysc_w_total"], prometheus.CounterValue, syscW, grouper, group)
99-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_io_read_bytes_total"], prometheus.CounterValue, readBytes, grouper, group)
100-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_io_write_bytes_total"], prometheus.CounterValue, writeBytes, grouper, group)
101-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_io_cancelled_write_bytes_total"], prometheus.CounterValue, cancelledWriteBytes, grouper, group)
95+
values := []struct {
96+
k string
97+
t prometheus.ValueType
98+
v float64
99+
}{
100+
{"grouped_process_io_r_char_total", prometheus.CounterValue, rChar},
101+
{"grouped_process_io_w_char_total", prometheus.CounterValue, wChar},
102+
{"grouped_process_io_sysc_r_total", prometheus.CounterValue, syscR},
103+
{"grouped_process_io_sysc_w_total", prometheus.CounterValue, syscW},
104+
{"grouped_process_io_read_bytes_total", prometheus.CounterValue, readBytes},
105+
{"grouped_process_io_write_bytes_total", prometheus.CounterValue, writeBytes},
106+
{"grouped_process_io_cancelled_write_bytes_total", prometheus.CounterValue, cancelledWriteBytes},
107+
}
108+
109+
for _, s := range values {
110+
if d, ok := descs[s.k]; ok {
111+
ch <- prometheus.MustNewConstMetric(d, s.t, s.v, grouper, group)
112+
}
113+
}
102114

103115
return nil
104116
}

metric/proc_procs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ func (m *ProcProcsMetric) CollectFromProc(proc procfs.Proc) error {
3737

3838
func (m *ProcProcsMetric) PushCollected(ch chan<- prometheus.Metric, descs map[string]*prometheus.Desc, grouper string, group string) error {
3939
m.Lock()
40-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_num_procs"], prometheus.GaugeValue, float64(len(m.metrics)), grouper, group)
40+
if d, ok := descs["grouped_process_num_procs"]; ok {
41+
ch <- prometheus.MustNewConstMetric(d, prometheus.GaugeValue, float64(len(m.metrics)), grouper, group)
42+
}
4143
m.metrics = make(map[int]struct{}) // clear
4244
m.Unlock()
4345
return nil

metric/proc_stat.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,30 @@ func (m *ProcStatMetric) PushCollected(ch chan<- prometheus.Metric, descs map[st
128128
}
129129
m.Unlock()
130130

131-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_minflt_total"], prometheus.CounterValue, minFlt, grouper, group)
132-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_cminflt_total"], prometheus.CounterValue, cMinFlt, grouper, group)
133-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_majflt_total"], prometheus.CounterValue, majFlt, grouper, group)
134-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_cmajflt_total"], prometheus.CounterValue, cMajFlt, grouper, group)
135-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_utime_total"], prometheus.CounterValue, uTime, grouper, group)
136-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_stime_total"], prometheus.CounterValue, sTime, grouper, group)
137-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_cutime_total"], prometheus.CounterValue, cUTime, grouper, group)
138-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_cstime_total"], prometheus.CounterValue, cSTime, grouper, group)
139-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_numthreads"], prometheus.GaugeValue, numThreads, grouper, group)
140-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_vsize_bytes"], prometheus.GaugeValue, vSize, grouper, group)
141-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_rss"], prometheus.GaugeValue, rss, grouper, group)
142-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_stat_clk_tck"], prometheus.GaugeValue, m.clkTck, grouper, group)
131+
values := []struct {
132+
k string
133+
t prometheus.ValueType
134+
v float64
135+
}{
136+
{"grouped_process_stat_minflt_total", prometheus.CounterValue, minFlt},
137+
{"grouped_process_stat_cminflt_total", prometheus.CounterValue, cMinFlt},
138+
{"grouped_process_stat_majflt_total", prometheus.CounterValue, majFlt},
139+
{"grouped_process_stat_cmajflt_total", prometheus.CounterValue, cMajFlt},
140+
{"grouped_process_stat_utime_total", prometheus.CounterValue, uTime},
141+
{"grouped_process_stat_stime_total", prometheus.CounterValue, sTime},
142+
{"grouped_process_stat_cutime_total", prometheus.CounterValue, cUTime},
143+
{"grouped_process_stat_cstime_total", prometheus.CounterValue, cSTime},
144+
{"grouped_process_stat_numthreads", prometheus.GaugeValue, numThreads},
145+
{"grouped_process_stat_vsize_bytes", prometheus.GaugeValue, vSize},
146+
{"grouped_process_stat_rss", prometheus.GaugeValue, rss},
147+
{"grouped_process_stat_clk_tck", prometheus.GaugeValue, m.clkTck},
148+
}
149+
150+
for _, s := range values {
151+
if d, ok := descs[s.k]; ok {
152+
ch <- prometheus.MustNewConstMetric(d, s.t, s.v, grouper, group)
153+
}
154+
}
143155

144156
return nil
145157
}

metric/proc_status.go

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -175,25 +175,37 @@ func (m *ProcStatusMetric) PushCollected(ch chan<- prometheus.Metric, descs map[
175175
}
176176
m.Unlock()
177177

178-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmPeak_bytes_total"], prometheus.GaugeValue, vmPeak, grouper, group)
179-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmSize_bytes_total"], prometheus.GaugeValue, vmSize, grouper, group)
180-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmLck_bytes_total"], prometheus.GaugeValue, vmLck, grouper, group)
181-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmPin_bytes_total"], prometheus.GaugeValue, vmPin, grouper, group)
182-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmHWM_bytes_total"], prometheus.GaugeValue, vmHWM, grouper, group)
183-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmRSS_bytes_total"], prometheus.GaugeValue, vmRSS, grouper, group)
184-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_RssAnon_bytes_total"], prometheus.GaugeValue, rssAnon, grouper, group)
185-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_RssFile_bytes_total"], prometheus.GaugeValue, rssFile, grouper, group)
186-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_RssShmem_bytes_total"], prometheus.GaugeValue, rssShmem, grouper, group)
187-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmData_bytes_total"], prometheus.GaugeValue, vmData, grouper, group)
188-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmStk_bytes_total"], prometheus.GaugeValue, vmStk, grouper, group)
189-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmExe_bytes_total"], prometheus.GaugeValue, vmExe, grouper, group)
190-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmLib_bytes_total"], prometheus.GaugeValue, vmLib, grouper, group)
191-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmPTE_bytes_total"], prometheus.GaugeValue, vmPTE, grouper, group)
192-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmPMD_bytes_total"], prometheus.GaugeValue, vmPMD, grouper, group)
193-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VmSwap_bytes_total"], prometheus.GaugeValue, vmSwap, grouper, group)
194-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_HugetlbPages_bytes_total"], prometheus.GaugeValue, hugetlbPages, grouper, group)
195-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_VoluntaryCtxtSwitches_total"], prometheus.CounterValue, voluntaryCtxtSwitches, grouper, group)
196-
ch <- prometheus.MustNewConstMetric(descs["grouped_process_status_NonVoluntaryCtxtSwitches_total"], prometheus.CounterValue, nonVoluntaryCtxtSwitches, grouper, group)
178+
values := []struct {
179+
k string
180+
t prometheus.ValueType
181+
v float64
182+
}{
183+
{"grouped_process_status_VmPeak_bytes_total", prometheus.GaugeValue, vmPeak},
184+
{"grouped_process_status_VmSize_bytes_total", prometheus.GaugeValue, vmSize},
185+
{"grouped_process_status_VmLck_bytes_total", prometheus.GaugeValue, vmLck},
186+
{"grouped_process_status_VmPin_bytes_total", prometheus.GaugeValue, vmPin},
187+
{"grouped_process_status_VmHWM_bytes_total", prometheus.GaugeValue, vmHWM},
188+
{"grouped_process_status_VmRSS_bytes_total", prometheus.GaugeValue, vmRSS},
189+
{"grouped_process_status_RssAnon_bytes_total", prometheus.GaugeValue, rssAnon},
190+
{"grouped_process_status_RssFile_bytes_total", prometheus.GaugeValue, rssFile},
191+
{"grouped_process_status_RssShmem_bytes_total", prometheus.GaugeValue, rssShmem},
192+
{"grouped_process_status_VmData_bytes_total", prometheus.GaugeValue, vmData},
193+
{"grouped_process_status_VmStk_bytes_total", prometheus.GaugeValue, vmStk},
194+
{"grouped_process_status_VmExe_bytes_total", prometheus.GaugeValue, vmExe},
195+
{"grouped_process_status_VmLib_bytes_total", prometheus.GaugeValue, vmLib},
196+
{"grouped_process_status_VmPTE_bytes_total", prometheus.GaugeValue, vmPTE},
197+
{"grouped_process_status_VmPMD_bytes_total", prometheus.GaugeValue, vmPMD},
198+
{"grouped_process_status_VmSwap_bytes_total", prometheus.GaugeValue, vmSwap},
199+
{"grouped_process_status_HugetlbPages_bytes_total", prometheus.GaugeValue, hugetlbPages},
200+
{"grouped_process_status_VoluntaryCtxtSwitches_total", prometheus.CounterValue, voluntaryCtxtSwitches},
201+
{"grouped_process_status_NonVoluntaryCtxtSwitches_total", prometheus.CounterValue, nonVoluntaryCtxtSwitches},
202+
}
203+
204+
for _, s := range values {
205+
if d, ok := descs[s.k]; ok {
206+
ch <- prometheus.MustNewConstMetric(d, s.t, s.v, grouper, group)
207+
}
208+
}
197209

198210
return nil
199211
}

0 commit comments

Comments
 (0)