Skip to content

xraysink on FastAPI with boto3 #104

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

Closed
dhartford opened this issue Jun 20, 2023 · 3 comments
Closed

xraysink on FastAPI with boto3 #104

dhartford opened this issue Jun 20, 2023 · 3 comments

Comments

@dhartford
Copy link

Question/query - has anyone successfully used boto3 with xraysink? or particularly secrets manager?

    xray_recorder.configure( context=AsyncContext() )
    libraries = (['botocore','boto3','psycopg2'])
    patch(libraries)
    app.add_middleware(BaseHTTPMiddleware, dispatch=xray_middleware) 

Some of the symptoms I'm seeing appears that mixing the two may potentially be an issue (mixing async context in xray recorder w/ boto3 sync methods)?

My app logs shows things like this:

Could not get Secret Manager credentials, exception: [ cannot find the current segment/subsegment, please make sure you have a segment open ]

@garyd203
Copy link
Owner

Hi, thanks for your question. Can you provide a bit more detail about how you're calling Secrets Manager?

  1. Which AsyncContext are you using? The one from xraysink, or the one from aws_xray_sdk? If it's from aws_xray_sdk, which version of that library do you have?
  2. Can you show some code for how you use boto3. eg. Is it from a request handler, or a task, or a once-off startup-time initialisation, ...

@dhartford
Copy link
Author

The boto3 was used within the request handler. As a workaround, I ended up removing xraysink (and the FastAPI middleware) and wrapped the APIrouter request handler manually. For a small project this was fine, but larger projects would have been nicer with xraysink!

router=APIRouter()

@router.post(.....)
def sample_post():
  xray_recorder.begin_segment('myapp-method')
  ....
  #boto3 actions here
  ....
  xray_recorder.endsegment() #make sure to do this on each return or exception handling statement

@garyd203
Copy link
Owner

Thanks for your response, that code snippet helped. And I'm glad you found a way to workaround your problem.

The fundamental problem, as you suspected, is that the AsyncContext only gets propagated through async calls, which means it is not present when your non-async request handler is called (AIUI FastAPI runs these in a separate thread pool). I'm aware of this issue but the fix (see #89) appears to be a non-trivial amount of work.

Something I have done is to use the async aioboto3 library for calling the AWS API, which allows me to then write an async request handler. The only downside is that testing becomes more arduous - moto doesn't really support aioboto3, although let me know if you find something

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants