Skip to content

Commit 286dcab

Browse files
jmccanndanielnelson
authored andcommitted
Add container health metrics to docker input (#3666)
1 parent 4c9e9ac commit 286dcab

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

plugins/inputs/docker/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ based on the availability of per-cpu stats on your system.
199199
- network
200200
- docker_container_blkio specific:
201201
- device
202+
- docker_container_health specific:
203+
- health_status
204+
- failing_streak
202205
- docker_swarm specific:
203206
- service_id
204207
- service_name
@@ -257,4 +260,4 @@ io_serviced_recursive_total=6599i,io_serviced_recursive_write=107i 1453409536840
257260
>docker_swarm,
258261
service_id=xaup2o9krw36j2dy1mjx1arjw,service_mode=replicated,service_name=test,\
259262
tasks_desired=3,tasks_running=3 1508968160000000000
260-
```
263+
```

plugins/inputs/docker/docker.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,13 @@ func (d *Docker) gatherContainer(
391391
}
392392
}
393393

394+
info, err := d.client.ContainerInspect(ctx, container.ID)
395+
if err != nil {
396+
return fmt.Errorf("Error inspecting docker container: %s", err.Error())
397+
}
398+
394399
// Add whitelisted environment variables to tags
395400
if len(d.TagEnvironment) > 0 {
396-
info, err := d.client.ContainerInspect(ctx, container.ID)
397-
if err != nil {
398-
return fmt.Errorf("Error inspecting docker container: %s", err.Error())
399-
}
400401
for _, envvar := range info.Config.Env {
401402
for _, configvar := range d.TagEnvironment {
402403
dock_env := strings.SplitN(envvar, "=", 2)
@@ -408,6 +409,14 @@ func (d *Docker) gatherContainer(
408409
}
409410
}
410411

412+
if info.State.Health != nil {
413+
healthfields := map[string]interface{}{
414+
"health_status": info.State.Health.Status,
415+
"failing_streak": info.ContainerJSONBase.State.Health.FailingStreak,
416+
}
417+
acc.AddFields("docker_container_health", healthfields, tags, time.Now())
418+
}
419+
411420
gatherContainerStats(v, acc, tags, container.ID, d.PerDevice, d.Total, daemonOSType)
412421

413422
return nil

plugins/inputs/docker/docker_testdata.go

+8
Original file line numberDiff line numberDiff line change
@@ -477,4 +477,12 @@ var containerInspect = types.ContainerJSON{
477477
"PATH=/bin:/sbin",
478478
},
479479
},
480+
ContainerJSONBase: &types.ContainerJSONBase{
481+
State: &types.ContainerState{
482+
Health: &types.Health{
483+
FailingStreak: 1,
484+
Status: "Unhealthy",
485+
},
486+
},
487+
},
480488
}

0 commit comments

Comments
 (0)