Skip to content

Commit

Permalink
create publisher with logger
Browse files Browse the repository at this point in the history
  • Loading branch information
jdn5126 committed Oct 26, 2022
1 parent 9bee3e4 commit 592289b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
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 {
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(),
}
}

0 comments on commit 592289b

Please sign in to comment.