Skip to content

Commit e72ee1e

Browse files
committed
upgrade aws-sdk-go-v2 check-aws-cloudwatch-logs
1 parent 0569858 commit e72ee1e

File tree

4 files changed

+71
-66
lines changed

4 files changed

+71
-66
lines changed

check-aws-cloudwatch-logs/lib/check-aws-cloudwatch-logs.go

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@ import (
1313
"strings"
1414
"time"
1515

16-
"github.com/aws/aws-sdk-go/aws"
17-
"github.com/aws/aws-sdk-go/aws/session"
18-
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
19-
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
16+
"github.com/aws/aws-sdk-go-v2/aws"
17+
"github.com/aws/aws-sdk-go-v2/config"
18+
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
2019
"github.com/jessevdk/go-flags"
2120

2221
"github.com/mackerelio/checkers"
@@ -50,15 +49,15 @@ func Do() {
5049
}
5150

5251
type awsCloudwatchLogsPlugin struct {
53-
Service cloudwatchlogsiface.CloudWatchLogsAPI
52+
Service cloudwatchlogs.FilterLogEventsAPIClient
5453
StateFile string
5554
*logOpts
5655
}
5756

58-
func newCloudwatchLogsPlugin(opts *logOpts, args []string) (*awsCloudwatchLogsPlugin, error) {
57+
func newCloudwatchLogsPlugin(ctx context.Context, opts *logOpts, args []string) (*awsCloudwatchLogsPlugin, error) {
5958
var err error
6059
p := &awsCloudwatchLogsPlugin{logOpts: opts}
61-
p.Service, err = createService(opts)
60+
p.Service, err = createService(ctx, opts)
6261
if err != nil {
6362
return nil, err
6463
}
@@ -94,20 +93,22 @@ func getStateFile(stateDir, logGroupName, logStreamNamePrefix string, args []str
9493
)
9594
}
9695

97-
func createAWSConfig(opts *logOpts) *aws.Config {
98-
conf := aws.NewConfig()
96+
func createCloudwatchlogsOptions(opts *logOpts) (optFns []func(*cloudwatchlogs.Options)) {
9997
if opts.MaxRetries > 0 {
100-
return conf.WithMaxRetries(opts.MaxRetries)
98+
optFns = append(optFns, func(o *cloudwatchlogs.Options) {
99+
o.RetryMaxAttempts = opts.MaxRetries
100+
})
101101
}
102-
return conf
102+
return
103103
}
104104

105-
func createService(opts *logOpts) (*cloudwatchlogs.CloudWatchLogs, error) {
106-
sess, err := session.NewSession()
105+
func createService(ctx context.Context, opts *logOpts) (*cloudwatchlogs.Client, error) {
106+
cfg, err := config.LoadDefaultConfig(ctx)
107107
if err != nil {
108108
return nil, err
109109
}
110-
return cloudwatchlogs.New(sess, createAWSConfig(opts)), nil
110+
111+
return cloudwatchlogs.NewFromConfig(cfg, createCloudwatchlogsOptions(opts)...), nil
111112
}
112113

113114
type logState struct {
@@ -134,9 +135,12 @@ func (p *awsCloudwatchLogsPlugin) collect(ctx context.Context, now time.Time) ([
134135
if p.LogStreamNamePrefix != "" {
135136
input.LogStreamNamePrefix = aws.String(p.LogStreamNamePrefix)
136137
}
137-
err = p.Service.FilterLogEventsPages(input, func(output *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) bool {
138-
if ctx.Err() != nil {
139-
return false
138+
139+
paginator := cloudwatchlogs.NewFilterLogEventsPaginator(p.Service, input)
140+
for paginator.HasMorePages() {
141+
output, err := paginator.NextPage(ctx)
142+
if err != nil {
143+
return nil, err
140144
}
141145
for _, event := range output.Events {
142146
messages = append(messages, *event.Message)
@@ -145,18 +149,11 @@ func (p *awsCloudwatchLogsPlugin) collect(ctx context.Context, now time.Time) ([
145149
}
146150
}
147151
s.NextToken = output.NextToken
148-
if lastPage {
149-
s.NextToken = nil
150-
}
151-
err = p.saveState(s)
152-
if err != nil {
153-
return false
152+
153+
if err = p.saveState(s); err != nil {
154+
return nil, err
154155
}
155156
time.Sleep(150 * time.Millisecond)
156-
return true
157-
})
158-
if err != nil {
159-
return nil, err
160157
}
161158
return messages, nil
162159
}
@@ -220,7 +217,7 @@ func run(ctx context.Context, args []string) *checkers.Checker {
220217
if err != nil {
221218
os.Exit(1)
222219
}
223-
p, err := newCloudwatchLogsPlugin(opts, args)
220+
p, err := newCloudwatchLogsPlugin(ctx, opts, args)
224221
if err != nil {
225222
return checkers.Unknown(fmt.Sprint(err))
226223
}

check-aws-cloudwatch-logs/lib/check-aws-cloudwatch-logs_test.go

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,41 +7,44 @@ import (
77
"context"
88
"encoding/json"
99
"fmt"
10-
"io/ioutil"
1110
"os"
11+
"path/filepath"
12+
"strconv"
1213
"testing"
1314
"time"
1415

1516
"github.com/jessevdk/go-flags"
1617
"github.com/mackerelio/checkers"
1718
"github.com/stretchr/testify/assert"
1819

19-
"github.com/aws/aws-sdk-go/aws"
20-
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
21-
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
20+
"github.com/aws/aws-sdk-go-v2/aws"
21+
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
22+
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
2223
)
2324

2425
type mockAWSCloudWatchLogsClient struct {
25-
cloudwatchlogsiface.CloudWatchLogsAPI
26+
cloudwatchlogs.FilterLogEventsAPIClient
2627
outputs []*cloudwatchlogs.FilterLogEventsOutput
2728
}
2829

29-
func (c *mockAWSCloudWatchLogsClient) FilterLogEventsPages(input *cloudwatchlogs.FilterLogEventsInput, fn func(*cloudwatchlogs.FilterLogEventsOutput, bool) bool) error {
30-
for i, output := range c.outputs {
31-
lastPage := i == len(c.outputs)-1
32-
if !fn(output, lastPage) {
33-
break
34-
}
30+
func (c *mockAWSCloudWatchLogsClient) FilterLogEvents(ctx context.Context, input *cloudwatchlogs.FilterLogEventsInput, _ ...func(*cloudwatchlogs.Options)) (*cloudwatchlogs.FilterLogEventsOutput, error) {
31+
if ctx.Err() != nil {
32+
return nil, ctx.Err()
33+
}
34+
35+
var pageNo = 0
36+
if input.NextToken != nil {
37+
pageNo, _ = strconv.Atoi(*input.NextToken)
3538
}
36-
return nil
39+
return c.outputs[pageNo], nil
3740
}
3841

39-
func createMockService() cloudwatchlogsiface.CloudWatchLogsAPI {
42+
func createMockService() cloudwatchlogs.FilterLogEventsAPIClient {
4043
return &mockAWSCloudWatchLogsClient{
4144
outputs: []*cloudwatchlogs.FilterLogEventsOutput{
4245
{
4346
NextToken: aws.String("1"),
44-
Events: []*cloudwatchlogs.FilteredLogEvent{
47+
Events: []types.FilteredLogEvent{
4548
{
4649
EventId: aws.String("event-id-0"),
4750
Message: aws.String("message-0"),
@@ -56,7 +59,7 @@ func createMockService() cloudwatchlogsiface.CloudWatchLogsAPI {
5659
},
5760
{
5861
NextToken: aws.String("2"),
59-
Events: []*cloudwatchlogs.FilteredLogEvent{
62+
Events: []types.FilteredLogEvent{
6063
{
6164
EventId: aws.String("event-id-2"),
6265
Message: aws.String("message-2"),
@@ -75,7 +78,7 @@ func createMockService() cloudwatchlogsiface.CloudWatchLogsAPI {
7578
},
7679
},
7780
{
78-
Events: []*cloudwatchlogs.FilteredLogEvent{
81+
Events: []types.FilteredLogEvent{
7982
{
8083
EventId: aws.String("event-id-5"),
8184
Message: aws.String("message-5"),
@@ -88,13 +91,11 @@ func createMockService() cloudwatchlogsiface.CloudWatchLogsAPI {
8891
}
8992

9093
func Test_cloudwatchLogsPlugin_collect(t *testing.T) {
91-
file, _ := ioutil.TempFile("", "check-cloudwatch-logs-test-collect")
92-
os.Remove(file.Name())
93-
file.Close()
94-
defer os.Remove(file.Name())
94+
stateFile := filepath.Join(t.TempDir(), "check-cloudwatch-logs-test-collect")
95+
9596
p := &awsCloudwatchLogsPlugin{
9697
Service: createMockService(),
97-
StateFile: file.Name(),
98+
StateFile: stateFile,
9899
logOpts: &logOpts{
99100
LogGroupName: "test-group",
100101
},
@@ -104,7 +105,7 @@ func Test_cloudwatchLogsPlugin_collect(t *testing.T) {
104105
messages, err := p.collect(context.Background(), time.Unix(0, 0))
105106
assert.Equal(t, err, nil, "err should be nil")
106107
assert.Equal(t, len(messages), 6)
107-
cnt, _ := ioutil.ReadFile(file.Name())
108+
cnt, _ := os.ReadFile(stateFile)
108109
var s logState
109110
json.NewDecoder(bytes.NewReader(cnt)).Decode(&s)
110111
assert.Equal(t, s, logState{StartTime: aws.Int64(5 + 1)})
@@ -115,7 +116,7 @@ func Test_cloudwatchLogsPlugin_collect(t *testing.T) {
115116
cancel()
116117

117118
messages, err := p.collect(ctx, time.Unix(0, 0))
118-
assert.Equal(t, err, nil, "err should be nil")
119+
assert.NotEqual(t, err, nil, "err should be someting")
119120
assert.Equal(t, len(messages), 0)
120121
})
121122
}
@@ -221,25 +222,32 @@ func Test_cloudwatchLogsPlugin_options(t *testing.T) {
221222
}
222223
}
223224

224-
func Test_createAWSConfig(t *testing.T) {
225+
func Test_createCloudwatchlogsOptions(t *testing.T) {
225226
tests := []struct {
226-
opts *logOpts
227-
want *aws.Config
227+
opts *logOpts
228+
want cloudwatchlogs.Options
229+
length int
228230
}{
229231
{
230-
opts: &logOpts{MaxRetries: 0},
231-
want: aws.NewConfig(),
232+
opts: &logOpts{MaxRetries: 0},
233+
length: 0,
232234
},
233235
{
234-
opts: &logOpts{MaxRetries: 1},
235-
want: aws.NewConfig().WithMaxRetries(1),
236+
opts: &logOpts{MaxRetries: 1},
237+
want: cloudwatchlogs.Options{RetryMaxAttempts: 1},
238+
length: 1,
236239
},
237240
}
238241

239242
for i, tt := range tests {
240243
t.Run(fmt.Sprintf("case:%d", i), func(t *testing.T) {
241-
res := createAWSConfig(tt.opts)
242-
assert.Equal(t, tt.want, res)
244+
res := createCloudwatchlogsOptions(tt.opts)
245+
assert.Equal(t, len(res), tt.length)
246+
if tt.length > 0 {
247+
opts := cloudwatchlogs.Options{}
248+
res[0](&opts)
249+
assert.Equal(t, tt.want, opts)
250+
}
243251
})
244252
}
245253
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ require (
1010
github.com/aws/aws-sdk-go-v2 v1.36.3
1111
github.com/aws/aws-sdk-go-v2/config v1.29.14
1212
github.com/aws/aws-sdk-go-v2/credentials v1.17.67
13+
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.47.3
1314
github.com/aws/aws-sdk-go-v2/service/sqs v1.38.5
1415
github.com/beevik/ntp v1.3.1
1516
github.com/bradfitz/gomemcache v0.0.0-20230905024940-24af94b03874
@@ -43,6 +44,7 @@ require (
4344
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
4445
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
4546
github.com/Microsoft/go-winio v0.6.1 // indirect
47+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 // indirect
4648
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.30 // indirect
4749
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
4850
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
@@ -62,7 +64,6 @@ require (
6264
github.com/go-asn1-ber/asn1-ber v1.5.5 // indirect
6365
github.com/gogo/protobuf v1.3.2 // indirect
6466
github.com/google/uuid v1.6.0 // indirect
65-
github.com/jmespath/go-jmespath v0.4.0 // indirect
6667
github.com/klauspost/compress v1.15.9 // indirect
6768
github.com/kr/text v0.2.0 // indirect
6869
github.com/moby/patternmatcher v0.6.0 // indirect

go.sum

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ github.com/aws/aws-sdk-go v1.53.14 h1:SzhkC2Pzag0iRW8WBb80RzKdGXDydJR9LAMs2GyKJ2
1818
github.com/aws/aws-sdk-go v1.53.14/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
1919
github.com/aws/aws-sdk-go-v2 v1.36.3 h1:mJoei2CxPutQVxaATCzDUjcZEjVRdpsiiXi2o38yqWM=
2020
github.com/aws/aws-sdk-go-v2 v1.36.3/go.mod h1:LLXuLpgzEbD766Z5ECcRmi8AzSwfZItDtmABVkRLGzg=
21+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 h1:zAybnyUQXIZ5mok5Jqwlf58/TFE7uvd3IAsa1aF9cXs=
22+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10/go.mod h1:qqvMj6gHLR/EXWZw4ZbqlPbQUyenf4h82UQUlKc+l14=
2123
github.com/aws/aws-sdk-go-v2/config v1.29.14 h1:f+eEi/2cKCg9pqKBoAIwRGzVb70MRKqWX4dg1BDcSJM=
2224
github.com/aws/aws-sdk-go-v2/config v1.29.14/go.mod h1:wVPHWcIFv3WO89w0rE10gzf17ZYy+UVS1Geq8Iei34g=
2325
github.com/aws/aws-sdk-go-v2/credentials v1.17.67 h1:9KxtdcIA/5xPNQyZRgUSpYOE6j9Bc4+D7nZua0KGYOM=
@@ -30,6 +32,8 @@ github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 h1:SZwFm17ZUNNg5Np0io
3032
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34/go.mod h1:dFZsC0BLo346mvKQLWmoJxT+Sjp+qcVR1tRVHQGOH9Q=
3133
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 h1:bIqFDwgGXXN1Kpp99pDOdKMTTb5d2KyU5X/BZxjOkRo=
3234
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3/go.mod h1:H5O/EsxDWyU+LP/V8i5sm8cxoZgc2fdNR9bxlOFrQTo=
35+
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.47.3 h1:3y0jkGtsaZLCg+n73BoSXOAkLFtgmD/+4prXW1pzovc=
36+
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.47.3/go.mod h1:uo14VBn5cNk/BPGTPz3kyLBxgpgOObgO8lmz+H7Z4Ck=
3337
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3 h1:eAh2A4b5IzM/lum78bZ590jy36+d/aFLgKF/4Vd1xPE=
3438
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.3/go.mod h1:0yKJC/kb8sAnmlYa6Zs3QVYqaC8ug2AbnNChv5Ox3uA=
3539
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.15 h1:dM9/92u2F1JbDaGooxTq18wmmFzbJRfXfVfy96/1CXM=
@@ -112,8 +116,6 @@ github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LF
112116
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
113117
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
114118
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
115-
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
116-
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
117119
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
118120
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
119121
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
@@ -288,9 +290,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
288290
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
289291
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
290292
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
291-
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
292-
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
293-
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
294293
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
295294
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
296295
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)