Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add docker CPU shares gauge #1597

Merged
merged 3 commits into from Apr 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions pkg/collector/corechecks/containers/docker.go
Expand Up @@ -179,6 +179,7 @@ func (d *DockerCheck) Run() error {
sender.Rate("docker.cpu.system", float64(c.CPU.System), "", tags)
sender.Rate("docker.cpu.user", float64(c.CPU.User), "", tags)
sender.Rate("docker.cpu.usage", c.CPU.UsageTotal, "", tags)
sender.Gauge("docker.cpu.shares", float64(c.CPU.Shares), "", tags)
sender.Rate("docker.cpu.throttled", float64(c.CPUNrThrottled), "", tags)
sender.Gauge("docker.mem.cache", float64(c.Memory.Cache), "", tags)
sender.Gauge("docker.mem.rss", float64(c.Memory.RSS), "", tags)
Expand Down
30 changes: 19 additions & 11 deletions pkg/util/docker/cgroup.go
Expand Up @@ -85,6 +85,7 @@ type CgroupTimesStat struct {
User uint64
UsageTotal float64
SystemUsage uint64
Shares uint64
}

// CgroupIOStat store I/O statistics about a cgroup.
Expand Down Expand Up @@ -112,7 +113,7 @@ func (c ContainerCgroup) Mem() (*CgroupMemStat, error) {

f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("missing cgroup file: %s", statfile)
log.Debugf("Missing cgroup file: %s", statfile)
return ret, nil
} else if err != nil {
return nil, err
Expand Down Expand Up @@ -198,7 +199,7 @@ func (c ContainerCgroup) Mem() (*CgroupMemStat, error) {
func (c ContainerCgroup) MemLimit() (uint64, error) {
v, err := c.ParseSingleStat("memory", "memory.limit_in_bytes")
if os.IsNotExist(err) {
log.Debugf("missing cgroup file: %s",
log.Debugf("Missing cgroup file: %s",
c.cgroupFilePath("memory", "memory.limit_in_bytes"))
return 0, nil
} else if err != nil {
Expand All @@ -219,7 +220,7 @@ func (c ContainerCgroup) CPU() (*CgroupTimesStat, error) {
statfile := c.cgroupFilePath("cpuacct", "cpuacct.stat")
f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("missing cgroup file: %s", statfile)
log.Debugf("Missing cgroup file: %s", statfile)
return ret, nil
} else if err != nil {
return nil, err
Expand Down Expand Up @@ -249,7 +250,14 @@ func (c ContainerCgroup) CPU() (*CgroupTimesStat, error) {
if err == nil {
ret.UsageTotal = float64(usage) / NanoToUserHZDivisor
} else {
log.Debugf("missing total cpu usage stat for %s: %s", c.ContainerID, err.Error())
log.Debugf("Missing total cpu usage stat for %s: %s", c.ContainerID, err.Error())
}

shares, err := c.ParseSingleStat("cpu", "cpu.shares")
if err == nil {
ret.Shares = shares
} else {
log.Debugf("Missing cpu shares stat for %s: %s", c.ContainerID, err.Error())
}

return ret, nil
Expand All @@ -262,7 +270,7 @@ func (c ContainerCgroup) CPUNrThrottled() (uint64, error) {
statfile := c.cgroupFilePath("cpu", "cpu.stat")
f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("missing cgroup file: %s", statfile)
log.Debugf("Missing cgroup file: %s", statfile)
return 0, nil
} else if err != nil {
return 0, err
Expand All @@ -279,7 +287,7 @@ func (c ContainerCgroup) CPUNrThrottled() (uint64, error) {
return value, nil
}
}
log.Debugf("missing nr_throttled line in %s", statfile)
log.Debugf("Missing nr_throttled line in %s", statfile)
return 0, nil
}

Expand All @@ -297,14 +305,14 @@ func (c ContainerCgroup) CPULimit() (float64, error) {
quotaFile := c.cgroupFilePath("cpu", "cpu.cfs_quota_us")
plines, err := readLines(periodFile)
if os.IsNotExist(err) {
log.Debugf("missing cgroup file: %s", periodFile)
log.Debugf("Missing cgroup file: %s", periodFile)
return 100, nil
} else if err != nil {
return 0, err
}
qlines, err := readLines(quotaFile)
if os.IsNotExist(err) {
log.Debugf("missing cgroup file: %s", quotaFile)
log.Debugf("Missing cgroup file: %s", quotaFile)
return 100, nil
} else if err != nil {
return 0, err
Expand Down Expand Up @@ -344,7 +352,7 @@ func (c ContainerCgroup) IO() (*CgroupIOStat, error) {
statfile := c.cgroupFilePath("blkio", "blkio.throttle.io_service_bytes")
f, err := os.Open(statfile)
if os.IsNotExist(err) {
log.Debugf("missing cgroup file: %s", statfile)
log.Debugf("Missing cgroup file: %s", statfile)
return ret, nil
} else if err != nil {
return nil, err
Expand Down Expand Up @@ -407,12 +415,12 @@ func (c ContainerCgroup) ContainerStartTime() (int64, error) {
func (c ContainerCgroup) cgroupFilePath(target, file string) string {
mount, ok := c.Mounts[target]
if !ok {
log.Errorf("missing target %s from mounts", target)
log.Errorf("Missing target %s from mounts", target)
return ""
}
targetPath, ok := c.Paths[target]
if !ok {
log.Errorf("missing target %s from paths", target)
log.Errorf("Missing target %s from paths", target)
return ""
}
// sometimes the container is running inside a "dind container" instead of directly on the host,
Expand Down
4 changes: 3 additions & 1 deletion pkg/util/docker/cgroup_test.go
Expand Up @@ -26,14 +26,16 @@ func TestCPU(t *testing.T) {
}
tempFolder.add("cpuacct/cpuacct.stat", cpuacctStats.String())
tempFolder.add("cpuacct/cpuacct.usage", "915266418275")
tempFolder.add("cpu/cpu.shares", "1024")

cgroup := newDummyContainerCgroup(tempFolder.RootPath, "cpuacct")
cgroup := newDummyContainerCgroup(tempFolder.RootPath, "cpuacct", "cpu")

timeStat, err := cgroup.CPU()
assert.Nil(t, err)
assert.Equal(t, timeStat.ContainerID, "dummy")
assert.Equal(t, timeStat.User, uint64(64140))
assert.Equal(t, timeStat.System, uint64(18327))
assert.Equal(t, timeStat.Shares, uint64(1024))
assert.InDelta(t, timeStat.UsageTotal, 91526.6418275, 0.0000001)
}

Expand Down
@@ -0,0 +1,4 @@
---
features:
- |
Introduce new docker cpu shares gauge.
1 change: 1 addition & 0 deletions test/integration/corechecks/docker/basemetrics_test.go
Expand Up @@ -29,6 +29,7 @@ func TestContainerMetricsTagging(t *testing.T) {

expectedMetrics := map[string][]string{
"Gauge": {
"docker.cpu.shares",
"docker.mem.cache",
"docker.mem.rss",
"docker.mem.in_use",
Expand Down