Merge pull request #1158 from vishh/net_metrics

Avoid collecting network stats for non root cgroups in raw handler.
This commit is contained in:
Vish Kannan 2016-03-15 14:25:42 -07:00
commit 1cc8e4d641
3 changed files with 39 additions and 28 deletions

View File

@ -56,7 +56,8 @@ var enableProfiling = flag.Bool("profiling", false, "Enable profiling via web in
var ( var (
// Metrics to be ignored. // Metrics to be ignored.
ignoreMetrics metricSetValue = metricSetValue{container.MetricSet{}} // Tcp metrics are ignored by default.
ignoreMetrics metricSetValue = metricSetValue{container.MetricSet{container.NetworkTcpUsageMetrics: struct{}{}}}
// List of metrics that can be ignored. // List of metrics that can be ignored.
ignoreWhitelist = container.MetricSet{ ignoreWhitelist = container.MetricSet{
@ -87,8 +88,6 @@ func (ml *metricSetValue) Set(value string) error {
func init() { 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.")
// Tcp metrics are ignored by default.
flag.Set("disable_metrics", "tcp")
} }
func main() { func main() {

View File

@ -92,29 +92,30 @@ func GetStats(cgroupManager cgroups.Manager, rootFs string, pid int, ignoreMetri
stats := toContainerStats(libcontainerStats) stats := toContainerStats(libcontainerStats)
// If we know the pid then get network stats from /proc/<pid>/net/dev // If we know the pid then get network stats from /proc/<pid>/net/dev
if pid > 0 { if pid == 0 {
if !ignoreMetrics.Has(container.NetworkUsageMetrics) { return stats, nil
netStats, err := networkStatsFromProc(rootFs, pid) }
if err != nil { if !ignoreMetrics.Has(container.NetworkUsageMetrics) {
glog.V(2).Infof("Unable to get network stats from pid %d: %v", pid, err) netStats, err := networkStatsFromProc(rootFs, pid)
} else { if err != nil {
stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...) glog.V(2).Infof("Unable to get network stats from pid %d: %v", pid, err)
} } else {
stats.Network.Interfaces = append(stats.Network.Interfaces, netStats...)
}
}
if !ignoreMetrics.Has(container.NetworkTcpUsageMetrics) {
t, err := tcpStatsFromProc(rootFs, pid, "net/tcp")
if err != nil {
glog.V(2).Infof("Unable to get tcp stats from pid %d: %v", pid, err)
} else {
stats.Network.Tcp = t
} }
if !ignoreMetrics.Has(container.NetworkTcpUsageMetrics) {
t, err := tcpStatsFromProc(rootFs, pid, "net/tcp")
if err != nil {
glog.V(2).Infof("Unable to get tcp stats from pid %d: %v", pid, err)
} else {
stats.Network.Tcp = t
}
t6, err := tcpStatsFromProc(rootFs, pid, "net/tcp6") t6, err := tcpStatsFromProc(rootFs, pid, "net/tcp6")
if err != nil { if err != nil {
glog.V(2).Infof("Unable to get tcp6 stats from pid %d: %v", pid, err) glog.V(2).Infof("Unable to get tcp6 stats from pid %d: %v", pid, err)
} else { } else {
stats.Network.Tcp6 = t6 stats.Network.Tcp6 = t6
}
} }
} }

View File

@ -18,7 +18,6 @@ package raw
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path" "path"
"strings" "strings"
@ -62,6 +61,8 @@ type rawContainerHandler struct {
// Metrics to be ignored. // Metrics to be ignored.
ignoreMetrics container.MetricSet ignoreMetrics container.MetricSet
pid int
} }
func (self *rawContainerHandler) GetCgroupPaths() map[string]string { func (self *rawContainerHandler) GetCgroupPaths() map[string]string {
@ -88,6 +89,10 @@ func (self *rawContainerHandler) HasFilesystem() bool {
return false return false
} }
func isRootCgroup(name string) bool {
return name == "/"
}
func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, watcher *common.InotifyWatcher, rootFs string, ignoreMetrics container.MetricSet) (container.ContainerHandler, error) { func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSubsystems, machineInfoFactory info.MachineInfoFactory, fsInfo fs.FsInfo, watcher *common.InotifyWatcher, rootFs string, ignoreMetrics container.MetricSet) (container.ContainerHandler, error) {
cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems.MountPoints, name) cgroupPaths := common.MakeCgroupPaths(cgroupSubsystems.MountPoints, name)
@ -112,6 +117,11 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu
} }
} }
pid := 0
if isRootCgroup(name) {
pid = 1
}
return &rawContainerHandler{ return &rawContainerHandler{
name: name, name: name,
cgroupSubsystems: cgroupSubsystems, cgroupSubsystems: cgroupSubsystems,
@ -124,6 +134,7 @@ func newRawContainerHandler(name string, cgroupSubsystems *libcontainer.CgroupSu
watcher: watcher, watcher: watcher,
rootFs: rootFs, rootFs: rootFs,
ignoreMetrics: ignoreMetrics, ignoreMetrics: ignoreMetrics,
pid: pid,
}, nil }, nil
} }
@ -136,7 +147,7 @@ func (self *rawContainerHandler) ContainerReference() (info.ContainerReference,
func (self *rawContainerHandler) GetRootNetworkDevices() ([]info.NetInfo, error) { func (self *rawContainerHandler) GetRootNetworkDevices() ([]info.NetInfo, error) {
nd := []info.NetInfo{} nd := []info.NetInfo{}
if self.name == "/" { if isRootCgroup(self.name) {
mi, err := self.machineInfoFactory.GetMachineInfo() mi, err := self.machineInfoFactory.GetMachineInfo()
if err != nil { if err != nil {
return nd, err return nd, err
@ -158,7 +169,7 @@ func (self *rawContainerHandler) GetSpec() (info.ContainerSpec, error) {
func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error { func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error {
// Get Filesystem information only for the root cgroup. // Get Filesystem information only for the root cgroup.
if self.name == "/" { if isRootCgroup(self.name) {
filesystems, err := self.fsInfo.GetGlobalFsInfo() filesystems, err := self.fsInfo.GetGlobalFsInfo()
if err != nil { if err != nil {
return err return err
@ -221,7 +232,7 @@ func (self *rawContainerHandler) getFsStats(stats *info.ContainerStats) error {
} }
func (self *rawContainerHandler) GetStats() (*info.ContainerStats, error) { func (self *rawContainerHandler) GetStats() (*info.ContainerStats, error) {
stats, err := libcontainer.GetStats(self.cgroupManager, self.rootFs, os.Getpid(), self.ignoreMetrics) stats, err := libcontainer.GetStats(self.cgroupManager, self.rootFs, self.pid, self.ignoreMetrics)
if err != nil { if err != nil {
return stats, err return stats, err
} }