Skip to content

Commit

Permalink
Allow logger injection.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoboat committed Sep 28, 2022
1 parent 3513105 commit af3384e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 15 deletions.
3 changes: 3 additions & 0 deletions awscommons/v2/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/gruntwork-io/go-commons/errors"
"github.com/sirupsen/logrus"
)

const (
Expand All @@ -17,6 +18,8 @@ type Options struct {
Region string

Context context.Context

Logger logrus.Entry
}

// NewOptions will create a new aws.Options struct that provides reasonable defaults for unspecified values.
Expand Down
17 changes: 8 additions & 9 deletions awscommons/v2/ecs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
"github.com/gruntwork-io/go-commons/collections"
"github.com/gruntwork-io/go-commons/errors"
"github.com/gruntwork-io/go-commons/logging"
"github.com/gruntwork-io/go-commons/retry"
)

Expand All @@ -29,7 +28,7 @@ func GetContainerInstanceArns(opts *Options, clusterName string) ([]string, erro
return nil, err
}

logger := logging.GetProjectLogger()
logger := opts.Logger
logger.Infof("Looking up Container Instance ARNs for ECS cluster %s", clusterName)

input := &ecs.ListContainerInstancesInput{Cluster: aws.String(clusterName)}
Expand Down Expand Up @@ -60,7 +59,7 @@ func StartDrainingContainerInstances(opts *Options, clusterName string, containe
return err
}

logger := logging.GetProjectLogger()
logger := opts.Logger
batchSize := 10
numBatches := int(math.Ceil(float64(len(containerInstanceArns) / batchSize)))

Expand Down Expand Up @@ -100,7 +99,7 @@ func WaitForContainerInstancesToDrain(opts *Options, clusterName string, contain
return err
}

logger := logging.GetProjectLogger()
logger := opts.Logger
logger.Infof("Checking if all ECS Tasks have been drained from the ECS Container Instances in Cluster %s.", clusterName)

batchSize := 100
Expand Down Expand Up @@ -133,7 +132,7 @@ func WaitForContainerInstancesToDrain(opts *Options, clusterName string, contain
}

// Yay, all done.
if drained, _ := allInstancesFullyDrained(responses); drained == true {
if drained, _ := allInstancesFullyDrained(opts, responses); drained == true {
logger.Infof("All container instances have been drained in Cluster %s!", clusterName)
return nil
}
Expand Down Expand Up @@ -164,24 +163,24 @@ func NewECSClient(opts *Options) (*ecs.Client, error) {
return ecs.NewFromConfig(cfg), nil
}

func allInstancesFullyDrained(responses []*ecs.DescribeContainerInstancesOutput) (bool, error) {
func allInstancesFullyDrained(opts *Options, responses []*ecs.DescribeContainerInstancesOutput) (bool, error) {
for _, response := range responses {
instances := response.ContainerInstances
if len(instances) == 0 {
return false, errors.WithStackTrace(fmt.Errorf("querying DescribeContainerInstances returned no instances"))
}

for _, instance := range instances {
if !instanceFullyDrained(instance) {
if !instanceFullyDrained(opts, instance) {
return false, nil
}
}
}
return true, nil
}

func instanceFullyDrained(instance ecsTypes.ContainerInstance) bool {
logger := logging.GetProjectLogger()
func instanceFullyDrained(opts *Options, instance ecsTypes.ContainerInstance) bool {
logger := opts.Logger
instanceArn := instance.ContainerInstanceArn

if *instance.Status == "ACTIVE" {
Expand Down
6 changes: 0 additions & 6 deletions logging/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@ import (
var globalLogLevel = logrus.InfoLevel
var globalLogLevelLock = sync.Mutex{}

// GetProjectLogger creates a new project logger
func GetProjectLogger() *logrus.Entry {
logger := GetLogger("")
return logger.WithField("name", "go-commons")
}

// GetLogger create a new logger with the given name
func GetLogger(name string) *logrus.Logger {
logger := logrus.New()
Expand Down

0 comments on commit af3384e

Please sign in to comment.