Closed narmatov-asylbek closed 2 years ago
Hi @narmatov-asylbek, thanks for opening this issue.
After looking at it the router/app method will not work on a router unless explicitly added. The reason for this is because the Rollbar router integration method simple replaces the APIRouter.route_class
(default APIRoute
) with the RollbarLoggingRoute
class. At first glance it looked like FastAPI should support this with nested routers. However, when a new router is being added with the include_router()
method, A route_class
must be specified or it will default to APIRoute
.
This means you would need to add...
router = APIRouter(...)
rollbar_add_to(router)
However, as you mentioned this is a bit problematic. It is also worth mentioning that the above is the same as...
from rollbar.contrib.fastapi.routing import RollbarLoggingRoute
router = APIRouter(...)
router.route_class = RollbarLoggingRoute
This means that if you needed any advanced functionality from your route class and created a custom route class, you would not get the expected outcome.
from fastapi.routing import APIRoute
class CustomRoute(APIRoute):
...
# advanced route class stuff
router = APIRouter(
# router stuff
route_class=CustomRoute,
)
rollbar_add_to(router)
# the route_class is now RollbarLoggingRoute not CustomRoute
Here are some possible solutions...
APIRouter
and uses RollbarLoggingRoute
as the default route_class
. Then always extend RollbarLoggingRoute
not APIRoute
when creating custom route classses.APIRouter
router instance, but does not accept a route_class
argument. The route_class
is always RollbarLoggingRoute
, unless you create a custom class that extends RollbarLoggingRoute
, and is set via assigning the route_class
attribute on the new APIRouter
instance.rollbar_add_to(router)
after you create every router, and hope it never breaks anything.APIRoute
include the logic from RollbarLoggingRoute
. This is a terrible idea. Don't do this.These options range from generally unadvisable, to terrible. I would use the middleware option. Things, like Rollbar, are why middleware exists. However, because you said you need to integrate via a router you will likely need to chose one of the above options.
@danielmorell thank you for the thoughtful response.
@danielmorell thank you for your response. It will definitely help
@danielmorell the in-depth explanation of how the Rollbar middleware instruments the FastAPI router is very illuminating, as is the comparison of ways to get this working for @narmatov-asylbek's use case. Cheers!
Python 3.8 Rollbar 0.16.2
I am trying to integrate Rollbar with FastAPI using the recommended way(via FastAPI router). But I have some trouble with it.
Rollbar not sending errors with nested routers.
The problem is Rollbar not sending any errors with of router.
In our project, we have more than 20 routers. And adding to every single of them rollbar_add_to(router) can be problematic. Is there any way to fix this?
PS: Please do not suggest integrating via Middleware. I need to integrate Rollbar this way