make health check log more readable

Signed-off-by: allencloud <allen.sun@daocloud.io>
This commit is contained in:
allencloud 2016-09-28 14:00:46 +08:00
parent 9a2f01ef13
commit a4a4f3733f
2 changed files with 9 additions and 9 deletions

View File

@ -94,7 +94,7 @@ func (p *cmdProbe) run(ctx context.Context, d *Daemon, container *container.Cont
return nil, err return nil, err
} }
if info.ExitCode == nil { if info.ExitCode == nil {
return nil, fmt.Errorf("Healthcheck has no exit code!") return nil, fmt.Errorf("Healthcheck for container %s has no exit code!", container.ID)
} }
// Note: Go's json package will handle invalid UTF-8 for us // Note: Go's json package will handle invalid UTF-8 for us
out := output.String() out := output.String()
@ -149,17 +149,17 @@ func monitor(d *Daemon, c *container.Container, stop chan struct{}, probe probe)
for { for {
select { select {
case <-stop: case <-stop:
logrus.Debug("Stop healthcheck monitoring (received while idle)") logrus.Debugf("Stop healthcheck monitoring for container %s (received while idle)", c.ID)
return return
case <-time.After(probeInterval): case <-time.After(probeInterval):
logrus.Debug("Running health check...") logrus.Debugf("Running health check for container %s ...", c.ID)
startTime := time.Now() startTime := time.Now()
ctx, cancelProbe := context.WithTimeout(context.Background(), probeTimeout) ctx, cancelProbe := context.WithTimeout(context.Background(), probeTimeout)
results := make(chan *types.HealthcheckResult) results := make(chan *types.HealthcheckResult)
go func() { go func() {
result, err := probe.run(ctx, d, c) result, err := probe.run(ctx, d, c)
if err != nil { if err != nil {
logrus.Warnf("Health check error: %v", err) logrus.Warnf("Health check for container %s error: %v", c.ID, err)
results <- &types.HealthcheckResult{ results <- &types.HealthcheckResult{
ExitCode: -1, ExitCode: -1,
Output: err.Error(), Output: err.Error(),
@ -168,14 +168,14 @@ func monitor(d *Daemon, c *container.Container, stop chan struct{}, probe probe)
} }
} else { } else {
result.Start = startTime result.Start = startTime
logrus.Debugf("Health check done (exitCode=%d)", result.ExitCode) logrus.Debugf("Health check for container %s done (exitCode=%d)", c.ID, result.ExitCode)
results <- result results <- result
} }
close(results) close(results)
}() }()
select { select {
case <-stop: case <-stop:
logrus.Debug("Stop healthcheck monitoring (received while probing)") logrus.Debugf("Stop healthcheck monitoring for container %s (received while probing)", c.ID)
// Stop timeout and kill probe, but don't wait for probe to exit. // Stop timeout and kill probe, but don't wait for probe to exit.
cancelProbe() cancelProbe()
return return
@ -184,7 +184,7 @@ func monitor(d *Daemon, c *container.Container, stop chan struct{}, probe probe)
// Stop timeout // Stop timeout
cancelProbe() cancelProbe()
case <-ctx.Done(): case <-ctx.Done():
logrus.Debug("Health check taking too long") logrus.Debugf("Health check for container %s taking too long", c.ID)
handleProbeResult(d, c, &types.HealthcheckResult{ handleProbeResult(d, c, &types.HealthcheckResult{
ExitCode: -1, ExitCode: -1,
Output: fmt.Sprintf("Health check exceeded timeout (%v)", probeTimeout), Output: fmt.Sprintf("Health check exceeded timeout (%v)", probeTimeout),
@ -213,7 +213,7 @@ func getProbe(c *container.Container) probe {
case "CMD-SHELL": case "CMD-SHELL":
return &cmdProbe{shell: true} return &cmdProbe{shell: true}
default: default:
logrus.Warnf("Unknown healthcheck type '%s' (expected 'CMD')", config.Test[0]) logrus.Warnf("Unknown healthcheck type '%s' (expected 'CMD') in container %s", config.Test[0], c.ID)
return nil return nil
} }
} }

View File

@ -30,7 +30,7 @@ func (daemon *Daemon) StateChanged(id string, e libcontainerd.StateInfo) error {
daemon.updateHealthMonitor(c) daemon.updateHealthMonitor(c)
daemon.LogContainerEvent(c, "oom") daemon.LogContainerEvent(c, "oom")
case libcontainerd.StateExit: case libcontainerd.StateExit:
// if containers AutoRemove flag is set, remove it after clean up // if container's AutoRemove flag is set, remove it after clean up
if c.HostConfig.AutoRemove { if c.HostConfig.AutoRemove {
defer func() { defer func() {
if err := daemon.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil { if err := daemon.ContainerRm(c.ID, &types.ContainerRmConfig{ForceRemove: true, RemoveVolume: true}); err != nil {