Skip to content

Commit 972801e

Browse files
committed
feat: Add user agent
1 parent d40f12f commit 972801e

5 files changed

Lines changed: 36 additions & 26 deletions

File tree

dth/client.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import (
2828
"sync"
2929

3030
"github.com/aws/aws-sdk-go-v2/aws"
31-
"github.com/aws/aws-sdk-go-v2/config"
3231
"github.com/aws/aws-sdk-go-v2/credentials"
3332
"github.com/aws/aws-sdk-go-v2/service/s3"
3433
"github.com/aws/aws-sdk-go-v2/service/s3/types"
@@ -85,13 +84,9 @@ func getEndpointURL(region, sourceType string) (url string) {
8584
// NewS3Client creates a S3Client instance
8685
func NewS3Client(ctx context.Context, bucket, prefix, endpoint, region, sourceType string, cred *S3Credentials) *S3Client {
8786

88-
// config, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
89-
config, err := config.LoadDefaultConfig(ctx)
90-
if err != nil {
91-
log.Fatalf("Failed to load default SDK config to create S3 client - %s\n", err.Error())
92-
}
87+
cfg := loadDefaultConfig(ctx)
9388

94-
client := s3.NewFromConfig(config, func(o *s3.Options) {
89+
client := s3.NewFromConfig(cfg, func(o *s3.Options) {
9590
// retryer := retry.AddWithMaxBackoffDelay(retry.NewStandard(), time.Second*5)
9691
// o.Retryer = retryer
9792
if region != "" {

dth/config.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@ limitations under the License.
1616

1717
package dth
1818

19+
import (
20+
"context"
21+
"log"
22+
23+
"github.com/aws/aws-sdk-go-v2/aws"
24+
mw "github.com/aws/aws-sdk-go-v2/aws/middleware"
25+
"github.com/aws/aws-sdk-go-v2/config"
26+
"github.com/aws/smithy-go/middleware"
27+
)
28+
1929
const (
2030
// MaxRetries when failed used globally
2131
// No need an option of this.
@@ -62,3 +72,18 @@ type JobConfig struct {
6272
SrcInCurrentAccount, DestInCurrentAccount bool
6373
*JobOptions
6474
}
75+
76+
// loadDefaultConfig reads the SDK's default external configurations with custom user agent for API tracking purpose
77+
func loadDefaultConfig(ctx context.Context) (cfg aws.Config) {
78+
cfg, err := config.LoadDefaultConfig(
79+
ctx,
80+
// config.WithClientLogMode(aws.LogRequest|aws.LogRequestWithBody),
81+
config.WithAPIOptions([]func(*middleware.Stack) error{
82+
mw.AddUserAgentKey("AWS/SO8001/v2.0.0"),
83+
}),
84+
)
85+
if err != nil {
86+
log.Fatalf("Failed to load default SDK config to create client - %s\n", err.Error())
87+
}
88+
return cfg
89+
}

dth/service.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"strconv"
2424
"time"
2525

26-
"github.com/aws/aws-sdk-go-v2/config"
2726
"github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue"
2827
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
2928
dtype "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
@@ -60,12 +59,8 @@ type SecretService struct {
6059

6160
// NewSecretService is a helper func to create a SecretService instance
6261
func NewSecretService(ctx context.Context) (*SecretService, error) {
63-
cfg, err := config.LoadDefaultConfig(ctx)
64-
if err != nil {
65-
// Don't need to quit the process
66-
log.Printf("Failed to load default SDK config to create SSM client - %s\n", err.Error())
67-
return nil, err
68-
}
62+
63+
cfg := loadDefaultConfig(ctx)
6964

7065
// Create an Amazon DynamoDB client.
7166
client := sm.NewFromConfig(cfg)
@@ -77,10 +72,8 @@ func NewSecretService(ctx context.Context) (*SecretService, error) {
7772

7873
// NewDBService is a helper func to create a DBService instance
7974
func NewDBService(ctx context.Context, tableName string) (*DBService, error) {
80-
cfg, err := config.LoadDefaultConfig(ctx)
81-
if err != nil {
82-
log.Fatalf("Failed to load default SDK config to create DynamoDB client - %s\n", err.Error())
83-
}
75+
76+
cfg := loadDefaultConfig(ctx)
8477

8578
// Create an Amazon DynamoDB client.
8679
client := dynamodb.NewFromConfig(cfg)
@@ -94,11 +87,7 @@ func NewDBService(ctx context.Context, tableName string) (*DBService, error) {
9487
// NewSqsService is a helper func to create a SqsService instance
9588
func NewSqsService(ctx context.Context, queueName string) (*SqsService, error) {
9689

97-
// Only to use default config here
98-
cfg, err := config.LoadDefaultConfig(ctx)
99-
if err != nil {
100-
log.Fatalf("Failed to load default SDK config to create SQS client - %s\n", err.Error())
101-
}
90+
cfg := loadDefaultConfig(ctx)
10291

10392
client := sqs.NewFromConfig(cfg)
10493
input := &sqs.GetQueueUrlInput{

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ require (
88
github.com/aws/aws-sdk-go-v2/credentials v1.2.1
99
github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue v1.1.1
1010
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.3.1
11-
github.com/aws/aws-sdk-go-v2/service/s3 v1.8.0
11+
github.com/aws/aws-sdk-go-v2/service/s3 v1.9.0
1212
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.3.1
1313
github.com/aws/aws-sdk-go-v2/service/sqs v1.4.1
14+
github.com/aws/smithy-go v1.4.0
1415
github.com/spf13/cobra v1.1.3
1516
github.com/spf13/viper v1.7.1
1617
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.1.1 h1:l7pDLsmOGr
4242
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.1.1/go.mod h1:2+ehJPkdIdl46VCj67Emz/EH2hpebHZtaLdzqg+sWOI=
4343
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.3.1 h1:VH1Y4k+IZ5kcRVqSNw7eAkXyfS7k2/ibKjrNtbhYhV4=
4444
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.3.1/go.mod h1:IpjxfORBAFfkMM0VEx5gPPnEy6WV4Hk0F/+zb/SUWyw=
45-
github.com/aws/aws-sdk-go-v2/service/s3 v1.8.0 h1:rljno3viFN46b59CbjkIqYwxEAzk4naLe+djOb/exLs=
46-
github.com/aws/aws-sdk-go-v2/service/s3 v1.8.0/go.mod h1:zHCjYoODbYRLz/iFicYswq1gRoxBnHvpY5h2Vg3/tJ4=
45+
github.com/aws/aws-sdk-go-v2/service/s3 v1.9.0 h1:FZ5UL5aiybSJKiJglPT7YMMwc431IgOX5gvlFAzSjzs=
46+
github.com/aws/aws-sdk-go-v2/service/s3 v1.9.0/go.mod h1:zHCjYoODbYRLz/iFicYswq1gRoxBnHvpY5h2Vg3/tJ4=
4747
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.3.1 h1:atHdsCczZyM/y9QIoCQnxudoKk8+ya2EPIplDOofkjw=
4848
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.3.1/go.mod h1:ayQUSrG5QyIl2jRSB0YnoJ1e9swNxsBWaCK3hNI2caI=
4949
github.com/aws/aws-sdk-go-v2/service/sqs v1.4.1 h1:BBW0EAZrJh3pesbs9m7ghgC8kylLjTPmWN3yG7POjnA=

0 commit comments

Comments
 (0)