1

We have an application with around 25 Lambdas where we want to implement Xray tracing for all (or most) AWS SDK client calls. We have done a PoC implementation as described here, and it's working as expected. What I'm wondering is how we can best pass around the xray context object that is needed by all the client ...WithContext() methods?

I feel that the best way would be to pass the context along when the actual client is created, but since we create the clients BEFORE the actual handler is invoked to allow for reuse of those clients, this is not possible since we don't yet have access to the request context object. Passing the context in every single method call downstream to the clients (DynamoDB, Cognito, S3, KMS etc.) doesn't feel like a good solution either since that will clutter the code, and we're essentially passing the exact same context everywhere.

Is there a better way to share the context?

Daniel
  • 2,050
  • 7
  • 31
  • 55

1 Answers1

0

You mention that the client is created before the handler is invoked. This is indeed the recommendation, but as long as you don't do any actual AWS SDK invocations during this initialization phase, this method will indeed work as you have seen.

Is there a reason why you need to pass the context manually yourself? We provide an API that allows you to patch existing clients to perform the context handling internally. You can initialize your clients outside the function handler, as you are doing right now, and then patch it by calling xray.AWS(CLIENT_HERE). And then you dont need to manually pass in the context. In my opinion, this would be cleaner and easier to manage. Does your use case require having a context present?

Here is documentation showing how to do it: https://docs.aws.amazon.com/xray/latest/devguide/xray-sdk-go-awssdkclients.html

  • But if you check the second example, you can clearly see that you need a context to actually call the API – Daniel Dec 17 '19 at 07:11