Skip to content

Commit a8078c0

Browse files
committed
Allow logger injection.
1 parent 3513105 commit a8078c0

File tree

3 files changed

+39
-24
lines changed

3 files changed

+39
-24
lines changed

awscommons/v2/auth.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/aws/aws-sdk-go-v2/aws"
77
"github.com/aws/aws-sdk-go-v2/config"
88
"github.com/gruntwork-io/go-commons/errors"
9+
"github.com/sirupsen/logrus"
910
)
1011

1112
const (
@@ -17,6 +18,8 @@ type Options struct {
1718
Region string
1819

1920
Context context.Context
21+
22+
Logger *logrus.Entry
2023
}
2124

2225
// NewOptions will create a new aws.Options struct that provides reasonable defaults for unspecified values.

awscommons/v2/ecs.go

Lines changed: 36 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
ecsTypes "github.com/aws/aws-sdk-go-v2/service/ecs/types"
1212
"github.com/gruntwork-io/go-commons/collections"
1313
"github.com/gruntwork-io/go-commons/errors"
14-
"github.com/gruntwork-io/go-commons/logging"
1514
"github.com/gruntwork-io/go-commons/retry"
1615
)
1716

@@ -29,8 +28,9 @@ func GetContainerInstanceArns(opts *Options, clusterName string) ([]string, erro
2928
return nil, err
3029
}
3130

32-
logger := logging.GetProjectLogger()
33-
logger.Infof("Looking up Container Instance ARNs for ECS cluster %s", clusterName)
31+
if opts.Logger != nil {
32+
opts.Logger.Debugf("Looking up Container Instance ARNs for ECS cluster %s", clusterName)
33+
}
3434

3535
input := &ecs.ListContainerInstancesInput{Cluster: aws.String(clusterName)}
3636
arns := []string{}
@@ -60,15 +60,16 @@ func StartDrainingContainerInstances(opts *Options, clusterName string, containe
6060
return err
6161
}
6262

63-
logger := logging.GetProjectLogger()
6463
batchSize := 10
6564
numBatches := int(math.Ceil(float64(len(containerInstanceArns) / batchSize)))
6665

