@@ -46,8 +46,8 @@ import (
4646)
4747
4848type nrMiddleware struct {
49- txn * newrelic.Transaction
50- creds aws. Credentials
49+ txn * newrelic.Transaction
50+ accountID string
5151}
5252
5353type contextKey string
@@ -77,14 +77,7 @@ func (m nrMiddleware) deserializeMiddleware(stack *smithymiddle.Stack) error {
7777 serviceName := awsmiddle .GetServiceID (ctx )
7878 operation := awsmiddle .GetOperationName (ctx )
7979 region := awsmiddle .GetRegion (ctx )
80-
81- creds := awsmiddle .GetSigningCredentials (ctx )
82- accountID , err := awssupport .AWSAccountIdFromAWSAccessKey (creds )
83- if err != nil {
84- accountID = ""
85- fmt .Println (err .Error ())
86- }
87-
80+ accountID := m .accountID
8881 var segment endable
8982
9083 if serviceName == "dynamodb" || serviceName == "DynamoDB" {
@@ -138,7 +131,9 @@ func (m nrMiddleware) deserializeMiddleware(stack *smithymiddle.Stack) error {
138131 integrationsupport .AddAgentSpanAttribute (txn , newrelic .AttributeAWSElastSearchDomainEndpoint , httpRequest .URL .String ()) // this way I don't have to pull it out of context
139132 }
140133 // Set additional span attributes
134+
141135 integrationsupport .AddAgentSpanAttribute (txn , newrelic .AttributeCloudAccountID , accountID ) // setting account ID here, why do we only do this if it is an SQS service?
136+
142137 integrationsupport .AddAgentSpanAttribute (txn ,
143138 newrelic .AttributeResponseCode , strconv .Itoa (response .StatusCode ))
144139 integrationsupport .AddAgentSpanAttribute (txn ,
@@ -161,7 +156,6 @@ func (m nrMiddleware) serializeMiddleware(stack *middleware.Stack) error {
161156 ctx context.Context , in middleware.InitializeInput , next middleware.InitializeHandler ) (
162157 out middleware.InitializeOutput , metadata middleware.Metadata , err error ) {
163158 serviceName := awsmiddle .GetServiceID (ctx )
164- ctx = awsmiddle .SetSigningCredentials (ctx , m .creds )
165159 switch serviceName {
166160 case "dynamodb" , "DynamoDB" :
167161 ctx = context .WithValue (ctx , dynamodbInputKey , dynamoDBInputFromMiddlewareInput (in ))
@@ -229,8 +223,31 @@ func (m nrMiddleware) serializeMiddleware(stack *middleware.Stack) error {
229223// if err != nil {
230224// log.Fatal(err)
231225// }
232- func AppendMiddlewares (apiOptions * []func (* smithymiddle.Stack ) error , txn * newrelic.Transaction , creds aws.Credentials ) {
233- m := nrMiddleware {txn : txn , creds : creds }
226+ func AppendMiddlewares (apiOptions * []func (* smithymiddle.Stack ) error , txn * newrelic.Transaction ) {
227+ m := nrMiddleware {txn : txn }
228+ * apiOptions = append (* apiOptions , m .deserializeMiddleware )
229+ * apiOptions = append (* apiOptions , m .serializeMiddleware )
230+ }
231+
232+ func NRAppendMiddlewares (apiOptions * []func (* smithymiddle.Stack ) error , ctx context.Context , awsConfig aws.Config ) {
233+ txn := newrelic .FromContext (ctx )
234+
235+ creds , err := awsConfig .Credentials .Retrieve (ctx )
236+ if err != nil {
237+ fmt .Println ("error: Couldn't get AWS Credentials" )
238+ }
239+
240+ cfg , ok := txn .Application ().Config ()
241+ m := nrMiddleware {txn : txn }
242+
243+ if ok {
244+ accountID , err := ResolveAWSCredentials (cfg , creds )
245+ if err != nil {
246+ fmt .Println ("error: Couldn't resolve AWS credentials" )
247+ }
248+ m .accountID = accountID
249+ }
250+
234251 * apiOptions = append (* apiOptions , m .deserializeMiddleware )
235252 * apiOptions = append (* apiOptions , m .serializeMiddleware )
236253}
@@ -291,3 +308,18 @@ func dynamoDBInputFromMiddlewareInput(in middleware.InitializeInput) dynamodbInp
291308 return dynamodbInput {}
292309 }
293310}
311+
312+ func ResolveAWSCredentials (cfg newrelic.Config , creds aws.Credentials ) (string , error ) {
313+
314+ accountID , err := awssupport .AWSAccountIdFromAWSAccessKey (creds )
315+ if err != nil {
316+ return "" , err
317+ }
318+
319+ // if the accountID that comes back is different than what is set in the env
320+ // then we should use that accountID since it is accurate
321+ if accountID != "" && accountID != cfg .CloudAWS .AccountID {
322+ return accountID , nil
323+ }
324+ return cfg .CloudAWS .AccountID , nil
325+ }
0 commit comments