From c89192cb83eecf646ad1170599f4603ab0dd6889 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 13 Dec 2024 12:10:40 +0200 Subject: [PATCH 1/2] Add CWService interface --- aws/cwatch/service.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/aws/cwatch/service.go b/aws/cwatch/service.go index 02f43db..44b1077 100644 --- a/aws/cwatch/service.go +++ b/aws/cwatch/service.go @@ -71,3 +71,11 @@ func (s *Service) processBatch(batch []types.MetricDatum) { slog.Error("error sending metrics to cloudwatch", "error", err, "count", len(batch)) } } + +type CWService interface { + Queue(d types.MetricDatum) + StartQueue(wg *sync.WaitGroup) + StopQueue() +} + +var _ CWService = (*Service)(nil) From 112a0bc4c3ee7aa79fd3191ff431920683ba08f3 Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Fri, 13 Dec 2024 13:00:04 +0200 Subject: [PATCH 2/2] Add MockCWService for testing --- aws/cwatch/mock/mock.go | 45 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 aws/cwatch/mock/mock.go diff --git a/aws/cwatch/mock/mock.go b/aws/cwatch/mock/mock.go new file mode 100644 index 0000000..01256b2 --- /dev/null +++ b/aws/cwatch/mock/mock.go @@ -0,0 +1,45 @@ +package mock + +import ( + "sync" + + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" + "github.com/nyaruka/gocommon/aws/cwatch" +) + +type MockCWService struct { + namespace string + deployment types.Dimension + Batcher []types.MetricDatum + + Stopped bool +} + +func NewMockCWService(accessKey, secretKey, region, namespace, deployment string) (*MockCWService, error) { + mockCW := MockCWService{ + namespace: namespace, + deployment: types.Dimension{Name: aws.String("Deployment"), Value: aws.String(deployment)}, + Batcher: nil, + } + + return &mockCW, nil +} + +func (s *MockCWService) Queue(d types.MetricDatum) { + if s.Stopped { + return + } + s.Batcher = append(s.Batcher, d) +} + +func (s *MockCWService) StartQueue(wg *sync.WaitGroup) { + s.Batcher = []types.MetricDatum{} + s.Stopped = false +} + +func (s *MockCWService) StopQueue() { + s.Stopped = true +} + +var _ cwatch.CWService = (*MockCWService)(nil)