Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nrawssdk-v2: fix usage examples of nrawssdk.AppendMiddlewares #599

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions v3/integrations/nrawssdk-v2/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
nraws "github.com/newrelic/go-agent/v3/integrations/nrawssdk-v2"
"github.com/newrelic/go-agent/v3/integrations/nrawssdk-v2"
"github.com/newrelic/go-agent/v3/newrelic"
)

Expand Down Expand Up @@ -39,14 +39,15 @@ func main() {
txn := app.StartTransaction("My sample transaction")

ctx := context.Background()
awsConfig, err := config.LoadDefaultConfig(ctx)
awsConfig, err := config.LoadDefaultConfig(ctx, func(awsConfig *config.LoadOptions) error {
// Instrument all new AWS clients with New Relic
nrawssdk.AppendMiddlewares(&awsConfig.APIOptions, nil)
return nil
})
if err != nil {
log.Fatal(err)
}

// Instrument all new AWS clients with New Relic
nraws.AppendMiddlewares(&awsConfig.APIOptions, nil)

s3Client := s3.NewFromConfig(awsConfig)
output, err := s3Client.ListBuckets(ctx, nil)
if err != nil {
Expand Down
52 changes: 39 additions & 13 deletions v3/integrations/nrawssdk-v2/nrawssdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,50 @@ func (m nrMiddleware) deserializeMiddleware(stack *smithymiddle.Stack) error {
// To see segments and spans for all AWS invocations, call AppendMiddlewares
// with the AWS Config `apiOptions` and provide nil for `txn`. For example:
//
// awsConfig, err := config.LoadDefaultConfig(ctx)
// if err != nil {
// log.Fatal(err)
// }
// nraws.AppendMiddlewares(&awsConfig.APIOptions, nil)
// awsConfig, err := config.LoadDefaultConfig(ctx, func(o *config.LoadOptions) error {
// // Instrument all new AWS clients with New Relic
// nrawssdk.AppendMiddlewares(&o.APIOptions, nil)
// return nil
// })
// if err != nil {
// log.Fatal(err)
// }
//
// If do not want the transaction to be retrived from the context, you can
// If do not want the transaction to be retrieved from the context, you can
// explicitly set `txn`. For example:
//
// awsConfig, err := config.LoadDefaultConfig(ctx)
// if err != nil {
// log.Fatal(err)
// }
// txn := loadNewRelicTransaction()
// awsConfig, err := config.LoadDefaultConfig(ctx, func(o *config.LoadOptions) error {
// // Instrument all new AWS clients with New Relic
// nrawssdk.AppendMiddlewares(&o.APIOptions, txn)
// return nil
// })
// if err != nil {
// log.Fatal(err)
// }
//
// ...
// The middleware can also be added later, per AWS service call using
// the `optFns` parameter. For example:
//
// txn := loadNewRelicTransaction()
// nraws.AppendMiddlewares(&awsConfig.APIOptions, txn)
// awsConfig, err := config.LoadDefaultConfig(ctx)
// if err != nil {
// log.Fatal(err)
// }
//
// ...
//
// s3Client := s3.NewFromConfig(awsConfig)
//
// ...
//
// txn := loadNewRelicTransaction()
// output, err := s3Client.ListBuckets(ctx, nil, func(o *s3.Options) error {
// nrawssdk.AppendMiddlewares(&o.APIOptions, txn)
// return nil
// })
// if err != nil {
// log.Fatal(err)
// }
func AppendMiddlewares(apiOptions *[]func(*smithymiddle.Stack) error, txn *newrelic.Transaction) {
m := nrMiddleware{txn: txn}
*apiOptions = append(*apiOptions, m.deserializeMiddleware)
Expand Down
7 changes: 4 additions & 3 deletions v3/integrations/nrawssdk-v2/nrawssdk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,16 @@ var fakeCreds = func() interface{} {
}()

func newConfig(ctx context.Context, txn *newrelic.Transaction) aws.Config {
cfg, _ := config.LoadDefaultConfig(ctx)
cfg, _ := config.LoadDefaultConfig(ctx, func(o *config.LoadOptions) error {
AppendMiddlewares(&o.APIOptions, txn)
return nil
})
cfg.Credentials = fakeCreds.(aws.CredentialsProvider)
cfg.Region = awsRegion
cfg.HTTPClient = &http.Client{
Transport: &fakeTransport{},
}

AppendMiddlewares(&cfg.APIOptions, txn)

return cfg
}

Expand Down
Loading