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

Pass logger to CloudWatch publisher #2119

Merged
merged 2 commits into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 10 additions & 10 deletions pkg/publisher/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ var (
}
)

var log = logger.Get()

// Publisher defines the interface to publish one or more data points
type Publisher interface {
// Publish publishes one or more metric data points
Expand All @@ -82,6 +80,7 @@ type cloudWatchPublisher struct {
cloudwatchClient cloudwatchiface.CloudWatchAPI
localMetricData []*cloudwatch.MetricDatum
lock sync.RWMutex
log logger.Logger
}

// Logic to fetch Region and CLUSTER_ID
Expand All @@ -100,7 +99,7 @@ func New(ctx context.Context, region string, clusterID string, log logger.Logger
return nil, errors.Wrap(err, "publisher: unable to obtain EC2 service client")
}

clusterID = getClusterID(ec2Client)
clusterID = getClusterID(ec2Client, log)
}

// Try to fetch region if not available
Expand Down Expand Up @@ -133,25 +132,26 @@ func New(ctx context.Context, region string, clusterID string, log logger.Logger
cloudwatchClient: cloudwatchClient,
clusterID: clusterID,
localMetricData: make([]*cloudwatch.MetricDatum, 0, localMetricDataSize),
log: log,
}, nil
}

// Start is used to setup the monitor loop
func (p *cloudWatchPublisher) Start() {
log.Info("Starting monitor loop for CloudWatch publisher")
p.log.Info("Starting monitor loop for CloudWatch publisher")
p.monitor(defaultInterval)
}

// Stop is used to cancel the monitor loop
func (p *cloudWatchPublisher) Stop() {
log.Info("Stopping monitor loop for CloudWatch publisher")
p.log.Info("Stopping monitor loop for CloudWatch publisher")
p.cancel()
}

// Publish is a variadic function to publish one or more metric data points
func (p *cloudWatchPublisher) Publish(metricDataPoints ...*cloudwatch.MetricDatum) {
// Fetch dimensions for override
log.Info("Fetching CloudWatch dimensions")
p.log.Info("Fetching CloudWatch dimensions")
dimensions := p.getCloudWatchMetricDatumDimensions()

// Grab lock
Expand All @@ -175,7 +175,7 @@ func (p *cloudWatchPublisher) pushLocal() {

func (p *cloudWatchPublisher) push(metricData []*cloudwatch.MetricDatum) {
if len(metricData) == 0 {
log.Info("Missing data for publishing CloudWatch metrics")
p.log.Info("Missing data for publishing CloudWatch metrics")
return
}

Expand All @@ -190,7 +190,7 @@ func (p *cloudWatchPublisher) push(metricData []*cloudwatch.MetricDatum) {
// Publish data
err := p.send(input)
if err != nil {
log.Warnf("Unable to publish CloudWatch metrics: %v", err)
p.log.Warnf("Unable to publish CloudWatch metrics: %v", err)
}

// Mutate slice
Expand All @@ -204,7 +204,7 @@ func (p *cloudWatchPublisher) push(metricData []*cloudwatch.MetricDatum) {
}

func (p *cloudWatchPublisher) send(input cloudwatch.PutMetricDataInput) error {
log.Info("Sending data to CloudWatch metrics")
p.log.Info("Sending data to CloudWatch metrics")
_, err := p.cloudwatchClient.PutMetricData(&input)
return err
}
Expand All @@ -227,7 +227,7 @@ func (p *cloudWatchPublisher) getCloudWatchMetricNamespace() *string {
return aws.String(cloudwatchMetricNamespace)
}

func getClusterID(ec2Client *ec2wrapper.EC2Wrapper) string {
func getClusterID(ec2Client *ec2wrapper.EC2Wrapper, log logger.Logger) string {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe just make this a method function of cloudWatchPublisher if it's not used elsewhere

var clusterID string
var err error
for _, tag := range clusterIDTags {
Expand Down
22 changes: 14 additions & 8 deletions pkg/publisher/publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ const (
)

func TestCloudWatchPublisherWithNoIMDS(t *testing.T) {
logConfig := logger.Configuration{
LogLevel: "Debug",
LogLocation: "stdout",
}
log := logger.New(&logConfig)
log := getCloudWatchLog()
ctx, cancel := context.WithCancel(context.TODO())
defer cancel()

Expand Down Expand Up @@ -150,6 +146,7 @@ func TestCloudWatchPublisherWithSingleDatumWithError(t *testing.T) {
cloudwatchClient: mockCloudWatch,
clusterID: testClusterID,
localMetricData: make([]*cloudwatch.MetricDatum, 0, localMetricDataSize),
log: getCloudWatchLog(),
}

testCloudwatchMetricDatum := &cloudwatch.MetricDatum{
Expand Down Expand Up @@ -188,7 +185,7 @@ func TestGetCloudWatchMetricDatumDimensions(t *testing.T) {
}

func TestGetCloudWatchMetricDatumDimensionsWithMissingClusterID(t *testing.T) {
cloudwatchPublisher := &cloudWatchPublisher{}
cloudwatchPublisher := &cloudWatchPublisher{log: getCloudWatchLog()}

expectedCloudwatchDimensions := []*cloudwatch.Dimension{
{
Expand All @@ -202,7 +199,7 @@ func TestGetCloudWatchMetricDatumDimensionsWithMissingClusterID(t *testing.T) {
}

func TestPublishWithNoData(t *testing.T) {
cloudwatchPublisher := &cloudWatchPublisher{}
cloudwatchPublisher := &cloudWatchPublisher{log: getCloudWatchLog()}

testMetricDataPoints := []*cloudwatch.MetricDatum{}

Expand All @@ -211,7 +208,7 @@ func TestPublishWithNoData(t *testing.T) {
}

func TestPushWithMissingData(t *testing.T) {
cloudwatchPublisher := &cloudWatchPublisher{}
cloudwatchPublisher := &cloudWatchPublisher{log: getCloudWatchLog()}
testMetricDataPoints := []*cloudwatch.MetricDatum{}

cloudwatchPublisher.push(testMetricDataPoints)
Expand All @@ -238,6 +235,14 @@ func (m mockCloudWatchClient) PutMetricData(input *cloudwatch.PutMetricDataInput
return &cloudwatch.PutMetricDataOutput{}, m.mockPutMetricDataError
}

func getCloudWatchLog() logger.Logger {
logConfig := logger.Configuration{
LogLevel: "Debug",
LogLocation: "stdout",
}
return logger.New(&logConfig)
}

func getCloudWatchPublisher(t *testing.T) *cloudWatchPublisher {
// Setup context
derivedContext, cancel := context.WithCancel(context.TODO())
Expand All @@ -248,5 +253,6 @@ func getCloudWatchPublisher(t *testing.T) *cloudWatchPublisher {
cloudwatchClient: mockCloudWatchClient{},
clusterID: testClusterID,
localMetricData: make([]*cloudwatch.MetricDatum, 0, localMetricDataSize),
log: getCloudWatchLog(),
}
}