-
Notifications
You must be signed in to change notification settings - Fork 419
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
contrib/aws/{aws-sdk-go/aws, aws-sdk-go-v2/aws}: add context example #1504
Conversation
This PR adds an example of passing context when making calls through the aws sdk. This allows the aws request spans to inherit their parent from context. This came up in APMS-7932.
Not sure if an example should warrant an entry to |
We actually just had a discussion about this due to an issue caused by an example added for a different contrib package that brought in a significant number of |
Moved the example into the package godoc @ajgajg1134 👍 |
Thanks for moving the package godoc, one last possible request :P Could we move the package godoc into a separate docs file? |
…ataDog/dd-trace-go into mackjmr/add-aws-sdk-go-context-example
True, it's much cleaner that way @ajgajg1134! Updated :) |
@mackjmr do you still want to work on this? If so, there are some merge conflicts to resolve first. |
@katiehockman I've resolved the conflicts :) I think it would still be good to get this example in, as it came up in an escalation. |
BenchmarksBenchmark execution time: 2023-09-28 18:31:03 Comparing candidate commit bec66de in PR branch Found 0 performance improvements and 0 performance regressions! Performance is the same for 40 metrics, 1 unstable metrics. |
// Package aws provides functions to trace aws/aws-sdk-go-v2 (https://github.com/aws/aws-sdk-go-v2). | ||
// | ||
// Usage Example: | ||
// | ||
// import ( | ||
// "context" | ||
// "log" | ||
// "os" | ||
// | ||
// "github.com/aws/aws-sdk-go-v2/aws" | ||
// awscfg "github.com/aws/aws-sdk-go-v2/config" | ||
// "github.com/aws/aws-sdk-go-v2/feature/s3/manager" | ||
// "github.com/aws/aws-sdk-go-v2/service/s3" | ||
// "github.com/aws/aws-sdk-go-v2/service/sqs" | ||
// | ||
// awstrace "gopkg.in/DataDog/dd-trace-go.v1/contrib/aws/aws-sdk-go-v2/aws" | ||
// "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext" | ||
// "gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer" | ||
// ) | ||
// | ||
// func Example() { | ||
// awsCfg, err := awscfg.LoadDefaultConfig(context.Background()) | ||
// if err != nil { | ||
// log.Fatalf(err.Error()) | ||
// } | ||
// awstrace.AppendMiddleware(&awsCfg) | ||
// sqsClient := sqs.NewFromConfig(awsCfg) | ||
// sqsClient.ListQueues(context.Background(), &sqs.ListQueuesInput{}) | ||
// } | ||
// | ||
// // An example of the aws span inheriting a parent span from context. | ||
// func Example_context() { | ||
// cfg, err := awscfg.LoadDefaultConfig(context.TODO(), awscfg.WithRegion("us-west-2")) | ||
// if err != nil { | ||
// log.Fatalf("error: %v", err) | ||
// } | ||
// awstrace.AppendMiddleware(&cfg) | ||
// client := s3.NewFromConfig(cfg) | ||
// uploader := manager.NewUploader(client) | ||
// | ||
// // Create a root span. | ||
// span, ctx := tracer.StartSpanFromContext(context.Background(), "parent.request", | ||
// tracer.SpanType(ext.SpanTypeWeb), | ||
// tracer.ServiceName("web"), | ||
// tracer.ResourceName("/upload"), | ||
// ) | ||
// defer span.Finish() | ||
// | ||
// // Open image file. | ||
// filename := "my_image.png" | ||
// file, err := os.Open(filename) | ||
// if err != nil { | ||
// log.Fatalf("error: %v", err) | ||
// } | ||
// defer file.Close() | ||
// | ||
// uploadParams := &s3.PutObjectInput{ | ||
// Bucket: aws.String("my_bucket"), | ||
// Key: aws.String(filename), | ||
// Body: file, | ||
// ContentType: aws.String("image/png"), | ||
// } | ||
// // Inherit parent span from context. | ||
// _, err = uploader.Upload(ctx, uploadParams) | ||
// if err != nil { | ||
// log.Fatalf("error: %v", err) | ||
// } | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is just a copy of the example we already have in example_test
, then I'm not sure it adds too much extra value. I'm not sure if @ajgajg1134 was suggesting this during his last comment, or if he was suggesting something else...
I'd personally just remove this but keep the example code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func Example
is a copy from example_test
, but func Example_context
was added, which is an example that demonstrate how to pass context.
The reason we moved all of this out of from example_test is to avoid adding entries to the go.mod
that are necessary for func Example_context
(for github.com/aws/aws-sdk-go-v2/feature/s3/manager
and github.com/aws/aws-sdk-go-v2/service/s3
)
This PR adds an example of passing context when making calls through the aws sdk. This allows the aws request spans to inherit their parent from context. This came up in APMS-7932.