1

As documented, we can use patch_all to automatically create subsegments for aws services such as s3 and sqs. My question is where should I call this method.

For example, let's assume there are 2 files which use boto3 and we want to patch. Because they are independent, we must call patch_all in both of them. Hence, we would have:

File a.py:

import boto3
from xray_recorder import patch_all
patch_all()

File b.py:

import boto3
from xray_recorder import patch_all
patch_all()

Now, lets assume we have a module named c.py which import both a.py and b.py:

import a
import b

In this flow, boto3 would be patched twice.

The only solution I found for it is to import boto3 only from inside a file, named for example my_boto3.py which imports boto3 and patch it.

What is the good practice to patch boto3? Thanks

MyNick
  • 536
  • 1
  • 9
  • 25

1 Answers1

0

You can simply call patch_all() in the main function. Just follow instructions here, and it should work for all of the libraries(functions and classes) patched within the same application.

Lu Peng
  • 26
  • 1
  • what is the "main" function when working with lambdas? there is no main – MyNick Sep 16 '20 at 10:10
  • That would be the lambda handler or, better yet, do it outside any function. It will save time in case the lambda context is reused. You may also want to check lambda power tools: https://awslabs.github.io/aws-lambda-powertools-python/2.8.0/ – jsa Feb 13 '23 at 09:43