Skip to content

Commit

Permalink
Add services name to containers logged while waiting for healthy (#1990)
Browse files Browse the repository at this point in the history
While investigating issues in the sql_input package I missed being able to see
what was the service that was failing to start. In the previous version the logs
only included the ID, what cannot be correlated with the service when running
in CI.

Take also the opportunity to refactor labels to use a struct instead of a map
for known labels.
  • Loading branch information
jsoriano authored Jul 30, 2024
1 parent 70ce2ee commit bce51fd
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 39 deletions.
45 changes: 18 additions & 27 deletions internal/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -369,35 +369,26 @@ func (p *Project) WaitForHealthy(ctx context.Context, opts CommandOptions) error
return err
}

for _, containerDescription := range descriptions {

for _, d := range descriptions {
switch {
// No healthcheck defined for service
if containerDescription.State.Status == "running" && containerDescription.State.Health == nil {
logger.Debugf("Container %s status: %s (no health status)", containerDescription.ID, containerDescription.State.Status)
continue
}

// Service is up and running and it's healthy
if containerDescription.State.Status == "running" && containerDescription.State.Health.Status == "healthy" {
logger.Debugf("Container %s status: %s (health: %s)", containerDescription.ID, containerDescription.State.Status, containerDescription.State.Health.Status)
continue
}

// Container started and finished with exit code 0
if containerDescription.State.Status == "exited" && containerDescription.State.ExitCode == 0 {
logger.Debugf("Container %s status: %s (exit code: %d)", containerDescription.ID, containerDescription.State.Status, containerDescription.State.ExitCode)
continue
}

// Container exited with code > 0
if containerDescription.State.Status == "exited" && containerDescription.State.ExitCode > 0 {
logger.Debugf("Container %s status: %s (exit code: %d)", containerDescription.ID, containerDescription.State.Status, containerDescription.State.ExitCode)
return fmt.Errorf("container (ID: %s) exited with code %d", containerDescription.ID, containerDescription.State.ExitCode)
}

case d.State.Status == "running" && d.State.Health == nil:
logger.Debugf("Container %s (%s) status: %s (no health status)", d.Config.Labels.ComposeService, d.ID, d.State.Status)
// Service is up and running and it's healthy
case d.State.Status == "running" && d.State.Health.Status == "healthy":
logger.Debugf("Container %s (%s) status: %s (health: %s)", d.Config.Labels.ComposeService, d.ID, d.State.Status, d.State.Health.Status)
// Container started and finished with exit code 0
case d.State.Status == "exited" && d.State.ExitCode == 0:
logger.Debugf("Container %s (%s) status: %s (exit code: %d)", d.Config.Labels.ComposeService, d.ID, d.State.Status, d.State.ExitCode)
// Container exited with code > 0
case d.State.Status == "exited" && d.State.ExitCode > 0:
logger.Debugf("Container %s (%s) status: %s (exit code: %d)", d.Config.Labels.ComposeService, d.ID, d.State.Status, d.State.ExitCode)
return fmt.Errorf("container (ID: %s) exited with code %d", d.ID, d.State.ExitCode)
// Any different status is considered unhealthy
logger.Debugf("Container %s status: unhealthy", containerDescription.ID)
healthy = false
default:
logger.Debugf("Container %s (%s) status: unhealthy", d.Config.Labels.ComposeService, d.ID)
healthy = false
}
}

// end loop before timeout if healthy
Expand Down
9 changes: 8 additions & 1 deletion internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type NetworkDescription struct {
type ContainerDescription struct {
Config struct {
Image string
Labels map[string]string
Labels ConfigLabels
}
ID string
State struct {
Expand All @@ -44,6 +44,13 @@ type ContainerDescription struct {
}
}

// ConfigLabels are the labels included in the config in container descriptions.
type ConfigLabels struct {
ComposeProject string `json:"com.docker.compose.project"`
ComposeService string `json:"com.docker.compose.service"`
ComposeVersion string `json:"com.docker.compose.version"`
}

// String function dumps string representation of the container description.
func (c *ContainerDescription) String() string {
b, err := json.Marshal(c)
Expand Down
4 changes: 1 addition & 3 deletions internal/stack/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ type ServiceStatus struct {
const readyServicesSuffix = "is_ready"

const (
// serviceLabelDockerCompose is the label with the service name created by docker-compose
serviceLabelDockerCompose = "com.docker.compose.service"
// projectLabelDockerCompose is the label with the project name created by docker-compose
projectLabelDockerCompose = "com.docker.compose.project"
)
Expand Down Expand Up @@ -213,7 +211,7 @@ func dockerComposeStatus(ctx context.Context, options Options) ([]ServiceStatus,

func newServiceStatus(description *docker.ContainerDescription) (*ServiceStatus, error) {
service := ServiceStatus{
Name: description.Config.Labels[serviceLabelDockerCompose],
Name: description.Config.Labels.ComposeService,
Status: description.State.Status,
Version: getVersionFromDockerImage(description.Config.Image),
}
Expand Down
12 changes: 6 additions & 6 deletions internal/stack/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ func TestNewServiceStatus(t *testing.T) {
description: docker.ContainerDescription{
Config: struct {
Image string
Labels map[string]string
Labels docker.ConfigLabels
}{
Image: "docker.test:1.42.0",
Labels: map[string]string{"com.docker.compose.service": "myservice", "foo": "bar"},
Labels: docker.ConfigLabels{ComposeService: "myservice"},
},
ID: "123456789ab",
State: struct {
Expand Down Expand Up @@ -85,10 +85,10 @@ func TestNewServiceStatus(t *testing.T) {
description: docker.ContainerDescription{
Config: struct {
Image string
Labels map[string]string
Labels docker.ConfigLabels
}{
Image: "docker.test:1.42.0",
Labels: map[string]string{"com.docker.compose.service": "myservice", "foo": "bar"},
Labels: docker.ConfigLabels{ComposeService: "myservice"},
},
ID: "123456789ab",
State: struct {
Expand Down Expand Up @@ -119,10 +119,10 @@ func TestNewServiceStatus(t *testing.T) {
description: docker.ContainerDescription{
Config: struct {
Image string
Labels map[string]string
Labels docker.ConfigLabels
}{
Image: "docker.test:1.42.0",
Labels: map[string]string{"com.docker.compose.service": "myservice", "foo": "bar"},
Labels: docker.ConfigLabels{ComposeService: "myservice"},
},
ID: "123456789ab",
State: struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/stack/serverless.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ func (sp *serverlessProvider) localAgentStatus() ([]ServiceStatus, error) {
func localServiceNames(project string) ([]string, error) {
services := []string{}
serviceFunc := func(description docker.ContainerDescription) error {
services = append(services, description.Config.Labels[serviceLabelDockerCompose])
services = append(services, description.Config.Labels.ComposeService)
return nil
}

Expand Down Expand Up @@ -518,7 +518,7 @@ func runOnLocalServices(project string, serviceFunc func(docker.ContainerDescrip
}

for _, containerDescription := range containerDescriptions {
serviceName := containerDescription.Config.Labels[serviceLabelDockerCompose]
serviceName := containerDescription.Config.Labels.ComposeService
if strings.HasSuffix(serviceName, readyServicesSuffix) {
continue
}
Expand Down

0 comments on commit bce51fd

Please sign in to comment.