diff --git a/agent/api/statechange.go b/agent/api/statechange.go index 73915f12b54..b5df6ec361f 100644 --- a/agent/api/statechange.go +++ b/agent/api/statechange.go @@ -101,12 +101,20 @@ type AttachmentStateChange struct { Attachment *apieni.ENIAttachment } +type ErrShouldNotSendEvent struct { + resourceId string +} + +func (e ErrShouldNotSendEvent) Error() string { + return fmt.Sprintf("should not send events for internal tasks or containers: %s", e.resourceId) +} + // NewTaskStateChangeEvent creates a new task state change event // returns error if the state change doesn't need to be sent to the ECS backend. func NewTaskStateChangeEvent(task *apitask.Task, reason string) (TaskStateChange, error) { var event TaskStateChange if task.IsInternal { - return event, errors.Errorf("skip creating task stage change event for internal task %v", task.Arn) + return event, ErrShouldNotSendEvent{task.Arn} } taskKnownStatus := task.GetKnownStatus() if !taskKnownStatus.BackendRecognized() { @@ -160,9 +168,7 @@ func NewContainerStateChangeEvent(task *apitask.Task, cont *apicontainer.Contain func newUncheckedContainerStateChangeEvent(task *apitask.Task, cont *apicontainer.Container, reason string) (ContainerStateChange, error) { var event ContainerStateChange if cont.IsInternal() { - return event, errors.Errorf( - "create container state change event api: internal container: %s", - cont.Name) + return event, ErrShouldNotSendEvent{cont.Name} } portBindings := cont.GetKnownPortBindings() if task.IsServiceConnectEnabled() && task.IsNetworkModeBridge() { diff --git a/agent/engine/docker_task_engine.go b/agent/engine/docker_task_engine.go index 85a11a7d46a..20ad32323d6 100644 --- a/agent/engine/docker_task_engine.go +++ b/agent/engine/docker_task_engine.go @@ -784,10 +784,14 @@ func (engine *DockerTaskEngine) deleteTask(task *apitask.Task) { func (engine *DockerTaskEngine) emitTaskEvent(task *apitask.Task, reason string) { event, err := api.NewTaskStateChangeEvent(task, reason) if err != nil { - logger.Error("Unable to create task state change event", logger.Fields{ - field.TaskID: task.GetID(), - field.Error: err, - }) + if _, ok := err.(api.ErrShouldNotSendEvent); ok { + logger.Debug(err.Error()) + } else { + logger.Error("Unable to create task state change event", logger.Fields{ + field.TaskID: task.GetID(), + field.Error: err, + }) + } return } logger.Info("Preparing to send change event", logger.Fields{ diff --git a/agent/engine/task_manager.go b/agent/engine/task_manager.go index 277db312a92..e8a8a770ee9 100644 --- a/agent/engine/task_manager.go +++ b/agent/engine/task_manager.go @@ -615,11 +615,15 @@ func (mtask *managedTask) emitTaskEvent(task *apitask.Task, reason string) { } event, err := api.NewTaskStateChangeEvent(task, reason) if err != nil { - logger.Error("Skipping emitting event for task due to error", logger.Fields{ - field.TaskID: mtask.GetID(), - field.Reason: reason, - field.Error: err, - }) + if _, ok := err.(api.ErrShouldNotSendEvent); ok { + logger.Debug(err.Error()) + } else { + logger.Error("Skipping emitting event for task due to error", logger.Fields{ + field.TaskID: mtask.GetID(), + field.Reason: reason, + field.Error: err, + }) + } return } logger.Debug("Sending task change event", logger.Fields{ @@ -680,11 +684,15 @@ func (mtask *managedTask) emitManagedAgentEvent(task *apitask.Task, cont *apicon func (mtask *managedTask) emitContainerEvent(task *apitask.Task, cont *apicontainer.Container, reason string) { event, err := api.NewContainerStateChangeEvent(task, cont, reason) if err != nil { - logger.Debug("Skipping emitting event for container", logger.Fields{ - field.TaskID: mtask.GetID(), - field.Container: cont.Name, - field.Error: err, - }) + if _, ok := err.(api.ErrShouldNotSendEvent); ok { + logger.Debug(err.Error()) + } else { + logger.Error("Skipping emitting event for container due to error", logger.Fields{ + field.TaskID: mtask.GetID(), + field.Container: cont.Name, + field.Error: err, + }) + } return } mtask.doEmitContainerEvent(event)