Skip to content

Commit 3856c89

Browse files
committed
feat(config): Add config options for aws configuration
1 parent 4756204 commit 3856c89

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

v3/integrations/nrawssdk-v2/example/main.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ func main() {
2424
newrelic.ConfigAppName("Example App"),
2525
newrelic.ConfigInfoLogger(os.Stdout),
2626
newrelic.ConfigDistributedTracerEnabled(true),
27+
// newrelic.ConfigCloudAWSAccountID("<insert-aws-account-id>"), // Set the AWS accountID. Will override any previous config options
28+
// newrelic.ConfigCloudAWSAccountDecodingEnabled(true), // Enable/disable accountID decoding. Default is disabled
2729
)
2830
if nil != err {
2931
fmt.Println(err)
@@ -40,12 +42,13 @@ func main() {
4042

4143
ctx := newrelic.NewContext(context.Background(), txn)
4244

43-
awsConfig, err := config.LoadDefaultConfig(ctx, func(awsConfig *config.LoadOptions) error {
44-
// Instrument all new AWS clients with New Relic
45-
return nil
46-
})
45+
// We need the values in the aws.Config for InitializeMiddleware, so
46+
// there is no longer a need for a config option function.
47+
awsConfig, err := config.LoadDefaultConfig(ctx)
4748

48-
nrawssdk.NRAppendMiddlewares(&awsConfig.APIOptions, ctx, awsConfig)
49+
// nrawssdk.AppendMiddlewares(&awsConfig.APIOptions, txn) // LEGACY
50+
// AppendMiddlewares is DEPRECATED. Please use InitializeMiddleware shown below
51+
nrawssdk.InitializeMiddleware(&awsConfig.APIOptions, ctx, awsConfig)
4952
if err != nil {
5053
log.Fatal(err)
5154
}

v3/integrations/nrawssdk-v2/nrawssdk.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ func AppendMiddlewares(apiOptions *[]func(*smithymiddle.Stack) error, txn *newre
235235
*apiOptions = append(*apiOptions, m.serializeMiddleware)
236236
}
237237

238-
func NRAppendMiddlewares(apiOptions *[]func(*smithymiddle.Stack) error, ctx context.Context, awsConfig aws.Config) {
238+
func InitializeMiddleware(apiOptions *[]func(*smithymiddle.Stack) error, ctx context.Context, awsConfig aws.Config) {
239239
txn := newrelic.FromContext(ctx)
240240

241241
creds, err := awsConfig.Credentials.Retrieve(ctx)
@@ -249,7 +249,7 @@ func NRAppendMiddlewares(apiOptions *[]func(*smithymiddle.Stack) error, ctx cont
249249
if ok {
250250
err := m.ResolveAWSCredentials(cfg, creds)
251251
if err != nil {
252-
fmt.Println("error: Couldn't resolve AWS credentials")
252+
cfg.Logger.Error(err.Error(), map[string]interface{}{})
253253
}
254254
}
255255

@@ -325,9 +325,9 @@ func (m *nrMiddleware) ResolveAWSCredentials(cfg newrelic.Config, creds aws.Cred
325325
if m.resolver == nil {
326326
m.resolver = &defaultResolver{}
327327
}
328-
329328
accountID, err := m.resolver.AWSAccountIdFromAWSAccessKey(creds)
330329
if err != nil {
330+
// return err, aws account id remains empty
331331
return err
332332
}
333333

v3/newrelic/config_options.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,22 @@ func ConfigCustomInsightsCustomAttributesValues(customAttributes map[string]stri
488488
}
489489
}
490490

491+
// ConfigCloudAWSAccountID is used to set the accountID for the AWS account to add as an attribute to span events. This may also be set using the
492+
// NEW_RELIC_CLOUD_AWS_ACCOUNT_ID environment variable.
493+
func ConfigCloudAWSAccountID(accountID string) ConfigOption {
494+
return func(cfg *Config) {
495+
cfg.CloudAWS.AccountID = accountID
496+
}
497+
}
498+
499+
// ConfigCloudAWSAccountDecodingEnabled is used to enable/disable accountID decoding for an AWS Access Key. Any value that is decoded will be
500+
// overriden if the accountID is set in the config.
501+
func ConfigCloudAWSAccountDecodingEnabled(enabled bool) ConfigOption {
502+
return func(cfg *Config) {
503+
cfg.CloudAWS.AccountDecoding.Enabled = enabled
504+
}
505+
}
506+
491507
// ConfigFromEnvironment populates the config based on environment variables:
492508
//
493509
// NEW_RELIC_APP_NAME sets AppName

0 commit comments

Comments
 (0)