From 8aa6164192a83bc61cc8b358ab07f2125234b705 Mon Sep 17 00:00:00 2001 From: Seth Jennings Date: Mon, 14 Mar 2016 14:14:52 -0500 Subject: [PATCH] allow ignoreMetrics in new manager --- cadvisor.go | 41 ++++++++++++++++++++++++++++++++++++++++- cadvisor_test.go | 29 +++++++++++++++++++++++++++++ manager/manager.go | 40 ++-------------------------------------- manager/manager_test.go | 7 +------ 4 files changed, 72 insertions(+), 45 deletions(-) create mode 100644 cadvisor_test.go diff --git a/cadvisor.go b/cadvisor.go index 9a5102ca..b67c0720 100644 --- a/cadvisor.go +++ b/cadvisor.go @@ -22,9 +22,11 @@ import ( "os" "os/signal" "runtime" + "strings" "syscall" "time" + "github.com/google/cadvisor/container" cadvisorhttp "github.com/google/cadvisor/http" "github.com/google/cadvisor/manager" "github.com/google/cadvisor/utils/sysfs" @@ -52,6 +54,43 @@ var allowDynamicHousekeeping = flag.Bool("allow_dynamic_housekeeping", true, "Wh var enableProfiling = flag.Bool("profiling", false, "Enable profiling via web interface host:port/debug/pprof/") +var ( + // Metrics to be ignored. + ignoreMetrics metricSetValue = metricSetValue{container.MetricSet{}} + + // List of metrics that can be ignored. + ignoreWhitelist = container.MetricSet{ + container.DiskUsageMetrics: struct{}{}, + container.NetworkUsageMetrics: struct{}{}, + container.NetworkTcpUsageMetrics: struct{}{}, + } +) + +type metricSetValue struct { + container.MetricSet +} + +func (ml *metricSetValue) String() string { + return fmt.Sprint(*ml) +} + +func (ml *metricSetValue) Set(value string) error { + for _, metric := range strings.Split(value, ",") { + if ignoreWhitelist.Has(container.MetricKind(metric)) { + (*ml).Add(container.MetricKind(metric)) + } else { + return fmt.Errorf("unsupported metric %q specified in disable_metrics", metric) + } + } + return nil +} + +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.") + // Tcp metrics are ignored by default. + flag.Set("disable_metrics", "tcp") +} + func main() { defer glog.Flush() flag.Parse() @@ -73,7 +112,7 @@ func main() { glog.Fatalf("Failed to create a system interface: %s", err) } - containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping) + containerManager, err := manager.New(memoryStorage, sysFs, *maxHousekeepingInterval, *allowDynamicHousekeeping, ignoreMetrics.MetricSet) if err != nil { glog.Fatalf("Failed to create a Container Manager: %s", err) } diff --git a/cadvisor_test.go b/cadvisor_test.go new file mode 100644 index 00000000..27a5ad0b --- /dev/null +++ b/cadvisor_test.go @@ -0,0 +1,29 @@ +// Copyright 2014 Google Inc. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "flag" + "testing" + + "github.com/google/cadvisor/container" + "github.com/stretchr/testify/assert" +) + +func TestTcpMetricsAreDisabledByDefault(t *testing.T) { + assert.True(t, ignoreMetrics.Has(container.NetworkTcpUsageMetrics)) + flag.Parse() + assert.True(t, ignoreMetrics.Has(container.NetworkTcpUsageMetrics)) +} diff --git a/manager/manager.go b/manager/manager.go index 1c3b551b..eb8d2cb9 100644 --- a/manager/manager.go +++ b/manager/manager.go @@ -50,42 +50,6 @@ var eventStorageAgeLimit = flag.String("event_storage_age_limit", "default=24h", var eventStorageEventLimit = flag.String("event_storage_event_limit", "default=100000", "Max number of events to store (per type). Value is a comma separated list of key values, where the keys are event types (e.g.: creation, oom) or \"default\" and the value is an integer. Default is applied to all non-specified event types") var applicationMetricsCountLimit = flag.Int("application_metrics_count_limit", 100, "Max number of application metrics to store (per container)") -var ( - // Metrics to be ignored. - ignoreMetrics metricSetValue = metricSetValue{container.MetricSet{}} - // List of metrics that can be ignored. - ignoreWhitelist = container.MetricSet{ - container.DiskUsageMetrics: struct{}{}, - container.NetworkUsageMetrics: struct{}{}, - container.NetworkTcpUsageMetrics: struct{}{}, - } -) - -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.") - // Tcp metrics are ignored by default. - flag.Set("disable_metrics", "tcp") -} - -type metricSetValue struct { - container.MetricSet -} - -func (ml *metricSetValue) String() string { - return fmt.Sprint(*ml) -} - -func (ml *metricSetValue) Set(value string) error { - for _, metric := range strings.Split(value, ",") { - if ignoreWhitelist.Has(container.MetricKind(metric)) { - (*ml).Add(container.MetricKind(metric)) - } else { - return fmt.Errorf("unsupported metric %q specified in disable_metrics", metric) - } - } - return nil -} - // The Manager interface defines operations for starting a manager and getting // container and machine information. type Manager interface { @@ -155,7 +119,7 @@ type Manager interface { } // New takes a memory storage and returns a new manager. -func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool) (Manager, error) { +func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingInterval time.Duration, allowDynamicHousekeeping bool, ignoreMetricsSet container.MetricSet) (Manager, error) { if memoryCache == nil { return nil, fmt.Errorf("manager requires memory storage") } @@ -194,7 +158,7 @@ func New(memoryCache *memory.InMemoryCache, sysfs sysfs.SysFs, maxHousekeepingIn startupTime: time.Now(), maxHousekeepingInterval: maxHousekeepingInterval, allowDynamicHousekeeping: allowDynamicHousekeeping, - ignoreMetrics: ignoreMetrics.MetricSet, + ignoreMetrics: ignoreMetricsSet, } machineInfo, err := getMachineInfo(sysfs, fsInfo, inHostNamespace) diff --git a/manager/manager_test.go b/manager/manager_test.go index 8aaee923..cc426a52 100644 --- a/manager/manager_test.go +++ b/manager/manager_test.go @@ -29,7 +29,6 @@ import ( info "github.com/google/cadvisor/info/v1" itest "github.com/google/cadvisor/info/v1/test" "github.com/google/cadvisor/utils/sysfs/fakesysfs" - "github.com/stretchr/testify/assert" ) // TODO(vmarmol): Refactor these tests. @@ -206,12 +205,8 @@ func TestDockerContainersInfo(t *testing.T) { } func TestNewNilManager(t *testing.T) { - _, err := New(nil, nil, 60*time.Second, true) + _, err := New(nil, nil, 60*time.Second, true, container.MetricSet{}) if err == nil { t.Fatalf("Expected nil manager to return error") } } - -func TestTcpMetricsAreDisabledByDefault(t *testing.T) { - assert.True(t, ignoreMetrics.Has(container.NetworkTcpUsageMetrics)) -}