Skip to content

Commit

Permalink
Fixes for --disable_metrics flag
Browse files Browse the repository at this point in the history
1. Fix for writing to nil map when value is non-empty.
2. Fix the usage string: go flag package uses backticks for the flag
name.
3. Render the flag default value in the same format as the flag accepts.
  • Loading branch information
tallclair committed May 3, 2016
1 parent 1faa767 commit c8d9cc1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
10 changes: 7 additions & 3 deletions cadvisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,15 @@ type metricSetValue struct {
}

func (ml *metricSetValue) String() string {
return fmt.Sprint(*ml)
var values []string
for metric, _ := range ml.MetricSet {
values = append(values, string(metric))
}
return strings.Join(values, ",")
}

func (ml *metricSetValue) Set(value string) error {
ignoreMetrics = metricSetValue{}
ml.MetricSet = container.MetricSet{}
if value == "" {
return nil
}
Expand All @@ -91,7 +95,7 @@ func (ml *metricSetValue) Set(value string) error {
}

func init() {
flag.Var(&ignoreMetrics, "disable_metrics", "comma-separated list of metrics to be disabled. Options are `disk`, `network`, `tcp`. Note: tcp is disabled by default due to high CPU usage.")
flag.Var(&ignoreMetrics, "disable_metrics", "comma-separated list of `metrics` to be disabled. Options are 'disk', 'network', 'tcp'. Note: tcp is disabled by default due to high CPU usage.")
}

func main() {
Expand Down
22 changes: 18 additions & 4 deletions cadvisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,22 @@ func TestTcpMetricsAreDisabledByDefault(t *testing.T) {
assert.True(t, ignoreMetrics.Has(container.NetworkTcpUsageMetrics))
}

func TestTcpMetricsAreEnabledOnDemand(t *testing.T) {
assert.True(t, ignoreMetrics.Has(container.NetworkTcpUsageMetrics))
ignoreMetrics.Set("")
assert.False(t, ignoreMetrics.Has(container.NetworkTcpUsageMetrics))
func TestIgnoreMetrics(t *testing.T) {
tests := []struct {
value string
expected []container.MetricKind
}{
{"", []container.MetricKind{}},
{"disk", []container.MetricKind{container.DiskUsageMetrics}},
{"disk,tcp,network", []container.MetricKind{container.DiskUsageMetrics, container.NetworkTcpUsageMetrics, container.NetworkUsageMetrics}},
}

for _, test := range tests {
assert.NoError(t, ignoreMetrics.Set(test.value))

assert.Equal(t, len(test.expected), len(ignoreMetrics.MetricSet))
for _, expected := range test.expected {
assert.True(t, ignoreMetrics.Has(expected), "Missing %s", expected)
}
}
}

0 comments on commit c8d9cc1

Please sign in to comment.