Skip to content

Commit 1a6bf19

Browse files
committed
refactor: use optimized should_log checks
1 parent 87df845 commit 1a6bf19

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

microbootstrap/middlewares/fastapi.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@
66
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
77

88
from microbootstrap.instruments.logging_instrument import fill_log_message
9+
from .utils import optimize_exclude_paths
910

1011

1112
def 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()

microbootstrap/middlewares/litestar.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
from litestar.status_codes import HTTP_500_INTERNAL_SERVER_ERROR
99

1010
from microbootstrap.instruments.logging_instrument import fill_log_message
11+
from .utils import optimize_exclude_paths
1112

1213

1314
def 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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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

0 commit comments

Comments
 (0)