-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #146 from nyaruka/cloudwatch
Add cloudwatch service with support for batching metric data
- Loading branch information
Showing
7 changed files
with
157 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/credentials" | ||
) | ||
|
||
// NewConfig creates a new AWS config with the given credentials and region | ||
func NewConfig(accessKey, secretKey, region string) (aws.Config, error) { | ||
opts := []func(*config.LoadOptions) error{config.WithRegion(region)} | ||
|
||
if accessKey != "" && secretKey != "" { | ||
opts = append(opts, config.WithCredentialsProvider(credentials.StaticCredentialsProvider{Value: aws.Credentials{ | ||
AccessKeyID: accessKey, SecretAccessKey: secretKey, | ||
}})) | ||
} | ||
|
||
return config.LoadDefaultConfig(context.TODO(), opts...) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package cwatch | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"sync" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" | ||
awsx "github.com/nyaruka/gocommon/aws" | ||
"github.com/nyaruka/gocommon/syncx" | ||
) | ||
|
||
type Service struct { | ||
Client *cloudwatch.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, wg *sync.WaitGroup) (*Service, error) { | ||
cfg, err := awsx.NewConfig(accessKey, secretKey, region) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
s := &Service{ | ||
Client: cloudwatch.NewFromConfig(cfg), | ||
namespace: namespace, | ||
deployment: types.Dimension{Name: aws.String("Deployment"), Value: aws.String(deployment)}, | ||
} | ||
s.batcher = syncx.NewBatcher(s.processBatch, 100, time.Second*3, 1000, wg) | ||
|
||
return s, nil | ||
} | ||
|
||
func (s *Service) Start() { | ||
s.batcher.Start() | ||
} | ||
|
||
func (s *Service) Stop() { | ||
s.batcher.Stop() | ||
} | ||
|
||
func (s *Service) Queue(d types.MetricDatum) { | ||
s.batcher.Queue(d) | ||
} | ||
|
||
func (s *Service) Prepare(data []types.MetricDatum) *cloudwatch.PutMetricDataInput { | ||
// add deployment as the first dimension to all metrics | ||
for i := range data { | ||
data[i].Dimensions = append([]types.Dimension{s.deployment}, data[i].Dimensions...) | ||
} | ||
|
||
return &cloudwatch.PutMetricDataInput{ | ||
Namespace: aws.String(s.namespace), | ||
MetricData: data, | ||
} | ||
} | ||
|
||
func (s *Service) processBatch(batch []types.MetricDatum) { | ||
_, err := s.Client.PutMetricData(context.TODO(), s.Prepare(batch)) | ||
if err != nil { | ||
slog.Error("error sending metrics to cloudwatch", "error", err, "count", len(batch)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package cwatch_test | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch" | ||
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types" | ||
"github.com/nyaruka/gocommon/aws/cwatch" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestService(t *testing.T) { | ||
wg := &sync.WaitGroup{} | ||
|
||
svc, err := cwatch.NewService("root", "key", "us-east-1", "Foo", "testing", wg) | ||
assert.NoError(t, err) | ||
|
||
assert.Equal(t, &cloudwatch.PutMetricDataInput{ | ||
Namespace: aws.String("Foo"), | ||
MetricData: []types.MetricDatum{ | ||
{ | ||
MetricName: aws.String("NumGoats"), | ||
Dimensions: []types.Dimension{ | ||
{Name: aws.String("Deployment"), Value: aws.String("testing")}, | ||
}, | ||
Value: aws.Float64(10), | ||
}, | ||
{ | ||
MetricName: aws.String("NumSheep"), | ||
Dimensions: []types.Dimension{ | ||
{Name: aws.String("Deployment"), Value: aws.String("testing")}, | ||
{Name: aws.String("Host"), Value: aws.String("foo1")}, | ||
}, | ||
Value: aws.Float64(20), | ||
}, | ||
}, | ||
}, 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)}, | ||
})) | ||
|
||
svc.Start() | ||
|
||
svc.Stop() | ||
|
||
wg.Wait() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters