Sentry-python: TypeError using SentryAsgiMiddleware with FastAPI/Starlette app

Created on 15 Nov 2019  路  9Comments  路  Source: getsentry/sentry-python

I cannot get SentryAsgiMiddleware to work with our FastAPI app. We tried to follow the example in the Sentry docs, so the app module basically looks like this:

from fastapi import FastAPI
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
...
app = FastAPI()

@app.post()
...

app = SentryAsgiMiddleware(app)

This gives an error on all requests, see the following stack trace:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py", line 385, in run_asgi
    result = await app(self.scope, self.receive, self.send)
  File "/usr/local/lib/python3.7/site-packages/uvicorn/middleware/proxy_headers.py", line 45, in __call__
    return await self.app(scope, receive, send)
  File "/usr/local/lib/python3.7/site-packages/uvicorn/middleware/asgi2.py", line 7, in __call__
    await instance(receive, send)
  File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/asgi.py", line 54, in run_asgi2
    scope, lambda: self.app(scope)(receive, send)
  File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/asgi.py", line 93, in _run_app
    raise exc from None
  File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/asgi.py", line 90, in _run_app
    return await callback()
  File "/usr/local/lib/python3.7/site-packages/sentry_sdk/integrations/asgi.py", line 54, in <lambda>
    scope, lambda: self.app(scope)(receive, send)
TypeError: __call__() missing 2 required positional arguments: 'receive' and 'send'

Library versions:

  • python==3.7.5
  • sentry-sdk==0.13.2
  • uvicorn==0.10.8
  • fastapi==0.42.0
  • starlette==0.12.9
bug

Most helpful comment

0.13.3 has been released, sorry for the delay

All 9 comments

It seems like uvicorn might inspect the function signature to figure out which ASGI version to use. Our function signature is *args, **kwargs, because we choose the ASGI version depending on the ASGI version we are called with. So uvicorn doesn't know how to call the app, but the app is wrapped with a middleware (Sentry) that doesn't know either.

I have a vague idea on how to fix this (middleware inspects app's function signature).

Could you try setting this option: https://www.uvicorn.org/settings/#application-interface (try all values except auto)

We run the server with gunicorn and uvicorn.workers.UvicornWorker, which makes it a bit inconvenient to change options in Uvicorn. We found a suggestion to subclass the UvicornWorker to change the config, will try that and report back.

The service is running now, after specifying ASGI3 interface using the following worker class:

from uvicorn.workers import UvicornWorker


class GunicornUvicornWorker(UvicornWorker):
    CONFIG_KWARGS = {"loop": "uvloop", "http": "httptools", "interface": "asgi3"}

ok we will fix this in the sentry sdk in any case. thanks for reporting!

@Stigjb could you try out https://github.com/getsentry/sentry-python/pull/557 while reverting your workaround?

I deployed the project using the standard UvicornWorker and the PR branch at #557, and it worked fine! 馃憤

We really appreciate the quick response! When is the next release planned?

@Stigjb sometime this week

0.13.3 has been released, sorry for the delay

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MaximZemskov picture MaximZemskov  路  3Comments

steven-murray picture steven-murray  路  3Comments

miracle2k picture miracle2k  路  6Comments

zegerius picture zegerius  路  4Comments

mumumumu picture mumumumu  路  6Comments