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

For dev deployments, cloudwatch service should just log to console #149

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 41 additions & 0 deletions aws/cwatch/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cwatch

import (
"context"
"log/slog"
"strings"
"sync/atomic"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/smithy-go/middleware"
)

// Client is the interface for a Cloudwatch client that can only send metrics
type Client interface {
PutMetricData(ctx context.Context, params *cloudwatch.PutMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error)
}

type DevClient struct {
callCount atomic.Int32
}

func (c *DevClient) PutMetricData(ctx context.Context, params *cloudwatch.PutMetricDataInput, optFns ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error) {
// log each metric being "sent"
for _, md := range params.MetricData {
log := slog.With("namespace", aws.ToString(params.Namespace))

for _, dim := range md.Dimensions {
log = log.With(strings.ToLower(aws.ToString(dim.Name)), aws.ToString(dim.Value))
}
log.With("metric", aws.ToString(md.MetricName), "value", aws.ToFloat64(md.Value)).Info("put metric data")
Copy link
Member Author

Choose a reason for hiding this comment

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

2024/12/13 11:03:02 INFO put metric data namespace=Foo deployment=dev metric=NumGoats value=10
2024/12/13 11:03:02 INFO put metric data namespace=Foo deployment=dev host=foo1 metric=NumSheep value=20
2024/12/13 11:03:02 INFO put metric data namespace=Foo deployment=dev metric=NumFish value=30

}

c.callCount.Add(1)

return &cloudwatch.PutMetricDataOutput{ResultMetadata: middleware.Metadata{}}, nil
}

func (c *DevClient) CallCount() int {
return int(c.callCount.Load())
}
21 changes: 14 additions & 7 deletions aws/cwatch/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,38 @@
)

type Service struct {
Client *cloudwatch.Client
Client Client
namespace string
deployment types.Dimension
batcher *syncx.Batcher[types.MetricDatum]
}

// NewService creates a new Cloudwatch service with the given credentials and configuration
func NewService(accessKey, secretKey, region, namespace, deployment string) (*Service, error) {
cfg, err := awsx.NewConfig(accessKey, secretKey, region)
if err != nil {
return nil, err
var client Client

if deployment == "dev" {
client = &DevClient{}
} else {
cfg, err := awsx.NewConfig(accessKey, secretKey, region)
if err != nil {
return nil, err
}
client = cloudwatch.NewFromConfig(cfg)

Check warning on line 34 in aws/cwatch/service.go

View check run for this annotation

Codecov / codecov/patch

aws/cwatch/service.go#L30-L34

Added lines #L30 - L34 were not covered by tests
}

return &Service{
Client: cloudwatch.NewFromConfig(cfg),
Client: client,
namespace: namespace,
deployment: types.Dimension{Name: aws.String("Deployment"), Value: aws.String(deployment)},
}, nil
}

func (s *Service) StartQueue(wg *sync.WaitGroup) {
func (s *Service) StartQueue(wg *sync.WaitGroup, maxAge time.Duration) {
if s.batcher != nil {
panic("queue already started")
}
s.batcher = syncx.NewBatcher(s.processBatch, 100, time.Second*3, 1000, wg)
s.batcher = syncx.NewBatcher(s.processBatch, 100, maxAge, 1000, wg)
s.batcher.Start()
}

Expand Down
24 changes: 20 additions & 4 deletions aws/cwatch/service_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package cwatch_test

import (
"context"
"sync"
"testing"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
Expand All @@ -12,7 +14,7 @@ import (
)

func TestService(t *testing.T) {
svc, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "testing")
svc, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "dev")
assert.NoError(t, err)

assert.Equal(t, &cloudwatch.PutMetricDataInput{
Expand All @@ -21,14 +23,14 @@ func TestService(t *testing.T) {
{
MetricName: aws.String("NumGoats"),
Dimensions: []types.Dimension{
{Name: aws.String("Deployment"), Value: aws.String("testing")},
{Name: aws.String("Deployment"), Value: aws.String("dev")},
},
Value: aws.Float64(10),
},
{
MetricName: aws.String("NumSheep"),
Dimensions: []types.Dimension{
{Name: aws.String("Deployment"), Value: aws.String("testing")},
{Name: aws.String("Deployment"), Value: aws.String("dev")},
{Name: aws.String("Host"), Value: aws.String("foo1")},
},
Value: aws.Float64(20),
Expand All @@ -40,7 +42,21 @@ func TestService(t *testing.T) {
}))

wg := &sync.WaitGroup{}
svc.StartQueue(wg)
svc.StartQueue(wg, time.Millisecond*100)

// test writing metrics directly via the client
_, err = svc.Client.PutMetricData(context.Background(), svc.Prepare([]types.MetricDatum{
{MetricName: aws.String("NumGoats"), Value: aws.Float64(10)},
{MetricName: aws.String("NumSheep"), Dimensions: []types.Dimension{{Name: aws.String("Host"), Value: aws.String("foo1")}}, Value: aws.Float64(20)},
}))
assert.NoError(t, err)

// test queuing metrics to be sent by batching process
svc.Queue(types.MetricDatum{MetricName: aws.String("NumFish"), Value: aws.Float64(30)})

svc.StopQueue()
wg.Wait()

// check the queued metric was sent
assert.Equal(t, 2, svc.Client.(*cwatch.DevClient).CallCount())
}
Loading