rollbar / pyrollbar

Error tracking and logging from Python to Rollbar
https://docs.rollbar.com/docs/python/
MIT License
213 stars 135 forks source link

FastAPI integration fails if docs are disabled #398

Closed MattiasOlla closed 1 week ago

MattiasOlla commented 2 years ago

I have a FastAPI application where the docs are disabled:

from fastapi import FastAPI
import rollbar
from rollbar.contrib.fastapi import add_to as rollbar_add_to

rollbar.init(
    ...,
    environment='test',
    handler="async",
    include_request_body=True,
)

app = FastAPI(docs_url=None, redoc_url=None)

rollbar_add_to(app)

Which gives me the error ERROR:rollbar.contrib.fastapi.routing:RollbarLoggingRoute must to be added to a bare router (before adding routes). See docs for more details.

I dug a bit, and the issue seems to be in this section https://github.com/rollbar/pyrollbar/blob/77cbffaa7447f04f653135e1d7f615ce41fcc4e9/rollbar/contrib/fastapi/utils.py#L69-L82 where the number of expected routes for a bare app is hardwired to 4 (meaning it expects /openapi.json, /docs, /docs/oauth2-redirect, and /redoc), but my app only has one of those (/openapi.json).

Depending on the FastAPI setup, there might be anywhere between 0 and 4 available routes for a "bare" app. I might create a PR for this later, but my suggestion would be checking that there are no available routes other than the ones in the fields openapi_url, docs_url, redoc_url, and swagger_ui_oauth2_redirect_url on the FastAPI object https://github.com/tiangolo/fastapi/blob/3efb4f7edff99fdc12802a85ae9d140ec4772497/fastapi/applications.py#L107-L121

antonio-cinnamon commented 5 months ago

This is also happening to me.