6766
errList := NewMultipleDrainContainerInstanceErrors()
6867
for batchIdx, batchedArnList := range collections.BatchListIntoGroupsOf(containerInstanceArns, batchSize) {
6968
batchedArns := aws.StringSlice(batchedArnList)
7069

71-
logger.Infof("Putting batch %d/%d of container instances in cluster %s into DRAINING state", batchIdx, numBatches, clusterName)
70+
if opts.Logger != nil {
71+
opts.Logger.Debugf("Putting batch %d/%d of container instances in cluster %s into DRAINING state", batchIdx, numBatches, clusterName)
72+
}
7273
input := &ecs.UpdateContainerInstancesStateInput{
7374
Cluster: aws.String(clusterName),
7475
ContainerInstances: aws.ToStringSlice(batchedArns),
@@ -77,18 +78,25 @@ func StartDrainingContainerInstances(opts *Options, clusterName string, containe
7778
_, err := client.UpdateContainerInstancesState(opts.Context, input)
7879
if err != nil {
7980
errList.AddError(err)
80-
logger.Errorf("Encountered error starting to drain container instances in batch %d: %s", batchIdx, err)
81-
logger.Errorf("Container Instance ARNs: %s", strings.Join(batchedArnList, ","))
81+
if opts.Logger != nil {
82+
opts.Logger.Errorf("Encountered error starting to drain container instances in batch %d: %s", batchIdx, err)
83+
opts.Logger.Errorf("Container Instance ARNs: %s", strings.Join(batchedArnList, ","))
84+
}
8285
continue
8386
}
8487

85-
logger.Infof("Started draining %d container instances from batch %d", len(batchedArnList), batchIdx)
88+
if opts.Logger != nil {
89+
opts.Logger.Debugf("Started draining %d container instances from batch %d", len(batchedArnList), batchIdx)
90+
}
8691
}
8792

8893
if !errList.IsEmpty() {
8994
return errors.WithStackTrace(errList)
9095
}
91-
logger.Infof("Successfully started draining all %d container instances", len(containerInstanceArns))
96+
97+
if opts.Logger != nil {
98+
opts.Logger.Debugf("Successfully started draining all %d container instances", len(containerInstanceArns))
99+
}
92100
return nil
93101
}
94102

@@ -100,22 +108,25 @@ func WaitForContainerInstancesToDrain(opts *Options, clusterName string, contain
100108
return err
101109
}
102110

103-
logger := logging.GetProjectLogger()
104-
logger.Infof("Checking if all ECS Tasks have been drained from the ECS Container Instances in Cluster %s.", clusterName)
111+
if opts.Logger != nil {
112+
opts.Logger.Debugf("Checking if all ECS Tasks have been drained from the ECS Container Instances in Cluster %s.", clusterName)
113+
}
105114

106115
batchSize := 100
107116
numBatches := int(math.Ceil(float64(len(containerInstanceArns) / batchSize)))
108117

109118
err = retry.DoWithRetry(
110-
logger.Logger,
119+
opts.Logger,
111120
"Wait for Container Instances to be Drained",
112121
maxRetries, sleepBetweenRetries,
113122
func() error {
114123
responses := []*ecs.DescribeContainerInstancesOutput{}
115124
for batchIdx, batchedArnList := range collections.BatchListIntoGroupsOf(containerInstanceArns, batchSize) {
116125
batchedArns := aws.StringSlice(batchedArnList)
117126

118-
logger.Infof("Fetching description of batch %d/%d of ECS Instances in Cluster %s.", batchIdx, numBatches, clusterName)
127+
if opts.Logger != nil {
128+
opts.Logger.Debugf("Fetching description of batch %d/%d of ECS Instances in Cluster %s.", batchIdx, numBatches, clusterName)
129+
}
119130
input := &ecs.DescribeContainerInstancesInput{
120131
Cluster: aws.String(clusterName),
121132
ContainerInstances: aws.ToStringSlice(batchedArns),
@@ -134,7 +145,9 @@ func WaitForContainerInstancesToDrain(opts *Options, clusterName string, contain
134145

135146
// Yay, all done.
136147
if drained, _ := allInstancesFullyDrained(responses); drained == true {
137-
logger.Infof("All container instances have been drained in Cluster %s!", clusterName)
148+
if opts.Logger != nil {
149+
opts.Logger.Debugf("All container instances have been drained in Cluster %s!", clusterName)
150+
}
138151
return nil
139152
}
140153

@@ -181,19 +194,24 @@ func allInstancesFullyDrained(responses []*ecs.DescribeContainerInstancesOutput)
181194
}
182195

183196
func instanceFullyDrained(instance ecsTypes.ContainerInstance) bool {
184-
logger := logging.GetProjectLogger()
185197
instanceArn := instance.ContainerInstanceArn
186198

187199
if *instance.Status == "ACTIVE" {
188-
logger.Infof("The ECS Container Instance %s is still in ACTIVE status", *instanceArn)
200+
if opts.Logger != nil {
201+
opts.Logger.Debugf("The ECS Container Instance %s is still in ACTIVE status", *instanceArn)
202+
}
189203
return false
190204
}
191205
if instance.PendingTasksCount > 0 {
192-
logger.Infof("The ECS Container Instance %s still has pending tasks", *instanceArn)
206+
if opts.Logger != nil {
207+
opts.Logger.Debugf("The ECS Container Instance %s still has pending tasks", *instanceArn)
208+
}
193209
return false
194210
}
195211
if instance.RunningTasksCount > 0 {
196-
logger.Infof("The ECS Container Instance %s still has running tasks", *instanceArn)
212+
if opts.Logger != nil {
213+
opts.Logger.Debugf("The ECS Container Instance %s still has running tasks", *instanceArn)
214+
}
197215
return false
198216
}
199217

logging/logging.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ import (
99
var globalLogLevel = logrus.InfoLevel
1010
var globalLogLevelLock = sync.Mutex{}
1111

12-
// GetProjectLogger creates a new project logger
13-
func GetProjectLogger() *logrus.Entry {
14-
logger := GetLogger("")
15-
return logger.WithField("name", "go-commons")
16-
}
17-
1812
// GetLogger create a new logger with the given name
1913
func GetLogger(name string) *logrus.Logger {
2014
logger := logrus.New()

0 commit comments

Comments
 (0)