-
Notifications
You must be signed in to change notification settings - Fork 135
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
Add integration for FastAPI, Starlette and ASGI & enhance async support #373
Conversation
This feature adds support for type hints in popular IDEs such as PyCharm or Visual Studio Code. The Python IDEs cannot infer type hinting for arbitrary dynamic code. So, annotations must be explicitly declared in the class body. To make Starlette installation optional, the class definition must be duplicated.
Only tests as functionality is already coverd by ASGIMiddleware.
Support http type for now due to Starlette limitation, see: https://github.com/encode/starlette/blob/master/starlette/applications.py#L192-L195
Without reloading tests are not independent because rollbar.BASE_DATA_HOOK remains from previously loaded framework.
Due to collision with ASGIApp class decorator
e3f1ce9
to
6d87324
Compare
assert_called_once() is added in Python3.6+
When providing non-async handler in tests. Default can be async sometimes, so better to test with thread.
Commented out code is the proper scenario but cannot be used as the test file is run under Python2
When reporting via concurrent coroutines, the async_handler ctx manager stores a locally substituted async handler. This allows each coroutine to use different handler. This solution is based on `contextvars` module that was added in Python 3.7+. In case of Python 3.6, it tries to backport the module using `aiocontextvars` 3rd party package.
fbc8ad2
to
35da296
Compare
@diegov I've made some big changes. Could you please re-review the PR? Thanks |
This feature requires `aiocontextvars` package to be installed when running on Python 3.6. Newer Python versions don't require the package.
Since it actually produce resources it's better to keep class naming convention here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯
We've released v0.16.0 with ASGI / Starlette / FastAPI support. You can update pyrollbar to the latest version (v0.16.1 as I write it) via pip and try it out: pip install -U rollbar Documentation available at https://docs.rollbar.com/docs/python |
The change fixes the check to match the previous code: `if not StarletteRequest and hasattr(request, 'user'):`
…ango fix #373 breaking collecting user information on *not* starlette
Description of the change
This pull request introduces ASGI-based framework integration and asynchronous reporting support.
New features
Details
rollbar.SETTINGS['handler']
equalsdefault
or it's unset). Any other handler can still be used if set explicitly.rollbar.SETTINGS
rollbar.lib._async
provides coroutines such asreport_exc_info()
andreport_message()
- pay attention to the leading underscore (_async
) - this is not part of the public APIpython-multipart
package to parse body content or Python 3.6 requiresaiocontextvars
package to supportLoggerMiddleware
and global access for current request objects)Public API
Async handler
rollbar.SETTINGS['handler']
- added new honored values:default
,async
andhttpx
default
- falls back tothread
for backwards compatibility if not used with ASGI/Starlette/FastAPI integration which usesasync
asdefault
async
- default async handler; currently it falls back tohttpx
. For ASGI integrationsdefault == async
httpx
- async HTTP client (requires HTTPX package to be installed). Requires Python 3.6+.ASGI integration
rollbar.contrib.asgi.ReporterMiddleware
- basic integration with any ASGI apps; allows to catch errors and then asynchronously report them to Rollbar. For now, the middleware provides limited capabilities, e.g. supports only ASGIv3 and HTTP connection type, no request object in payload etc. Requires Python 3.5+.Starlette integration
rollbar.contrib.starlette.ReporterMiddleware
- catches errors and asynchronously reports them to Rollbar (should be added first in the middleware stack). Current restrictions: no request body in the payload, only HTTP connection type support. Requires Python 3.6+.rollbar.contrib.starlette.LoggerMiddleware
- stores the current request object for global access. This middleware is necessary forrollbar.logger
used with Starlette (e.g. when you don't want to useReporterMiddleware
) and should be added last in the middleware stack. Requires Python 3.7+ oraiocontextvars
in Python 3.6.rollbar.contrib.starlette.get_current_request()
- returns the current request object (ReporterMiddleware
orLoggerMiddleware
must be installed). Requires Python 3.7+ oraiocontextvars
in Python 3.6.FastAPI integration
rollbar.contrib.fastapi.add_to(app_or_router)
- injectsRollbarLoggingRoute
class into the FastAPI router. This is the recommended way to integratepyrollbar
with FastAPI apps. It allows to add streamed data to the payload, such as request body, form fields or file names. Requires Python 3.6+ and FastAPI 0.41.0+.rollbar.contrib.fastapi.ReporterMiddleware
- similar capabilities to the Starlette middleware described above. Requires Python 3.6+.rollbar.contrib.fastapi.LoggerMiddleware
- similar capabilities to the Starlette middleware described above. Requires Python 3.7+ oraiocontextvars
in Python 3.6.rollbar.contrib.fastapi.get_current_request()
- similar capabilities to theget_current_request()
from the Starlette integration; FastAPI middleware orRollbarLoggingRoute
class must be installed. Requires Python 3.7+ oraiocontextvars
in Python 3.6.Exceptions
RollbarAsyncError
- base exception for internal async errorsFastAPIVersionError
- raised if the minimum version of FastAPI is not met (seerollbar.contrib.fastapi.add_to()
)Type of change
Related issues
Checklists
Development
Code review