Skip to content
This repository has been archived by the owner on Mar 27, 2024. It is now read-only.

Commit

Permalink
docker: remove data usage call (#780)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyam8 authored Aug 26, 2022
1 parent c455f16 commit 83d9a64
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 137 deletions.
2 changes: 0 additions & 2 deletions modules/docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ All metrics have "docker." prefix.
| unhealthy_containers | global | unhealthy | containers |
| images | global | active, dangling | images |
| images_size | global | size | B |
| volumes | global | active, dangling | images |
| volumes_size | global | size | B |

## Configuration

Expand Down
32 changes: 0 additions & 32 deletions modules/docker/charts.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ const (
prioContainersUnhealthy
prioImagesCount
prioImagesSize
prioVolumesCount
prioVolumesSize
)

var charts = module.Charts{
Expand All @@ -21,9 +19,6 @@ var charts = module.Charts{

imagesCountChart.Copy(),
imagesSizeChart.Copy(),

volumesCountChart.Copy(),
volumesSizeChart.Copy(),
}

var (
Expand Down Expand Up @@ -91,30 +86,3 @@ var (
},
}
)

var (
volumesCountChart = module.Chart{
ID: "volumes_count",
Title: "Number of volumes",
Units: "volumes",
Fam: "volumes",
Ctx: "docker.volumes",
Priority: prioVolumesCount,
Type: module.Stacked,
Dims: module.Dims{
{ID: "volumes_active", Name: "active"},
{ID: "volumes_dangling", Name: "dangling"},
},
}
volumesSizeChart = module.Chart{
ID: "volumes_size",
Title: "Volumes size",
Units: "B",
Fam: "volumes",
Ctx: "docker.volumes_size",
Priority: prioVolumesSize,
Dims: module.Dims{
{ID: "volumes_size", Name: "size"},
},
}
)
77 changes: 25 additions & 52 deletions modules/docker/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,79 +22,52 @@ func (d *Docker) collect() (map[string]int64, error) {

mx := make(map[string]int64)

if err := d.collectUsage(mx); err != nil {
if err := d.collectInfo(mx); err != nil {
return nil, err
}
if err := d.collectContainersHealth(mx); err != nil {
return nil, err
}
if err := d.collectImages(mx); err != nil {
return nil, err
}

return mx, nil
}

func (d *Docker) collectUsage(mx map[string]int64) error {
func (d *Docker) collectInfo(mx map[string]int64) error {
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()

usage, err := d.client.DiskUsage(ctx)
info, err := d.client.Info(ctx)
if err != nil {
return err
}

data := struct {
containersRunning int64
containersPaused int64
containersExited int64
imagesTotal int64
imagesDangling int64
imagesSize int64
volumesTotal int64
volumesDangling int64
volumesSize int64
}{}

for _, v := range usage.Containers {
switch v.State {
case "running":
data.containersRunning++
case "exited":
data.containersExited++
case "paused":
data.containersPaused++
}
}
mx["running_containers"] = int64(info.ContainersRunning)
mx["paused_containers"] = int64(info.ContainersPaused)
mx["exited_containers"] = int64(info.ContainersStopped)

for _, v := range usage.Images {
data.imagesTotal++
if v.Containers == 0 {
data.imagesDangling++
}
data.imagesSize += v.Size
return nil
}

func (d *Docker) collectImages(mx map[string]int64) error {
ctx, cancel := context.WithTimeout(context.Background(), d.Timeout.Duration)
defer cancel()

images, err := d.client.ImageList(ctx, types.ImageListOptions{})
if err != nil {
return err
}

for _, v := range usage.Volumes {
data.volumesTotal++
if v.UsageData == nil {
continue
}
if v.UsageData.RefCount == 0 {
data.volumesDangling++
}
if v.UsageData.Size != -1 {
data.volumesSize += v.UsageData.Size
for _, v := range images {
mx["images_size"] += v.Size
if v.Containers == 0 {
mx["images_dangling"]++
} else {
mx["images_active"]++
}
}

mx["running_containers"] = data.containersRunning
mx["exited_containers"] = data.containersExited
mx["paused_containers"] = data.containersPaused
mx["images_active"] = data.imagesTotal - data.imagesDangling
mx["images_dangling"] = data.imagesDangling
mx["images_size"] = data.imagesSize
mx["volumes_active"] = data.volumesTotal - data.volumesDangling
mx["volumes_dangling"] = data.volumesDangling
mx["volumes_size"] = data.volumesSize

return nil
}

Expand Down
8 changes: 3 additions & 5 deletions modules/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (

func init() {
module.Register("docker", module.Creator{
Defaults: module.Defaults{
UpdateEvery: 5,
},
Create: func() module.Module { return New() },
})
}
Expand All @@ -26,7 +23,7 @@ func New() *Docker {
return &Docker{
Config: Config{
Address: docker.DefaultDockerHost,
Timeout: web.Duration{Duration: time.Second * 1},
Timeout: web.Duration{Duration: time.Second * 5},
},
charts: charts.Copy(),
newClient: func(cfg Config) (dockerClient, error) {
Expand All @@ -51,7 +48,8 @@ type (
client dockerClient
}
dockerClient interface {
DiskUsage(ctx context.Context) (types.DiskUsage, error)
Info(context.Context) (types.Info, error)
ImageList(context.Context, types.ImageListOptions) ([]types.ImageSummary, error)
ContainerList(context.Context, types.ContainerListOptions) ([]types.Container, error)
Close() error
}
Expand Down
83 changes: 37 additions & 46 deletions modules/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func TestDocker_Check(t *testing.T) {
},
"fail when error on DiskUsage()": {
wantFail: true,
prepare: func() *Docker { return prepareDockerWithMock(&mockClient{errOnDiskUsage: true}) },
prepare: func() *Docker { return prepareDockerWithMock(&mockClient{errOnInfo: true}) },
},
"fail when error on ContainerList()": {
wantFail: true,
Expand Down Expand Up @@ -138,27 +138,28 @@ func TestDocker_Collect(t *testing.T) {
"images_active": 1,
"images_dangling": 1,
"images_size": 300,
"paused_containers": 1,
"running_containers": 1,
"exited_containers": 1,
"paused_containers": 5,
"running_containers": 4,
"exited_containers": 6,
"unhealthy_containers": 3,
"volumes_active": 1,
"volumes_dangling": 1,
"volumes_size": 300,
},
},
"fail when error on creating docker client": {
prepare: func() *Docker { return prepareDockerWithMock(nil) },
expected: nil,
},
"fail when error on DiskUsage()": {
prepare: func() *Docker { return prepareDockerWithMock(&mockClient{errOnDiskUsage: true}) },
"fail when error on Info()": {
prepare: func() *Docker { return prepareDockerWithMock(&mockClient{errOnInfo: true}) },
expected: nil,
},
"fail when error on ContainerList()": {
prepare: func() *Docker { return prepareDockerWithMock(&mockClient{errOnContainerList: true}) },
expected: nil,
},
"fail when error on ImageList()": {
prepare: func() *Docker { return prepareDockerWithMock(&mockClient{errOnImageList: true}) },
expected: nil,
},
}

for name, test := range tests {
Expand Down Expand Up @@ -186,49 +187,22 @@ func prepareDockerWithMock(m *mockClient) *Docker {
}

type mockClient struct {
errOnDiskUsage bool
errOnInfo bool
errOnContainerList bool
errOnImageList bool
closeCalled bool
}

func (m *mockClient) DiskUsage(_ context.Context) (types.DiskUsage, error) {
if m.errOnDiskUsage {
return types.DiskUsage{}, errors.New("mockClient.DiskUsage() error")
}

usage := types.DiskUsage{
Images: []*types.ImageSummary{
{
Containers: 0,
Size: 100,
},
{
Containers: 1,
Size: 200,
},
},
Containers: []*types.Container{
{State: "running"},
{State: "exited"},
{State: "paused"},
},
Volumes: []*types.Volume{
{
UsageData: &types.VolumeUsageData{
RefCount: 0,
Size: 100,
},
},
{
UsageData: &types.VolumeUsageData{
RefCount: 1,
Size: 200,
},
},
},
func (m *mockClient) Info(_ context.Context) (types.Info, error) {
if m.errOnInfo {
return types.Info{}, errors.New("mockClient.Info() error")
}

return usage, nil
return types.Info{
ContainersRunning: 4,
ContainersPaused: 5,
ContainersStopped: 6,
}, nil
}

func (m *mockClient) ContainerList(_ context.Context, opts types.ContainerListOptions) ([]types.Container, error) {
Expand All @@ -252,6 +226,23 @@ func (m *mockClient) ContainerList(_ context.Context, opts types.ContainerListOp
}
}

func (m *mockClient) ImageList(_ context.Context, _ types.ImageListOptions) ([]types.ImageSummary, error) {
if m.errOnImageList {
return nil, errors.New("mockClient.ImageList() error")
}

return []types.ImageSummary{
{
Containers: 0,
Size: 100,
},
{
Containers: 1,
Size: 200,
},
}, nil
}

func (m *mockClient) Close() error {
m.closeCalled = true
return nil
Expand Down

0 comments on commit 83d9a64

Please sign in to comment.