Skip to content

Commit

Permalink
Add some reused fields to logger/field/constants.go
Browse files Browse the repository at this point in the history
  • Loading branch information
sparrc committed May 15, 2023
1 parent b64e8bd commit 55bdb5f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 44 deletions.
7 changes: 4 additions & 3 deletions agent/api/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
resourcestatus "github.com/aws/amazon-ecs-agent/agent/taskresource/status"
"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/field"

"github.com/aws/aws-sdk-go/aws"
"github.com/cihub/seelog"
Expand Down Expand Up @@ -516,11 +517,11 @@ func (c *Container) Fields() logger.Fields {
exitCode = strconv.Itoa(*c.GetKnownExitCode())
}
return logger.Fields{
"containerName": c.Name,
"containerImage": c.Image,
field.ContainerName: c.Name,
field.ContainerImage: c.Image,
"containerKnownStatus": c.GetKnownStatus().String(),
"containerDesiredStatus": c.GetDesiredStatus().String(),
"containerExitCode": exitCode,
field.ContainerExitCode: exitCode,
}
}

Expand Down
18 changes: 9 additions & 9 deletions agent/engine/docker_image_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (imageManager *dockerImageManager) RecordContainerReference(container *apic
imageInspected, err := imageManager.client.InspectImage(container.Image)
if err != nil {
fields := container.Fields()
fields["err"] = err
fields[field.Error] = err
logger.Error("Error inspecting image", fields)
return err
}
Expand Down Expand Up @@ -488,16 +488,16 @@ func (imageManager *dockerImageManager) removeNonECSImages(ctx context.Context,
continue
}
fields := logger.Fields{
"imageID": image.ImageID,
"imageSizeBytes": image.Size,
"repoTags": image.RepoTags,
field.ImageID: image.ImageID,
field.ImageSizeBytes: image.Size,
"repoTags": image.RepoTags,
}
if len(image.RepoTags) > 1 {
logger.Debug("Non-ECS image has more than one tag", fields)
for _, tag := range image.RepoTags {
err := imageManager.client.RemoveImage(ctx, tag, dockerclient.RemoveImageTimeout)
if err != nil {
logger.Error("Error removing non-ECS RepoTag", fields, logger.Fields{"err": err})
logger.Error("Error removing non-ECS RepoTag", fields, logger.Fields{field.Error: err})
} else {
logger.Info("Non-ECS image tag removed", fields, logger.Fields{"imageTag": tag})
numImagesAlreadyDeleted++
Expand All @@ -507,7 +507,7 @@ func (imageManager *dockerImageManager) removeNonECSImages(ctx context.Context,
logger.Debug("Removing non-ECS image", fields)
err := imageManager.client.RemoveImage(ctx, image.ImageID, dockerclient.RemoveImageTimeout)
if err != nil {
logger.Error("Error removing non-ECS image", fields, logger.Fields{"err": err})
logger.Error("Error removing non-ECS image", fields, logger.Fields{field.Error: err})
} else {
logger.Info("Non-ECS image removed", fields)
numImagesAlreadyDeleted++
Expand All @@ -524,14 +524,14 @@ func (imageManager *dockerImageManager) getNonECSImages(ctx context.Context) []I
for _, imageID := range r.ImageIDs {
resp, err := imageManager.client.InspectImage(imageID)
if err != nil {
logger.Error("Error inspecting non-ECS image", logger.Fields{"imageID": imageID, "err": err})
logger.Error("Error inspecting non-ECS image", logger.Fields{field.ImageID: imageID, field.Error: err})
continue
}
createTime := time.Time{}
createTime, err = time.Parse(time.RFC3339, resp.Created)
if err != nil {
logger.Warn("Error parsing the inspected non-ECS image created time.",
logger.Fields{"imageID": imageID, "err": err})
logger.Fields{field.ImageID: imageID, field.Error: err})
}
allImages = append(allImages,
ImageWithSizeID{
Expand Down Expand Up @@ -675,7 +675,7 @@ func (imageManager *dockerImageManager) deleteImage(ctx context.Context, imageID
if strings.Contains(strings.ToLower(err.Error()), imageNotFoundForDeletionError) {
logger.Error(fmt.Sprintf("Image already removed from the instance: %v", imageID), imageState.Fields())
} else {
logger.Error(fmt.Sprintf("Error removing Image %v", imageID), imageState.Fields(), logger.Fields{"err": err})
logger.Error(fmt.Sprintf("Error removing Image %v", imageID), imageState.Fields(), logger.Fields{field.Error: err})
delete(imageManager.imageStatesConsideredForDeletion, imageState.Image.ImageID)
return
}
Expand Down
14 changes: 7 additions & 7 deletions agent/engine/image/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ func (imageState *ImageState) Fields() logger.Fields {
}

return logger.Fields{
"imageID": imageID,
"imageNames": names,
"imageSizeBytes": size,
"referencedBy": len(imageState.Containers),
"imagePulledAt": imageState.PulledAt.UTC().Format(time.RFC3339),
"imageLastUsedAt": imageState.LastUsedAt.UTC().Format(time.RFC3339),
"imagePullSucceeded": imageState.PullSucceeded,
field.ImageID: imageID,
field.ImageNames: names,
field.ImageSizeBytes: size,
"referencedBy": len(imageState.Containers),
field.ImagePulledAt: imageState.PulledAt.UTC().Format(time.RFC3339),
field.ImageLastUsedAt: imageState.LastUsedAt.UTC().Format(time.RFC3339),
field.ImagePullSucceeded: imageState.PullSucceeded,
}
}
59 changes: 34 additions & 25 deletions ecs-agent/logger/field/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,38 @@
package field

const (
TaskID = "task"
TaskARN = "taskARN"
Container = "container"
DockerId = "dockerId"
ManagedAgent = "managedAgent"
KnownStatus = "knownStatus"
KnownSent = "knownSent"
DesiredStatus = "desiredStatus"
SentStatus = "sentStatus"
FailedStatus = "failedStatus"
Sequence = "seqnum"
Reason = "reason"
Status = "status"
RuntimeID = "runtimeID"
Elapsed = "elapsed"
Resource = "resource"
Error = "error"
Event = "event"
Image = "image"
Volume = "volume"
Time = "time"
NetworkMode = "networkMode"
Cluster = "cluster"
ServiceName = "ServiceName"
TaskProtection = "TaskProtection"
TaskID = "task"
TaskARN = "taskARN"
Container = "container"
DockerId = "dockerId"
ManagedAgent = "managedAgent"
KnownStatus = "knownStatus"
KnownSent = "knownSent"
DesiredStatus = "desiredStatus"
SentStatus = "sentStatus"
FailedStatus = "failedStatus"
Sequence = "seqnum"
Reason = "reason"
Status = "status"
RuntimeID = "runtimeID"
Elapsed = "elapsed"
Resource = "resource"
Error = "error"
Event = "event"
Image = "image"
Volume = "volume"
Time = "time"
NetworkMode = "networkMode"
Cluster = "cluster"
ServiceName = "ServiceName"
TaskProtection = "TaskProtection"
ImageID = "imageID"
ImageNames = "imageNames"
ImageSizeBytes = "imageSizeBytes"
ImagePulledAt = "imagePulledAt"
ImageLastUsedAt = "imageLastUsedAt"
ImagePullSucceeded = "imagePullSucceeded"
ContainerName = "containerName"
ContainerImage = "containerImage"
ContainerExitCode = "containerExitCode"
)

0 comments on commit 55bdb5f

Please sign in to comment.