-
Notifications
You must be signed in to change notification settings - Fork 133
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 Fastapi contrib #369
Add Fastapi contrib #369
Conversation
Hi @arthurio ! Thanks for your effort on this PR. Great job 👌 Great timing too as we are about to release pyrollbar with our own FastAPI integration. I'm afraid this PR won't be merged though but the good news is that the release should be ready in the coming weeks! Stay tuned 😎✨ |
@bxsx Great to hear 👍🏻 |
@bxsx Do you have more info on when your FastAPI integration will be released? I had to switch FastAPI projects to Sentry in the meantime but would like to have them in Rollbar. |
@tedmiston I believe it will be released soon after upcoming public holidays. |
awesome. thank you! |
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: Documentation available at https://docs.rollbar.com/docs/python |
@bxsx Will do! Thanks for the update and all the work that went in. |
Thanks @arthurio ! PS. We have a few extra integration features in our roadmap, so expect some improvements over time. In any case, please let us know if anything is missing! |
Thank you! Looking forward to checking it out. |
@bxsx I was able to set it up for fastapi but the fact that you catch errors at the router level means that expected errors (like validation or unauthorized requests, i.e. @app.exception_handler(Exception)
async def handle_unexpected_exceptions(request: Request, exc: Exception):
"""Handle exceptions that were unexpected in endpoints.
Send an error message to rollbar and let it blow up.
"""
try:
raise exc
except Exception:
rollbar.report_exc_info(request=request)
return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) I haven't found a straightforward way to do that with your implementation. Do you have any recommendations? Edit: For now I'm going with the following, let me know if you can think of a better way: import sys
import rollbar
from fastapi import Request, __version__, status
from fastapi.responses import Response
from rollbar.lib._async import report_exc_info
from . import settings
def configure(app):
"""Init rollbar module."""
rollbar.init(
settings.rollbar_token,
environment=settings.environment,
root=str(settings.src_dir),
capture_ip=True,
handler="async",
include_request_body=True,
)
def _hook(request, data):
data["framework"] = f"fastapi {__version__}"
rollbar.BASE_DATA_HOOK = _hook
@app.exception_handler(Exception)
async def handle_unexpected_exceptions(request: Request, exc: Exception):
"""Handle exceptions that were unexpected in endpoints.
Send an error message to rollbar and let it blow up.
Note: To test locally, make sure to use DEBUG=0.
"""
try:
raise exc
except Exception:
await report_exc_info(sys.exc_info(), request=request)
return Response(status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) |
Thanks for taking the time for testing the new integration, @arthurio !
You're right but this behavior is intentional for now. The rationale behind this was that we have similar approaches in some other integrations (like Django) and it looks like it can be beneficial in some cases for some of our customers. We may consider disabling the reporting of these errors by default (and provide configurable interface) if there are requests to do so. However, you can still ignore these errors.
You can filter out errors that you don't want to report. Here is an example: import rollbar
from rollbar.contrib.fastapi import add_to as rollbar_add_to
def configure(app):
"""Init rollbar module."""
rollbar.init(
settings.rollbar_token,
environment=settings.environment,
root=str(settings.src_dir),
capture_ip=True,
handler="async",
include_request_body=True,
)
def ignore_expected_errors(payload, **kw):
EXPECTED_ERRORS = ('RequestValidationError', 'HTTPException')
if payload['data']['body']['trace']['exception']['class'] in EXPECTED_ERRORS:
return False
return payload
rollbar.events.add_payload_handler(ignore_expected_errors)
rollbar_add_to(app)
The problem with using the If that's OK for you (no request body in payload) you can be also interested in integrating via middleware. The middleware does not include the request body in the payload and does not report import rollbar
from rollbar.contrib.fastapi import ReporterMiddleware
def configure(app):
"""Init rollbar module."""
rollbar.init(
settings.rollbar_token,
environment=settings.environment,
root=str(settings.src_dir),
capture_ip=True,
handler="async",
include_request_body=True, # this is ignored for ASGI middleware
)
app.add_middleware(ReporterMiddleware) Let me know if you have further questions! PS. Thanks for your feedback! I've added a note about additional errors in case of integrating via router and how to handle it. |
@bxsx That's great, thanks for the suggestion, it works just as expected 👌🏻 |
Glad to hear! |
Description of the change
Add basic support for fastapi. Since accessing the request data (body and GET/POST) is done via asynchronous methods I'm not sure how to implement that. Looks like older version of python are still supported so it'll have to wait... At least this PR can be used as a guide for people seeking setting up rollbar with fastapi.
TODO
body
andGET/POST
to request dataperson
informationType of change
Checklists
Development
Code review