File tree Expand file tree Collapse file tree 3 files changed +23
-6
lines changed
microbootstrap/middlewares Expand file tree Collapse file tree 3 files changed +23
-6
lines changed Original file line number Diff line number Diff line change 66from starlette .middleware .base import BaseHTTPMiddleware , RequestResponseEndpoint
77
88from microbootstrap .instruments .logging_instrument import fill_log_message
9+ from .utils import optimize_exclude_paths
910
1011
1112def build_fastapi_logging_middleware (
1213 exclude_endpoints : typing .Iterable [str ],
1314) -> type [BaseHTTPMiddleware ]:
15+ endpoints_to_ignore : typing .Collection [str ] = optimize_exclude_paths (exclude_endpoints )
16+
1417 class FastAPILoggingMiddleware (BaseHTTPMiddleware ):
1518 async def dispatch (
1619 self ,
1720 request : fastapi .Request ,
1821 call_next : RequestResponseEndpoint ,
1922 ) -> fastapi .Response :
2023 request_path : typing .Final = request .url .path .removesuffix ("/" )
21- should_log : typing .Final = not any (
22- exclude_endpoint == request_path for exclude_endpoint in exclude_endpoints
23- )
2424
25- if not should_log :
25+ if request_path in endpoints_to_ignore :
2626 return await call_next (request )
2727
2828 start_time : typing .Final = time .perf_counter_ns ()
Original file line number Diff line number Diff line change 88from litestar .status_codes import HTTP_500_INTERNAL_SERVER_ERROR
99
1010from microbootstrap .instruments .logging_instrument import fill_log_message
11+ from .utils import optimize_exclude_paths
1112
1213
1314def build_litestar_logging_middleware (
1415 exclude_endpoints : typing .Iterable [str ],
1516) -> type [MiddlewareProtocol ]:
17+ endpoints_to_ignore : typing .Collection [str ] = optimize_exclude_paths (exclude_endpoints )
18+
1619 class LitestarLoggingMiddleware (MiddlewareProtocol ):
1720 def __init__ (self , app : litestar .types .ASGIApp ) -> None :
1821 self .app = app
@@ -26,9 +29,8 @@ async def __call__(
2629 request : typing .Final [litestar .Request ] = litestar .Request (request_scope ) # type: ignore[type-arg]
2730
2831 request_path = request .url .path .removesuffix ("/" )
29- should_log : typing .Final = not any (one_endpoint == request_path for one_endpoint in exclude_endpoints )
3032
31- if not should_log :
33+ if request_path in endpoints_to_ignore :
3234 await self .app (request_scope , receive , send_function )
3335 return
3436
Original file line number Diff line number Diff line change 1+ import typing
2+
3+
4+ def optimize_exclude_paths (
5+ exclude_endpoints : typing .Iterable [str ],
6+ ) -> typing .Collection [str ]:
7+ # `in` operator is faster for tuples than for lists
8+ endpoints_to_ignore : typing .Collection [str ] = tuple (exclude_endpoints )
9+
10+ # 10 is just an empirical value, based of measuring the performance
11+ # iterating over a tuple of <10 elements is faster than hashing
12+ if len (endpoints_to_ignore ) >= 10 : # noqa: PLR2004
13+ endpoints_to_ignore = set (endpoints_to_ignore )
14+
15+ return endpoints_to_ignore
You can’t perform that action at this time.
0 commit comments