Skip to content

Commit 64da854

Browse files
committed
Remove no longer used batching/queuing functionality from cloudwatch service
1 parent 8dc29fe commit 64da854

File tree

2 files changed

+4
-74
lines changed

2 files changed

+4
-74
lines changed

aws/cwatch/service.go

+2-46
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,21 @@ package cwatch
22

33
import (
44
"context"
5-
"log/slog"
6-
"sync"
7-
"time"
85

96
"github.com/aws/aws-sdk-go-v2/aws"
107
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
118
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
129
awsx "github.com/nyaruka/gocommon/aws"
13-
"github.com/nyaruka/gocommon/syncx"
1410
)
1511

1612
type Service struct {
1713
Client Client
1814
namespace string
1915
deployment string
20-
21-
batcher *syncx.Batcher[types.MetricDatum]
22-
batcherWG *sync.WaitGroup
2316
}
2417

25-
// NewService creates a new Cloudwatch service with the given credentials and configuration. Some behaviours depend on
26-
// the given deployment value:
27-
// - "test": metrics just logged, Queue(..) sends synchronously
28-
// - "dev": metrics just logged, Queue(..) adds to batcher
29-
// - "*": metrics sent to Cloudwatch, Queue(..) adds to batcher
18+
// NewService creates a new Cloudwatch service with the given credentials and configuration. If deployment is given as
19+
// "dev" or "test", then metrics are logged and not sent to Cloudwatch.
3020
func NewService(accessKey, secretKey, region, namespace, deployment string) (*Service, error) {
3121
var client Client
3222

@@ -43,34 +33,6 @@ func NewService(accessKey, secretKey, region, namespace, deployment string) (*Se
4333
return &Service{Client: client, namespace: namespace, deployment: deployment}, nil
4434
}
4535

46-
func (s *Service) StartQueue(maxAge time.Duration) {
47-
if s.batcher != nil {
48-
panic("queue already started")
49-
}
50-
51-
s.batcherWG = &sync.WaitGroup{}
52-
s.batcher = syncx.NewBatcher(s.processBatch, 100, maxAge, 1000, s.batcherWG)
53-
s.batcher.Start()
54-
}
55-
56-
func (s *Service) StopQueue() {
57-
if s.batcher == nil {
58-
panic("queue wasn't started")
59-
}
60-
s.batcher.Stop()
61-
s.batcherWG.Wait()
62-
}
63-
64-
func (s *Service) Queue(data ...types.MetricDatum) {
65-
if s.deployment == "test" {
66-
s.Send(context.TODO(), data...)
67-
} else {
68-
for _, d := range data {
69-
s.batcher.Queue(d)
70-
}
71-
}
72-
}
73-
7436
func (s *Service) Send(ctx context.Context, data ...types.MetricDatum) error {
7537
_, err := s.Client.PutMetricData(ctx, s.prepare(data))
7638
return err
@@ -87,9 +49,3 @@ func (s *Service) prepare(data []types.MetricDatum) *cloudwatch.PutMetricDataInp
8749
MetricData: data,
8850
}
8951
}
90-
91-
func (s *Service) processBatch(batch []types.MetricDatum) {
92-
if err := s.Send(context.TODO(), batch...); err != nil {
93-
slog.Error("error sending metric data batch", "error", err, "count", len(batch))
94-
}
95-
}

aws/cwatch/service_test.go

+2-28
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cwatch_test
33
import (
44
"context"
55
"testing"
6-
"time"
76

87
"github.com/aws/aws-sdk-go-v2/aws"
98
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
@@ -12,39 +11,14 @@ import (
1211
)
1312

1413
func TestService(t *testing.T) {
15-
// create service for test environment
16-
svc, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "test")
14+
// create service for dev environment
15+
svc, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "dev")
1716
assert.NoError(t, err)
1817

1918
err = svc.Send(context.Background(), types.MetricDatum{MetricName: aws.String("NumSheep"), Dimensions: []types.Dimension{{Name: aws.String("Host"), Value: aws.String("foo1")}}, Value: aws.Float64(20)})
2019
assert.NoError(t, err)
2120
assert.Equal(t, 1, svc.Client.(*cwatch.DevClient).CallCount())
2221

23-
// check Queue sends synchronously
24-
svc.Queue(types.MetricDatum{MetricName: aws.String("NumGoats"), Value: aws.Float64(10), Unit: types.StandardUnitCount})
25-
assert.Equal(t, 2, svc.Client.(*cwatch.DevClient).CallCount())
26-
27-
// create service for dev environment
28-
svc, err = cwatch.NewService("root", "key", "us-east-1", "Foo", "dev")
29-
assert.NoError(t, err)
30-
31-
svc.StartQueue(time.Millisecond * 100)
32-
33-
svc.Queue(cwatch.Datum("NumGoats", 10, types.StandardUnitCount, cwatch.Dimension("Host", "foo1")))
34-
svc.Queue(cwatch.Datum("NumSheep", 20, types.StandardUnitCount))
35-
assert.Equal(t, 0, svc.Client.(*cwatch.DevClient).CallCount()) // not sent yet
36-
37-
time.Sleep(time.Millisecond * 200)
38-
39-
assert.Equal(t, 1, svc.Client.(*cwatch.DevClient).CallCount()) // sent as one call
40-
41-
svc.Queue(cwatch.Datum("SleepTime", 30, types.StandardUnitSeconds))
42-
43-
svc.StopQueue()
44-
45-
// check the queued metric was sent
46-
assert.Equal(t, 2, svc.Client.(*cwatch.DevClient).CallCount())
47-
4822
// create service for prod environment
4923
svc, err = cwatch.NewService("root", "key", "us-east-1", "Foo", "prod")
5024
assert.NoError(t, err)

0 commit comments

Comments
 (0)