Skip to content

Commit c45ecb2

Browse files
committed
Fix docker health state
1 parent 9ad7b85 commit c45ecb2

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

container/docker/handler.go

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,8 @@ type dockerContainerHandler struct {
6868
creationTime time.Time
6969

7070
// Metadata associated with the container.
71-
envs map[string]string
72-
labels map[string]string
73-
healthStatus string
71+
envs map[string]string
72+
labels map[string]string
7473

7574
// Image name used for this container.
7675
image string
@@ -93,6 +92,9 @@ type dockerContainerHandler struct {
9392
reference info.ContainerReference
9493

9594
libcontainerHandler *containerlibcontainer.Handler
95+
96+
// the docker client is needed to inspect the container and get the health status
97+
client docker.APIClient
9698
}
9799

98100
var _ container.ContainerHandler = &dockerContainerHandler{}
@@ -201,10 +203,7 @@ func newDockerContainerHandler(
201203
labels: ctnr.Config.Labels,
202204
includedMetrics: metrics,
203205
zfsParent: zfsParent,
204-
}
205-
// Health status may be nil if no health check is configured
206-
if ctnr.State.Health != nil {
207-
handler.healthStatus = ctnr.State.Health.Status
206+
client: client,
208207
}
209208
// Timestamp returned by Docker is in time.RFC3339Nano format.
210209
handler.creationTime, err = time.Parse(time.RFC3339Nano, ctnr.Created)
@@ -328,13 +327,20 @@ func (h *dockerContainerHandler) GetSpec() (info.ContainerSpec, error) {
328327
// TODO(vmarmol): Get from libcontainer API instead of cgroup manager when we don't have to support older Dockers.
329328
func (h *dockerContainerHandler) GetStats() (*info.ContainerStats, error) {
330329
stats, err := h.libcontainerHandler.GetStats()
331-
332-
stats.Health.Status = h.healthStatus
333-
334330
if err != nil {
335331
return stats, err
336332
}
337333

334+
// We assume that if Inspect fails then the container is not known to docker.
335+
ctnr, err := h.client.ContainerInspect(context.Background(), h.reference.Id)
336+
if err != nil {
337+
return nil, fmt.Errorf("failed to inspect container %q: %v", h.reference.Id, err)
338+
}
339+
340+
if ctnr.State.Health != nil {
341+
stats.Health.Status = ctnr.State.Health.Status
342+
}
343+
338344
// Get filesystem stats.
339345
err = FsStats(stats, h.machineInfoFactory, h.includedMetrics, h.storageDriver,
340346
h.fsHandler, h.fsInfo, h.poolName, h.rootfsStorageDir, h.zfsParent)

metrics/prometheus.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,12 @@ func NewPrometheusCollector(i infoProvider, f ContainerLabelsFunc, includedMetri
143143
return metricValues{{
144144
// inline if to check if s.health.status = healthy
145145
value: func(s *info.ContainerStats) float64 {
146-
if s.Health.Status == "healthy" {
146+
switch s.Health.Status {
147+
case "healthy":
147148
return 1
148-
} else {
149+
case "": // if container has no health check defined
150+
return -1
151+
default: // starting or unhealthy
149152
return 0
150153
}
151154
}(s),

0 commit comments

Comments
 (0)