aws / aws-xray-sdk-go

AWS X-Ray SDK for the Go programming language.
Apache License 2.0
276 stars 117 forks source link

Change how SDK sets the context for AWS SDK calls #418

Closed jj22ee closed 11 months ago

jj22ee commented 11 months ago

Issue #, if available:

Currently, aws-xray-sdk-go only sets the context in the (*Request).HTTPRequest.ctx of an AWS SDK Go v1 call to a downstream service, which will extract the context that was set here. However, sometimes a downstream service would look inside (*Request).context in order to extract the context.

The solution is to set the context in both of these areas, which is also how aws-sdk-go generally sets the context. AWS SDK Go Lambda API InvokeWithContext Example

Description of changes:

This change is essentially:

-   r.HTTPRequest = r.HTTPRequest.WithContext(ctx)
+   r.SetContext(ctx)

Code flow Before:

(*request.Request).HTTPRequest = r.HTTPRequest.WithContext(ctx)    // Panics when ctx == nil

Code flow After:

(*request.Request).SetContext(ctx)

func (r *Request) SetContext(ctx aws.Context) {
    if ctx == nil {
        panic("context cannot be nil")
    }
    setRequestContext(r, ctx)
}

func setRequestContext(r *Request, ctx aws.Context) {
    r.context = ctx
    r.HTTPRequest = r.HTTPRequest.WithContext(ctx)
}

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.