Skip to content

Commit 7d84436

Browse files
committed
Migrate CloudWatch logs output plugin to SDKv2 (#1989)
1 parent b4fae2d commit 7d84436

40 files changed

Lines changed: 542 additions & 31261 deletions

cfg/aws/v2/credentials.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ package aws
66
import (
77
"context"
88
"log"
9-
"net/http"
109
"os"
1110
"time"
1211

1312
"github.com/aws/aws-sdk-go-v2/aws"
13+
"github.com/aws/aws-sdk-go-v2/aws/transport/http"
1414
"github.com/aws/aws-sdk-go-v2/config"
1515
"github.com/aws/aws-sdk-go-v2/credentials"
1616
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
@@ -52,7 +52,7 @@ func (c *CredentialsConfig) loadConfig(ctx context.Context, provider aws.Credent
5252
log.Printf("D! Fallback shared config file(s): %v", cfgFiles)
5353
opts := []func(*config.LoadOptions) error{
5454
config.WithRegion(c.Region),
55-
config.WithHTTPClient(&http.Client{Timeout: 1 * time.Minute}),
55+
config.WithHTTPClient(http.NewBuildableClient().WithTimeout(1 * time.Minute)),
5656
config.WithClientLogMode(SDKLogLevel()),
5757
config.WithLogger(SDKLogger{}),
5858
config.WithSharedCredentialsFiles(cfgFiles),

cfg/aws/v2/sts.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,10 @@ func newStsClient(cfg aws.Config) stscreds.AssumeRoleAPIClient {
8181
if sourceAccount != "" && sourceArn != "" {
8282
options = append(options, func(o *sts.Options) {
8383
o.APIOptions = append(o.APIOptions, func(s *smithymiddleware.Stack) error {
84-
return s.Finalize.Add(&middleware.CustomHeaderFinalizeMiddleware{
85-
Name: "ConfusedDeputyHeaders",
86-
Headers: map[string]string{
87-
SourceArnHeaderKey: sourceArn,
88-
SourceAccountHeaderKey: sourceAccount,
89-
},
90-
}, smithymiddleware.Before)
84+
return s.Build.Add(middleware.NewCustomHeaderMiddleware("ConfusedDeputyHeaders", map[string]string{
85+
SourceArnHeaderKey: sourceArn,
86+
SourceAccountHeaderKey: sourceAccount,
87+
}), smithymiddleware.Before)
9188
})
9289
})
9390
}

extension/entitystore/extension.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"context"
88
"time"
99

10+
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
1011
"github.com/aws/aws-sdk-go/aws"
1112
"github.com/aws/aws-sdk-go/aws/client"
1213
"github.com/aws/aws-sdk-go/service/ec2"
@@ -21,7 +22,6 @@ import (
2122
"github.com/aws/amazon-cloudwatch-agent/internal/ec2metadataprovider"
2223
"github.com/aws/amazon-cloudwatch-agent/internal/retryer"
2324
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsentity/entityattributes"
24-
"github.com/aws/amazon-cloudwatch-agent/sdk/service/cloudwatchlogs"
2525
"github.com/aws/amazon-cloudwatch-agent/translator/config"
2626
)
2727

@@ -147,7 +147,7 @@ func (e *EntityStore) NativeCredentialExists() bool {
147147
}
148148

149149
// CreateLogFileEntity creates the entity for log events that are being uploaded from a log file in the environment.
150-
func (e *EntityStore) CreateLogFileEntity(logFileGlob LogFileGlob, logGroupName LogGroupName) *cloudwatchlogs.Entity {
150+
func (e *EntityStore) CreateLogFileEntity(logFileGlob LogFileGlob, logGroupName LogGroupName) *types.Entity {
151151
if e.serviceprovider == nil {
152152
return nil
153153
}
@@ -159,7 +159,7 @@ func (e *EntityStore) CreateLogFileEntity(logFileGlob LogFileGlob, logGroupName
159159
if _, ok := keyAttributes[entityattributes.AwsAccountId]; !ok {
160160
return nil
161161
}
162-
return &cloudwatchlogs.Entity{
162+
return &types.Entity{
163163
KeyAttributes: keyAttributes,
164164
Attributes: attributeMap,
165165
}
@@ -175,7 +175,7 @@ func (e *EntityStore) GetMetricServiceNameAndSource() (string, string) {
175175

176176
// GetServiceMetricAttributesMap creates the attribute map for service metrics. This will be expanded upon in a later PR'S,
177177
// but for now is just covering the EC2 attributes for service metrics.
178-
func (e *EntityStore) GetServiceMetricAttributesMap() map[string]*string {
178+
func (e *EntityStore) GetServiceMetricAttributesMap() map[string]string {
179179
return e.createAttributeMap()
180180
}
181181

@@ -235,24 +235,24 @@ func (e *EntityStore) GetPodServiceEnvironmentMapping() *ttlcache.Cache[string,
235235
)
236236
}
237237

238-
func (e *EntityStore) createAttributeMap() map[string]*string {
239-
attributeMap := make(map[string]*string)
238+
func (e *EntityStore) createAttributeMap() map[string]string {
239+
attributeMap := make(map[string]string)
240240

241241
if e.mode == config.ModeEC2 {
242242
addNonEmptyToMap(attributeMap, InstanceIDKey, e.ec2Info.GetInstanceID())
243243
addNonEmptyToMap(attributeMap, ASGKey, e.GetAutoScalingGroup())
244244
}
245245
switch e.mode {
246246
case config.ModeEC2:
247-
attributeMap[PlatformType] = aws.String(EC2PlatForm)
247+
attributeMap[PlatformType] = EC2PlatForm
248248
}
249249
return attributeMap
250250
}
251251

252252
// createServiceKeyAttribute creates KeyAttributes for Service entities
253-
func (e *EntityStore) createServiceKeyAttributes(serviceAttr ServiceAttribute) map[string]*string {
254-
serviceKeyAttr := map[string]*string{
255-
entityattributes.EntityType: aws.String(Service),
253+
func (e *EntityStore) createServiceKeyAttributes(serviceAttr ServiceAttribute) map[string]string {
254+
serviceKeyAttr := map[string]string{
255+
entityattributes.EntityType: Service,
256256
}
257257
addNonEmptyToMap(serviceKeyAttr, entityattributes.ServiceName, serviceAttr.ServiceName)
258258
addNonEmptyToMap(serviceKeyAttr, entityattributes.DeploymentEnvironment, serviceAttr.Environment)
@@ -275,8 +275,8 @@ var getEC2Provider = func(region string, ec2CredentialConfig *configaws.Credenti
275275
})
276276
}
277277

278-
func addNonEmptyToMap(m map[string]*string, key, value string) {
278+
func addNonEmptyToMap(m map[string]string, key, value string) {
279279
if value != "" {
280-
m[key] = aws.String(value)
280+
m[key] = value
281281
}
282282
}

extension/entitystore/extension_test.go

Lines changed: 38 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"testing"
1313
"time"
1414

15-
"github.com/aws/aws-sdk-go/aws"
15+
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs/types"
1616
"github.com/aws/aws-sdk-go/aws/ec2metadata"
1717
"github.com/aws/aws-sdk-go/aws/session"
1818
"github.com/jellydator/ttlcache/v3"
@@ -24,7 +24,6 @@ import (
2424

2525
"github.com/aws/amazon-cloudwatch-agent/internal/ec2metadataprovider"
2626
"github.com/aws/amazon-cloudwatch-agent/plugins/processors/awsentity/entityattributes"
27-
"github.com/aws/amazon-cloudwatch-agent/sdk/service/cloudwatchlogs"
2827
"github.com/aws/amazon-cloudwatch-agent/translator/config"
2928
)
3029

@@ -211,7 +210,7 @@ func TestEntityStore_createAttributeMaps(t *testing.T) {
211210
tests := []struct {
212211
name string
213212
fields fields
214-
want map[string]*string
213+
want map[string]string
215214
}{
216215
{
217216
name: "HappyPath",
@@ -221,10 +220,10 @@ func TestEntityStore_createAttributeMaps(t *testing.T) {
221220
},
222221
mode: config.ModeEC2,
223222
},
224-
want: map[string]*string{
225-
ASGKey: aws.String("ASG-1"),
226-
InstanceIDKey: aws.String("i-123456789"),
227-
PlatformType: aws.String(EC2PlatForm),
223+
want: map[string]string{
224+
ASGKey: "ASG-1",
225+
InstanceIDKey: "i-123456789",
226+
PlatformType: EC2PlatForm,
228227
},
229228
},
230229
{
@@ -236,9 +235,9 @@ func TestEntityStore_createAttributeMaps(t *testing.T) {
236235
mode: config.ModeEC2,
237236
emptyASG: true,
238237
},
239-
want: map[string]*string{
240-
InstanceIDKey: aws.String("i-123456789"),
241-
PlatformType: aws.String(EC2PlatForm),
238+
want: map[string]string{
239+
InstanceIDKey: "i-123456789",
240+
PlatformType: EC2PlatForm,
242241
},
243242
},
244243
{
@@ -247,8 +246,8 @@ func TestEntityStore_createAttributeMaps(t *testing.T) {
247246
mode: config.ModeEC2,
248247
emptyASG: true,
249248
},
250-
want: map[string]*string{
251-
PlatformType: aws.String(EC2PlatForm),
249+
want: map[string]string{
250+
PlatformType: EC2PlatForm,
252251
},
253252
},
254253
{
@@ -259,7 +258,7 @@ func TestEntityStore_createAttributeMaps(t *testing.T) {
259258
},
260259
mode: config.ModeOnPrem,
261260
},
262-
want: map[string]*string{},
261+
want: map[string]string{},
263262
},
264263
}
265264
for _, tt := range tests {
@@ -275,7 +274,7 @@ func TestEntityStore_createAttributeMaps(t *testing.T) {
275274
sp.On("getAutoScalingGroup").Return("ASG-1")
276275
}
277276
e.serviceprovider = sp
278-
assert.Equalf(t, dereferenceMap(tt.want), dereferenceMap(e.createAttributeMap()), "createAttributeMap()")
277+
assert.Equalf(t, tt.want, e.createAttributeMap(), "createAttributeMap()")
279278
})
280279
}
281280
}
@@ -284,38 +283,38 @@ func TestEntityStore_createServiceKeyAttributes(t *testing.T) {
284283
tests := []struct {
285284
name string
286285
serviceAttr ServiceAttribute
287-
want map[string]*string
286+
want map[string]string
288287
}{
289288
{
290289
name: "NameAndEnvironmentSet",
291290
serviceAttr: ServiceAttribute{ServiceName: "test-service", Environment: "test-environment"},
292-
want: map[string]*string{
293-
entityattributes.DeploymentEnvironment: aws.String("test-environment"),
294-
entityattributes.ServiceName: aws.String("test-service"),
295-
entityattributes.EntityType: aws.String(Service),
291+
want: map[string]string{
292+
entityattributes.DeploymentEnvironment: "test-environment",
293+
entityattributes.ServiceName: "test-service",
294+
entityattributes.EntityType: Service,
296295
},
297296
},
298297
{
299298
name: "OnlyNameSet",
300299
serviceAttr: ServiceAttribute{ServiceName: "test-service"},
301-
want: map[string]*string{
302-
entityattributes.ServiceName: aws.String("test-service"),
303-
entityattributes.EntityType: aws.String(Service),
300+
want: map[string]string{
301+
entityattributes.ServiceName: "test-service",
302+
entityattributes.EntityType: Service,
304303
},
305304
},
306305
{
307306
name: "OnlyEnvironmentSet",
308307
serviceAttr: ServiceAttribute{Environment: "test-environment"},
309-
want: map[string]*string{
310-
entityattributes.DeploymentEnvironment: aws.String("test-environment"),
311-
entityattributes.EntityType: aws.String(Service),
308+
want: map[string]string{
309+
entityattributes.DeploymentEnvironment: "test-environment",
310+
entityattributes.EntityType: Service,
312311
},
313312
},
314313
}
315314
for _, tt := range tests {
316315
t.Run(tt.name, func(t *testing.T) {
317316
e := &EntityStore{}
318-
assert.Equalf(t, dereferenceMap(tt.want), dereferenceMap(e.createServiceKeyAttributes(tt.serviceAttr)), "createServiceKeyAttributes()")
317+
assert.Equalf(t, tt.want, e.createServiceKeyAttributes(tt.serviceAttr), "createServiceKeyAttributes()")
319318
})
320319
}
321320
}
@@ -342,22 +341,22 @@ func TestEntityStore_createLogFileRID(t *testing.T) {
342341

343342
entity := e.CreateLogFileEntity(glob, group)
344343

345-
expectedEntity := cloudwatchlogs.Entity{
346-
KeyAttributes: map[string]*string{
347-
entityattributes.DeploymentEnvironment: aws.String("test-environment"),
348-
entityattributes.ServiceName: aws.String("test-service"),
349-
entityattributes.EntityType: aws.String(Service),
350-
entityattributes.AwsAccountId: aws.String(accountId),
344+
expectedEntity := types.Entity{
345+
KeyAttributes: map[string]string{
346+
entityattributes.DeploymentEnvironment: "test-environment",
347+
entityattributes.ServiceName: "test-service",
348+
entityattributes.EntityType: Service,
349+
entityattributes.AwsAccountId: accountId,
351350
},
352-
Attributes: map[string]*string{
353-
InstanceIDKey: aws.String(instanceId),
354-
ServiceNameSourceKey: aws.String(ServiceNameSourceUserConfiguration),
355-
PlatformType: aws.String(EC2PlatForm),
356-
entityattributes.AutoscalingGroup: aws.String("ASG-1"),
351+
Attributes: map[string]string{
352+
InstanceIDKey: instanceId,
353+
ServiceNameSourceKey: ServiceNameSourceUserConfiguration,
354+
PlatformType: EC2PlatForm,
355+
entityattributes.AutoscalingGroup: "ASG-1",
357356
},
358357
}
359-
assert.Equal(t, dereferenceMap(expectedEntity.KeyAttributes), dereferenceMap(entity.KeyAttributes))
360-
assert.Equal(t, dereferenceMap(expectedEntity.Attributes), dereferenceMap(entity.Attributes))
358+
assert.Equal(t, expectedEntity.KeyAttributes, entity.KeyAttributes)
359+
assert.Equal(t, expectedEntity.Attributes, entity.Attributes)
361360
}
362361

363362
func TestEntityStore_createLogFileRID_ServiceProviderIsEmpty(t *testing.T) {
@@ -375,18 +374,6 @@ func TestEntityStore_createLogFileRID_ServiceProviderIsEmpty(t *testing.T) {
375374
assert.Nil(t, entity)
376375
}
377376

378-
func dereferenceMap(input map[string]*string) map[string]string {
379-
result := make(map[string]string)
380-
for k, v := range input {
381-
if v != nil {
382-
result[k] = *v
383-
} else {
384-
result[k] = ""
385-
}
386-
}
387-
return result
388-
}
389-
390377
func TestEntityStore_addServiceAttrEntryForLogFile(t *testing.T) {
391378
sp := new(mockServiceProvider)
392379
e := EntityStore{serviceprovider: sp}

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ require (
108108
github.com/aws/aws-sdk-go-v2/config v1.32.7
109109
github.com/aws/aws-sdk-go-v2/credentials v1.19.7
110110
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.53.1
111+
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.63.1
111112
github.com/aws/aws-sdk-go-v2/service/sts v1.41.6
112113
github.com/aws/smithy-go v1.24.0
113114
github.com/bigkevmcd/go-configparser v0.0.0-20200217161103-d137835d2579
@@ -276,6 +277,7 @@ require (
276277
github.com/armon/go-metrics v0.4.1 // indirect
277278
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
278279
github.com/aws/aws-msk-iam-sasl-signer-go v1.0.1 // indirect
280+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4 // indirect
279281
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.17 // indirect
280282
github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.17 // indirect
281283
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.17 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ github.com/aws/aws-sdk-go-v2 v1.9.1/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVj
281281
github.com/aws/aws-sdk-go-v2 v1.9.2/go.mod h1:cK/D0BBs0b/oWPIcX/Z/obahJK1TT7IPVjy53i/mX/4=
282282
github.com/aws/aws-sdk-go-v2 v1.41.1 h1:ABlyEARCDLN034NhxlRUSZr4l71mh+T5KAeGh6cerhU=
283283
github.com/aws/aws-sdk-go-v2 v1.41.1/go.mod h1:MayyLB8y+buD9hZqkCW3kX1AKq07Y5pXxtgB+rRFhz0=
284-
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10 h1:zAybnyUQXIZ5mok5Jqwlf58/TFE7uvd3IAsa1aF9cXs=
285-
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.10/go.mod h1:qqvMj6gHLR/EXWZw4ZbqlPbQUyenf4h82UQUlKc+l14=
284+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4 h1:489krEF9xIGkOaaX3CE/Be2uWjiXrkCH6gUX+bZA/BU=
285+
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.4/go.mod h1:IOAPF6oT9KCsceNTvvYMNHy0+kMF8akOjeDvPENWxp4=
286286
github.com/aws/aws-sdk-go-v2/config v1.8.3/go.mod h1:4AEiLtAb8kLs7vgw2ZV3p2VZ1+hBavOc84hqxVNpCyw=
287287
github.com/aws/aws-sdk-go-v2/config v1.32.7 h1:vxUyWGUwmkQ2g19n7JY/9YL8MfAIl7bTesIUykECXmY=
288288
github.com/aws/aws-sdk-go-v2/config v1.32.7/go.mod h1:2/Qm5vKUU/r7Y+zUk/Ptt2MDAEKAfUtKc1+3U1Mo3oY=
@@ -309,8 +309,8 @@ github.com/aws/aws-sdk-go-v2/service/appconfig v1.4.2/go.mod h1:FZ3HkCe+b10uFZZk
309309
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.8.1/go.mod h1:CM+19rL1+4dFWnOQKwDc7H1KwXTz+h61oUSHyhV0b3o=
310310
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.53.1 h1:ElB5x0nrBHgQs+XcpQ1XJpSJzMFCq6fDTpT6WQCWOtQ=
311311
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.53.1/go.mod h1:Cj+LUEvAU073qB2jInKV6Y0nvHX0k7bL7KAga9zZ3jw=
312-
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.13.0 h1:NfqONXoDwWtBCnkPVz7GL/FKMo/s//TnHSwF+PjzG5c=
313-
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.13.0/go.mod h1:OFC7Rn7jyPoKtczT+TARRbxKHRmN9nyrJ2rmCc3ewuQ=
312+
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.63.1 h1:l65dmgr7tO26EcHe6WMdseRnFLoJ2nqdkPz1nJdXfaw=
313+
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.63.1/go.mod h1:wvnXh1w1pGS2UpEvPTKSjXYuxiXhuvob/IMaK2AWvek=
314314
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.14.0 h1:P+eF8PKkeaiTfN/VBe5GI3uNdhwCPVYCQxchRewJcWk=
315315
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.14.0/go.mod h1:15NiwrGGBpsC7C3zScmoaqNo1QJ9SRjdM5jxEPnCUR8=
316316
github.com/aws/aws-sdk-go-v2/service/dynamodbstreams v1.4.0 h1:QbFWJr2SAyVYvyoOHvJU6sCGLnqNT94ZbWElJMEI1JY=

0 commit comments

Comments
 (0)