Skip to content

Commit 4922785

Browse files
eero-tPaweł Szulik
authored andcommitted
Use AllMetrics for enable & disable lists instead of ignoreWhitelist
Use of AllMetrics instead of ignoreWhitelist, adds CpuUsageMetrics ("cpu") & AppMetrics ("app") to metrics that can be disabled/enabled from command line. Whether they are enabled is already checked by prometheus export code. That also makes metricSetValue type redundant, as it can now be replaced with MetricSet use. Removing it requires moving String() & Set() methods used by flag package for option handling and validating given metrics options, to factory.go.
1 parent 63d8378 commit 4922785

File tree

3 files changed

+39
-73
lines changed

3 files changed

+39
-73
lines changed

cmd/cadvisor.go

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"os"
2424
"os/signal"
2525
"runtime"
26-
"sort"
2726
"strings"
2827
"syscall"
2928
"time"
@@ -81,7 +80,7 @@ var perfEvents = flag.String("perf_events_config", "", "Path to a JSON file cont
8180
var (
8281
// Metrics to be ignored.
8382
// Tcp metrics are ignored by default.
84-
ignoreMetrics metricSetValue = metricSetValue{container.MetricSet{
83+
ignoreMetrics = container.MetricSet{
8584
container.MemoryNumaMetrics: struct{}{},
8685
container.NetworkTcpUsageMetrics: struct{}{},
8786
container.NetworkUdpUsageMetrics: struct{}{},
@@ -93,74 +92,16 @@ var (
9392
container.CPUTopologyMetrics: struct{}{},
9493
container.ResctrlMetrics: struct{}{},
9594
container.CPUSetMetrics: struct{}{},
96-
}}
97-
98-
// Metrics to be enabled on top of metrics not in ignoreWhitelist.
99-
// Used only if non-empty.
100-
enableMetrics metricSetValue = metricSetValue{container.MetricSet{}}
101-
102-
// List of metrics that can be ignored.
103-
ignoreWhitelist = container.MetricSet{
104-
container.AcceleratorUsageMetrics: struct{}{},
105-
container.CpuLoadMetrics: struct{}{},
106-
container.DiskUsageMetrics: struct{}{},
107-
container.DiskIOMetrics: struct{}{},
108-
container.MemoryNumaMetrics: struct{}{},
109-
container.MemoryUsageMetrics: struct{}{},
110-
container.NetworkUsageMetrics: struct{}{},
111-
container.NetworkTcpUsageMetrics: struct{}{},
112-
container.NetworkAdvancedTcpUsageMetrics: struct{}{},
113-
container.NetworkUdpUsageMetrics: struct{}{},
114-
container.PerCpuUsageMetrics: struct{}{},
115-
container.PerfMetrics: struct{}{},
116-
container.ProcessSchedulerMetrics: struct{}{},
117-
container.ProcessMetrics: struct{}{},
118-
container.HugetlbUsageMetrics: struct{}{},
119-
container.ReferencedMemoryMetrics: struct{}{},
120-
container.CPUTopologyMetrics: struct{}{},
121-
container.ResctrlMetrics: struct{}{},
122-
container.CPUSetMetrics: struct{}{},
123-
container.OOMMetrics: struct{}{},
12495
}
125-
)
12696

127-
type metricSetValue struct {
128-
container.MetricSet
129-
}
130-
131-
func (ml *metricSetValue) String() string {
132-
values := make([]string, 0, len(ml.MetricSet))
133-
for metric := range ml.MetricSet {
134-
values = append(values, string(metric))
135-
}
136-
sort.Strings(values)
137-
return strings.Join(values, ",")
138-
}
139-
140-
func (ml *metricSetValue) Set(value string) error {
141-
ml.MetricSet = container.MetricSet{}
142-
if value == "" {
143-
return nil
144-
}
145-
for _, metric := range strings.Split(value, ",") {
146-
if ignoreWhitelist.Has(container.MetricKind(metric)) {
147-
(*ml).Add(container.MetricKind(metric))
148-
} else {
149-
return fmt.Errorf("unsupported metric %q specified", metric)
150-
}
151-
}
152-
return nil
153-
}
97+
// Metrics to be enabled. Used only if non-empty.
98+
enableMetrics = container.MetricSet{}
99+
)
154100

155101
func init() {
156-
opts := make([]string, 0, len(ignoreWhitelist))
157-
for key := range ignoreWhitelist {
158-
opts = append(opts, string(key))
159-
}
160-
sort.Strings(opts)
161-
optstr := strings.Join(opts, "', '")
162-
flag.Var(&ignoreMetrics, "disable_metrics", fmt.Sprintf("comma-separated list of `metrics` to be disabled. Options are '%s'.", optstr))
163-
flag.Var(&enableMetrics, "enable_metrics", fmt.Sprintf("comma-separated list of `metrics` to be enabled. If set, overrides 'disable_metrics'. Options are '%s'.", optstr))
102+
optstr := container.AllMetrics.String()
103+
flag.Var(&ignoreMetrics, "disable_metrics", fmt.Sprintf("comma-separated list of `metrics` to be disabled. Options are %s.", optstr))
104+
flag.Var(&enableMetrics, "enable_metrics", fmt.Sprintf("comma-separated list of `metrics` to be enabled. If set, overrides 'disable_metrics'. Options are %s.", optstr))
164105

165106
// Default logging verbosity to V(2)
166107
flag.Set("v", "2")
@@ -177,13 +118,12 @@ func main() {
177118
}
178119

179120
var includedMetrics container.MetricSet
180-
if len(enableMetrics.MetricSet) > 0 {
181-
includedMetrics = toIncludedMetrics(ignoreWhitelist)
182-
includedMetrics = includedMetrics.Append(enableMetrics.MetricSet)
121+
if len(enableMetrics) > 0 {
122+
includedMetrics = enableMetrics
183123
} else {
184-
includedMetrics = toIncludedMetrics(ignoreMetrics.MetricSet)
124+
includedMetrics = toIncludedMetrics(ignoreMetrics)
185125
}
186-
klog.V(1).Infof("enabled metrics: %s", (&metricSetValue{includedMetrics}).String())
126+
klog.V(1).Infof("enabled metrics: %s", includedMetrics.String())
187127
setMaxProcs()
188128

189129
memoryStorage, err := NewMemoryStorage()

cmd/cadvisor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func TestEnableAndIgnoreMetrics(t *testing.T) {
6969
}
7070

7171
for _, test := range tests {
72-
for _, sets := range []metricSetValue{enableMetrics, ignoreMetrics} {
72+
for _, sets := range []container.MetricSet{enableMetrics, ignoreMetrics} {
7373
assert.NoError(t, sets.Set(test.value))
7474

75-
assert.Equal(t, len(test.expected), len(sets.MetricSet))
75+
assert.Equal(t, len(test.expected), len(sets))
7676
for _, expected := range test.expected {
7777
assert.True(t, sets.Has(expected), "Missing %s", expected)
7878
}

container/factory.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package container
1616

1717
import (
1818
"fmt"
19+
"sort"
20+
"strings"
1921
"sync"
2022

2123
"github.com/google/cadvisor/fs"
@@ -108,6 +110,30 @@ func (ms MetricSet) Add(mk MetricKind) {
108110
ms[mk] = struct{}{}
109111
}
110112

113+
func (ms MetricSet) String() string {
114+
values := make([]string, 0, len(ms))
115+
for metric := range ms {
116+
values = append(values, string(metric))
117+
}
118+
sort.Strings(values)
119+
return strings.Join(values, ",")
120+
}
121+
122+
func (ms *MetricSet) Set(value string) error {
123+
*ms = MetricSet{}
124+
if value == "" {
125+
return nil
126+
}
127+
for _, metric := range strings.Split(value, ",") {
128+
if AllMetrics.Has(MetricKind(metric)) {
129+
(*ms).Add(MetricKind(metric))
130+
} else {
131+
return fmt.Errorf("unsupported metric %q specified", metric)
132+
}
133+
}
134+
return nil
135+
}
136+
111137
func (ms MetricSet) Difference(ms1 MetricSet) MetricSet {
112138
result := MetricSet{}
113139
for kind := range ms {

0 commit comments

Comments
 (0)