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

Migrate to aws sdk 2 for cloudwatch/aws cloud provider #730

Merged
merged 15 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ $(TOOLS_DIR):
rm protoc-$(PROTOBUF_VERSION)-linux-x86_64.zip

pb/gostatsd.pb.go: pb/gostatsd.proto .tools/bin/protoc
GOPATH="$(TOOLS_DIR):$(shell go env GOPATH)/bin" $(TOOLS_DIR)/protoc --go_out=.\
GOPATH="$(TOOLS_DIR):$(shell go env GOPATH)/bin" $(TOOLS_DIR)/bin/protoc --go_out=.\
--go_opt=paths=source_relative $<

build:
Expand Down Expand Up @@ -73,7 +73,7 @@ test-all: check-fmt cover test-race bench bench-race check
test-all-full: check-fmt cover test-race-full bench-full bench-race-full check

check-fmt: $(TOOLS_DIR)/goimports
@# Since gofmt and goimports dont return 1 on chamges, this !() stuff will trigger a build failure if theres any problems.
@# Since gofmt and goimports dont return 1 on changes, this !() stuff will trigger a build failure if theres any problems.
! (gofmt -l -s $$(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./pb/*") | grep .)
! ($(TOOLS_DIR)/goimports -l -local github.com/atlassian/gostatsd $$(find . -type f -name '*.go' -not -path "./vendor/*" -not -path "./pb/*") | grep .)

Expand Down Expand Up @@ -196,7 +196,7 @@ version:

clean:
rm -rf bin/*
-docker rm $(docker ps -a -f 'status=exited' -q)
-docker rm $(docker ps -a -f 'status=exited' -q)
-docker rmi $(docker images -f 'dangling=true' -q)

.PHONY: build
20 changes: 18 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.4
require (
github.com/alicebob/miniredis/v2 v2.23.0
github.com/ash2k/stager v0.0.0-20170622123058-6e9c7b0eacd4
github.com/aws/aws-sdk-go v1.44.126
github.com/aws/aws-sdk-go-v2 v1.32.3 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/go-redis/redis/v8 v8.11.5
github.com/gorilla/mux v1.8.0
Expand All @@ -32,12 +32,28 @@ require (
stathat.com/c/consistent v1.0.0
)

require golang.org/x/sync v0.6.0
require (
github.com/aws/aws-sdk-go-v2/config v1.26.2
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.42.3
github.com/aws/aws-sdk-go-v2/service/ec2 v1.187.0
golang.org/x/sync v0.6.0
)

require (
github.com/PuerkitoBio/purell v1.1.1 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/alicebob/gopher-json v0.0.0-20200520072559-a9ecdc9d1d3a // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.16.13 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.22 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.22 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.3 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 // indirect
github.com/aws/smithy-go v1.22.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
Expand Down
34 changes: 30 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,36 @@ github.com/alicebob/miniredis/v2 v2.23.0 h1:+lwAJYjvvdIVg6doFHuotFjueJ/7KY10xo/v
github.com/alicebob/miniredis/v2 v2.23.0/go.mod h1:XNqvJdQJv5mSuVMc0ynneafpnL/zv52acZ6kqeS0t88=
github.com/ash2k/stager v0.0.0-20170622123058-6e9c7b0eacd4 h1:pG7CUDQmAqAxVv4smDHWTtorVUI5B7aOcFDfgqtZuWA=
github.com/ash2k/stager v0.0.0-20170622123058-6e9c7b0eacd4/go.mod h1:20N8GhJtHSLeRJvNhy5D1SnEHni4Xlt6p13JQMHYdDY=
github.com/aws/aws-sdk-go v1.44.126 h1:7HQJw2DNiwpxqMe2H7odGNT2rhO4SRrUe5/8dYXl0Jk=
github.com/aws/aws-sdk-go v1.44.126/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go-v2 v1.32.3 h1:T0dRlFBKcdaUPGNtkBSwHZxrtis8CQU17UpNBZYd0wk=
github.com/aws/aws-sdk-go-v2 v1.32.3/go.mod h1:2SK5n0a2karNTv5tbP1SjsX0uhttou00v/HpXKM1ZUo=
github.com/aws/aws-sdk-go-v2/config v1.26.2 h1:+RWLEIWQIGgrz2pBPAUoGgNGs1TOyF4Hml7hCnYj2jc=
github.com/aws/aws-sdk-go-v2/config v1.26.2/go.mod h1:l6xqvUxt0Oj7PI/SUXYLNyZ9T/yBPn3YTQcJLLOdtR8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.13 h1:WLABQ4Cp4vXtXfOWOS3MEZKr6AAYUpMczLhgKtAjQ/8=
github.com/aws/aws-sdk-go-v2/credentials v1.16.13/go.mod h1:Qg6x82FXwW0sJHzYruxGiuApNo31UEtJvXVSZAXeWiw=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10 h1:w98BT5w+ao1/r5sUuiH6JkVzjowOKeOJRHERyy1vh58=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.14.10/go.mod h1:K2WGI7vUvkIv1HoNbfBA1bvIZ+9kL3YVmWxeKuLQsiw=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.22 h1:Jw50LwEkVjuVzE1NzkhNKkBf9cRN7MtE1F/b2cOKTUM=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.22/go.mod h1:Y/SmAyPcOTmpeVaWSzSKiILfXTVJwrGmYZhcRbhWuEY=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.22 h1:981MHwBaRZM7+9QSR6XamDzF/o7ouUGxFzr+nVSIhrs=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.22/go.mod h1:1RA1+aBEfn+CAB/Mh0MB6LsdCYCnjZm7tKXtnk499ZQ=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2 h1:GrSw8s0Gs/5zZ0SX+gX4zQjRnRsMJDJ2sLur1gRBhEM=
github.com/aws/aws-sdk-go-v2/internal/ini v1.7.2/go.mod h1:6fQQgfuGmw8Al/3M2IgIllycxV7ZW7WCdVSqfBeUiCY=
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.42.3 h1:C6oS3hSFIB1ydz3dhgkZ0HyzWV41qVjNxS/mA0AGLMQ=
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.42.3/go.mod h1:OXYzq1k1XwhwghGdHASEDeFr0Ij8dyFRaIy6w0yrIms=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.187.0 h1:cA4hWo269CN5RY7Arqt8BfzXF0KIN8DSNo/KcqHKkWk=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.187.0/go.mod h1:ossaD9Z1ugYb6sq9QIqQLEOorCGcqUoxlhud9M9yE70=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0 h1:TToQNkvGguu209puTojY/ozlqy2d/SFNcoLIqTFi42g=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.0/go.mod h1:0jp+ltwkf+SwG2fm/PKo8t4y8pJSgOCO4D8Lz3k0aHQ=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.3 h1:qcxX0JYlgWH3hpPUnd6U0ikcl6LLA9sLkXE2w1fpMvY=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.3/go.mod h1:cLSNEmI45soc+Ef8K/L+8sEA3A3pYFEYf5B5UI+6bH4=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5 h1:ldSFWz9tEHAwHNmjx2Cvy1MjP5/L9kNoR0skc6wyOOM=
github.com/aws/aws-sdk-go-v2/service/sso v1.18.5/go.mod h1:CaFfXLYL376jgbP7VKC96uFcU8Rlavak0UlAwk1Dlhc=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5 h1:2k9KmFawS63euAkY4/ixVNsYYwrwnd5fIvgEKkfZFNM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.21.5/go.mod h1:W+nd4wWDVkSUIox9bacmkBP5NMFQeTJ/xqNabpzSR38=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6 h1:HJeiuZ2fldpd0WqngyMR6KW7ofkXNLyOaHwEIGm39Cs=
github.com/aws/aws-sdk-go-v2/service/sts v1.26.6/go.mod h1:XX5gh4CB7wAs4KhcF46G6C8a2i7eupU19dcAAE+EydU=
github.com/aws/smithy-go v1.22.0 h1:uunKnWlcoL3zO7q+gG2Pk53joueEOsnNB28QdMsmiMM=
github.com/aws/smithy-go v1.22.0/go.mod h1:irrKGvNn1InZwb2d7fkIRNucdfwR8R+Ts3wxYa/cJHg=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -377,7 +405,6 @@ golang.org/x/net v0.0.0-20201209123823-ac852fbbde11/go.mod h1:m0MpNAwzfU5UDzcl9v
golang.org/x/net v0.0.0-20201224014010-6772e930b67b/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
Expand Down Expand Up @@ -442,7 +469,6 @@ golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
35 changes: 18 additions & 17 deletions pkg/backends/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"

Expand All @@ -26,11 +25,15 @@ const MAX_DIMENSIONS = 10
// BackendName is the name of this backend.
const BackendName = "cloudwatch"

type CloudwatchClient interface {
PutMetricData(context.Context, *cloudwatch.PutMetricDataInput, ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error)
}

// Client is an object that is used to send messages to AWS CloudWatch.
type Client struct {
logger logrus.FieldLogger

cloudwatch cloudwatchiface.CloudWatchAPI
cloudwatch CloudwatchClient
namespace string

disabledSubtypes gostatsd.TimerSubtypes
Expand All @@ -57,25 +60,23 @@ func NewClient(namespace, transport string, disabled gostatsd.TimerSubtypes, log
if err != nil {
return nil, err
}
sess, err := session.NewSession(&aws.Config{
HTTPClient: httpClient.Client,
})
cfg, err := config.LoadDefaultConfig(context.Background(), config.WithHTTPClient(httpClient.Client))
if err != nil {
return nil, err
}

return &Client{
logger: logger,

cloudwatch: cloudwatch.New(sess),
cloudwatch: cloudwatch.NewFromConfig(cfg),
namespace: namespace,

disabledSubtypes: disabled,
}, nil
}

func (client *Client) extractDimensions(tags gostatsd.Tags) (dimensions []*cloudwatch.Dimension) {
dimensions = []*cloudwatch.Dimension{}
func (client *Client) extractDimensions(tags gostatsd.Tags) (dimensions []types.Dimension) {
dimensions = []types.Dimension{}

for _, tag := range tags {
key := tag
Expand All @@ -87,7 +88,7 @@ func (client *Client) extractDimensions(tags gostatsd.Tags) (dimensions []*cloud
value = segments[1]
}

dimensions = append(dimensions, &cloudwatch.Dimension{
dimensions = append(dimensions, types.Dimension{
Name: &key,
Value: &value,
})
Expand All @@ -106,21 +107,21 @@ func (client *Client) extractDimensions(tags gostatsd.Tags) (dimensions []*cloud
return dimensions
}

func (client *Client) buildMetricData(metrics *gostatsd.MetricMap) (metricData []*cloudwatch.MetricDatum) {
func (client *Client) buildMetricData(metrics *gostatsd.MetricMap) (metricData []types.MetricDatum) {
disabled := client.disabledSubtypes

metricData = []*cloudwatch.MetricDatum{}
metricData = []types.MetricDatum{}
now := time.Now()
prefix := ""

addMetricData := func(key string, unit string, value float64, tags gostatsd.Tags) {
dimensions := client.extractDimensions(tags)
key = prefix + key

metricData = append(metricData, &cloudwatch.MetricDatum{
metricData = append(metricData, types.MetricDatum{
MetricName: &key,
Timestamp: &now,
Unit: &unit,
Unit: types.StandardUnit(unit),
Value: &value,
Dimensions: dimensions,
})
Expand Down Expand Up @@ -224,7 +225,7 @@ func (client *Client) SendMetricsAsync(ctx context.Context, metrics *gostatsd.Me
data := metricData[start:end]
start = end

_, err := api.PutMetricData(&cloudwatch.PutMetricDataInput{
_, err := api.PutMetricData(ctx, &cloudwatch.PutMetricDataInput{
MetricData: data,
Namespace: &client.namespace,
})
Expand Down
16 changes: 8 additions & 8 deletions pkg/backends/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"math"
"testing"

"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
Expand All @@ -17,12 +17,12 @@ import (
)

type mockedCloudwatch struct {
cloudwatchiface.CloudWatchAPI
CloudwatchClient

PutMetricDataHandler func(*cloudwatch.PutMetricDataInput) (*cloudwatch.PutMetricDataOutput, error)
}

func (m *mockedCloudwatch) PutMetricData(input *cloudwatch.PutMetricDataInput) (*cloudwatch.PutMetricDataOutput, error) {
func (m *mockedCloudwatch) PutMetricData(_ context.Context, input *cloudwatch.PutMetricDataInput, _ ...func(*cloudwatch.Options)) (*cloudwatch.PutMetricDataOutput, error) {
return m.PutMetricDataHandler(input)
}

Expand Down Expand Up @@ -64,7 +64,7 @@ func TestSendMetrics(t *testing.T) {

for idx, row := range expected {
assert.Equal(t, row.Name, *input.MetricData[idx].MetricName)
assert.Equal(t, row.Unit, *input.MetricData[idx].Unit)
assert.Equal(t, types.StandardUnit(row.Unit), input.MetricData[idx].Unit)
assert.Equal(t, row.Value, *input.MetricData[idx].Value)
}

Expand Down Expand Up @@ -240,15 +240,15 @@ func TestSendHistogram(t *testing.T) {
}
}

func findMetricDatum(input *cloudwatch.PutMetricDataInput, name string, dimenensionName string, dimensionValue string) *cloudwatch.MetricDatum {
func findMetricDatum(input *cloudwatch.PutMetricDataInput, name string, dimensionName string, dimensionValue string) *types.MetricDatum {
akavatl marked this conversation as resolved.
Show resolved Hide resolved
for _, data := range input.MetricData {
MetricName := *data.MetricName
if MetricName != name {
continue
}
for _, dimension := range data.Dimensions {
if *dimension.Name == dimenensionName && *dimension.Value == dimensionValue {
return data
if *dimension.Name == dimensionName && *dimension.Value == dimensionValue {
return &data
}
}
}
Expand Down
Loading
Loading