Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom contextual loggers #2319

Merged
merged 4 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions agent/api/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ func (c *Container) GetLogDriver() string {
hostConfig := &dockercontainer.HostConfig{}
err := json.Unmarshal([]byte(*c.DockerConfig.HostConfig), hostConfig)
if err != nil {
seelog.Warnf("Encountered error when trying to get log driver for container %s: %v", err)
seelog.Warnf("Encountered error when trying to get log driver for container %s: %v", c.String(), err)
return ""
}

Expand All @@ -1021,7 +1021,7 @@ func (c *Container) GetNetworkModeFromHostConfig() string {
// TODO return error to differentiate between error and default mode .
err := json.Unmarshal([]byte(*c.DockerConfig.HostConfig), hostConfig)
if err != nil {
seelog.Warnf("Encountered error when trying to get network mode for container %s: %v", err)
seelog.Warnf("Encountered error when trying to get network mode for container %s: %v", c.String(), err)
return ""
}

Expand Down
14 changes: 14 additions & 0 deletions agent/api/task/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,17 @@ func (t *Task) MarshalJSON() ([]byte, error) {

return json.Marshal((*jTask)(t))
}

// UnmarshalJSON wraps Go's unmarshalling logic to guarantee that the logger gets created
func (t *Task) UnmarshalJSON(data []byte) error {
err := json.Unmarshal(data, (*jTask)(t))
if err != nil {
return err
}
t.log.SetContext(map[string]string{
"taskARN": t.Arn,
"taskFamily": t.Family,
"taskVersion": t.Version,
})
return nil
}
102 changes: 54 additions & 48 deletions agent/api/task/task.go

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions agent/dockerclient/dockerapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,15 @@ func (event *DockerContainerChangeEvent) String() string {

return res
}

// String returns a short human readable string of the container change event
func (event *DockerContainerChangeEvent) ShortString() string {
res := fmt.Sprintf("event type: %s, event container status: %s, docker ID: %s",
event.Type.String(), event.Status.String(), event.DockerID)

if event.ExitCode != nil {
res += fmt.Sprintf(", ExitCode: %d", aws.IntValue(event.ExitCode))
}

return res
}
2 changes: 1 addition & 1 deletion agent/engine/docker_task_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1392,7 +1392,7 @@ func (engine *DockerTaskEngine) applyContainerState(task *apitask.Task, containe
}
metadata := transitionFunction(task, container)
if metadata.Error != nil {
seelog.Infof("Task engine [%s]: error transitioning container [%s] to [%s]: %v",
seelog.Errorf("Task engine [%s]: error transitioning container [%s] to [%s]: %v",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we know full implication of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's logging a non-nil error I thought it more appropriate to be logging at the ERROR level

task.Arn, container.Name, nextState.String(), metadata.Error)
} else {
seelog.Debugf("Task engine [%s]: transitioned container [%s] to [%s]",
Expand Down
Loading