diff --git a/src/gafaelfawr/dependencies/auth.py b/src/gafaelfawr/dependencies/auth.py index ccc1a640..e874450f 100644 --- a/src/gafaelfawr/dependencies/auth.py +++ b/src/gafaelfawr/dependencies/auth.py @@ -3,6 +3,7 @@ from typing import Annotated from urllib.parse import urlencode, urlparse +import sentry_sdk from fastapi import Depends, Header, HTTPException, status from ..auth import ( @@ -236,6 +237,7 @@ class AuthenticateRead(Authenticate): Should be used as a FastAPI dependency. """ + @sentry_sdk.trace async def __call__( self, *, diff --git a/src/gafaelfawr/dependencies/context.py b/src/gafaelfawr/dependencies/context.py index adab1d71..b3e75e80 100644 --- a/src/gafaelfawr/dependencies/context.py +++ b/src/gafaelfawr/dependencies/context.py @@ -6,10 +6,12 @@ including from dependencies. """ +from collections.abc import AsyncIterator from dataclasses import dataclass -from typing import Annotated, Any +from typing import Any -from fastapi import Depends, HTTPException, Request +import sentry_sdk +from fastapi import HTTPException, Request from safir.dependencies.db_session import db_session_dependency from safir.dependencies.logger import logger_dependency from safir.metrics import EventManager @@ -99,11 +101,7 @@ async def __call__( self, *, request: Request, - session: Annotated[ - async_scoped_session, Depends(db_session_dependency) - ], - logger: Annotated[BoundLogger, Depends(logger_dependency)], - ) -> RequestContext: + ) -> AsyncIterator[RequestContext]: """Create a per-request context and return it.""" if not self._config or not self._process_context or not self._events: raise RuntimeError("ContextDependency not initialized") @@ -117,15 +115,19 @@ async def __call__( "type": "missing_client_ip", }, ) - return RequestContext( - request=request, - ip_address=ip_address, - config=self._config, - logger=logger, - events=self._events, - session=session, - factory=Factory(self._process_context, session, logger), - ) + with sentry_sdk.start_span(name="logger"): + logger = await logger_dependency(request) + with sentry_sdk.start_span(name="context"): + async for session in db_session_dependency(): + yield RequestContext( + request=request, + ip_address=ip_address, + config=self._config, + logger=logger, + events=self._events, + session=session, + factory=Factory(self._process_context, session, logger), + ) @property def process_context(self) -> ProcessContext: