Skip to content

Commit 6f5d465

Browse files
eero-tPaweł Szulik
authored andcommitted
Add 'enable_metrics' option
- If set, it's used instead of disable_metrics - Append() method is added to MetricSet for it - Log at start list of metrics that are enabled
1 parent 385c659 commit 6f5d465

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

cmd/cadvisor.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ var (
9595
container.CPUSetMetrics: struct{}{},
9696
}}
9797

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+
98102
// List of metrics that can be ignored.
99103
ignoreWhitelist = container.MetricSet{
100104
container.AcceleratorUsageMetrics: struct{}{},
@@ -142,7 +146,7 @@ func (ml *metricSetValue) Set(value string) error {
142146
if ignoreWhitelist.Has(container.MetricKind(metric)) {
143147
(*ml).Add(container.MetricKind(metric))
144148
} else {
145-
return fmt.Errorf("unsupported metric %q specified in disable_metrics", metric)
149+
return fmt.Errorf("unsupported metric %q specified", metric)
146150
}
147151
}
148152
return nil
@@ -154,7 +158,9 @@ func init() {
154158
opts = append(opts, string(key))
155159
}
156160
sort.Strings(opts)
157-
flag.Var(&ignoreMetrics, "disable_metrics", fmt.Sprintf("comma-separated list of `metrics` to be disabled. Options are '%s'.", strings.Join(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))
158164

159165
// Default logging verbosity to V(2)
160166
flag.Set("v", "2")
@@ -170,8 +176,14 @@ func main() {
170176
os.Exit(0)
171177
}
172178

173-
includedMetrics := toIncludedMetrics(ignoreMetrics.MetricSet)
174-
179+
var includedMetrics container.MetricSet
180+
if len(enableMetrics.MetricSet) > 0 {
181+
includedMetrics = toIncludedMetrics(ignoreWhitelist)
182+
includedMetrics = includedMetrics.Append(enableMetrics.MetricSet)
183+
} else {
184+
includedMetrics = toIncludedMetrics(ignoreMetrics.MetricSet)
185+
}
186+
klog.V(1).Infof("enabled metrics: %s", (&metricSetValue{includedMetrics}).String())
175187
setMaxProcs()
176188

177189
memoryStorage, err := NewMemoryStorage()

container/factory.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ func (ms MetricSet) Difference(ms1 MetricSet) MetricSet {
118118
return result
119119
}
120120

121+
func (ms MetricSet) Append(ms1 MetricSet) MetricSet {
122+
result := ms
123+
for kind := range ms1 {
124+
if !ms.Has(kind) {
125+
result.Add(kind)
126+
}
127+
}
128+
return result
129+
}
130+
121131
// All registered auth provider plugins.
122132
var pluginsLock sync.Mutex
123133
var plugins = make(map[string]Plugin)

0 commit comments

Comments
 (0)