1414from fastapi .dependencies .utils import _should_embed_body_fields # noqa
1515from fastapi .openapi .constants import REF_PREFIX
1616
17+
1718from fastapi ._compat import ModelField , Undefined # noqa
1819from fastapi .dependencies .models import Dependant
1920from fastapi .encoders import jsonable_encoder
3536
3637logger = logging .getLogger (__name__ )
3738
38- try :
39- from fastapi ._compat import _normalize_errors # noqa
40- except ImportError :
41- def _normalize_errors (errors : Sequence [Any ]) -> List [Dict [str , Any ]]:
42- return errors
39+
4340try :
4441 import sentry_sdk
4542 import sentry_sdk .tracing
4643 from sentry_sdk .utils import transaction_from_function as sentry_transaction_from_function
47-
48- if hasattr (sentry_sdk , 'new_scope' ):
49- # sentry_sdk 2.x
50- @contextmanager
51- def _handle_disabled_sentry_integration (self : "JsonRpcContext" ):
52- with sentry_sdk .new_scope () as scope :
53- scope .clear_breadcrumbs ()
54- scope .add_event_processor (self ._make_sentry_event_processor ())
55- yield scope
56-
57-
58- def get_sentry_integration ():
59- return sentry_sdk .get_client ().get_integration (
60- "FastApiJsonRPCIntegration"
61- )
62- else :
63- # sentry_sdk 1.x
64- @contextmanager
65- def _handle_disabled_sentry_integration (self : "JsonRpcContext" ):
66- hub = sentry_sdk .Hub .current
67- with sentry_sdk .Hub (hub ) as hub :
68- with hub .configure_scope () as scope :
69- scope .clear_breadcrumbs ()
70- scope .add_event_processor (self ._make_sentry_event_processor ())
71- yield scope
72-
73-
74- get_sentry_integration = lambda : None
75-
7644except ImportError :
77- # no sentry installed
7845 sentry_sdk = None
7946 sentry_transaction_from_function = None
80- get_sentry_integration = lambda : None
8147
48+ try :
49+ from fastapi ._compat import _normalize_errors # noqa
50+ except ImportError :
51+ def _normalize_errors (errors : Sequence [Any ]) -> List [Dict [str , Any ]]:
52+ return errors
53+
54+ if hasattr (sentry_sdk , 'new_scope' ):
55+ # sentry_sdk 2.x
56+ sentry_new_scope = sentry_sdk .new_scope
57+
58+ def get_sentry_integration ():
59+ return sentry_sdk .get_client ().get_integration (
60+ "FastApiJsonRPCIntegration"
61+ )
62+ else :
63+ # sentry_sdk 1.x
64+ @contextmanager
65+ def sentry_new_scope ():
66+ hub = sentry_sdk .Hub .current
67+ with sentry_sdk .Hub (hub ) as hub :
68+ with hub .configure_scope () as scope :
69+ yield scope
70+
71+ get_sentry_integration = lambda : None
8272
83- @asynccontextmanager
84- def _handle_disabled_sentry_integration (self : "JsonRpcContext" ):
85- yield None
8673
8774class Params (fastapi .params .Body ):
8875 def __init__ (
@@ -632,8 +619,12 @@ def request(self) -> JsonRpcRequest:
632619 async def __aenter__ (self ):
633620 assert self .exit_stack is None
634621 self .exit_stack = await AsyncExitStack ().__aenter__ ()
635- if sentry_sdk is not None :
636- self .exit_stack .enter_context (self ._enter_sentry_scope ())
622+ if (
623+ sentry_sdk is not None
624+ and get_sentry_integration () is None
625+ ):
626+ self .exit_stack .enter_context (self ._enter_old_sentry_integration ())
627+
637628 await self .exit_stack .enter_async_context (self ._handle_exception (reraise = False ))
638629 self .jsonrpc_context_token = _jsonrpc_context .set (self )
639630 return self
@@ -662,20 +653,22 @@ async def _handle_exception(self, reraise=True):
662653 logger .exception (str (self .exception ), exc_info = self .exception )
663654
664655 @contextmanager
665- def _enter_sentry_scope (self ):
666- integration_is_active = get_sentry_integration () is not None
667- # we do not need to use sentry fallback if `sentry-sdk`is not installed or integration is enabled explicitly
668- if integration_is_active or sentry_sdk is None :
669- yield
670- return
671-
672-
656+ def _enter_old_sentry_integration (self ):
673657 warnings .warn (
674658 "Implicit Sentry integration is deprecated and may be removed in a future major release. "
675659 "To ensure compatibility, use sentry-sdk 2.* and explicit integration:"
676660 "`from fastapi_jsonrpc.contrib.sentry import FastApiJsonRPCIntegration`. "
677661 )
678- with _handle_disabled_sentry_integration (self ) as scope :
662+ with sentry_new_scope () as scope :
663+ # Actually we can use set_transaction_name
664+ # scope.set_transaction_name(
665+ # sentry_transaction_from_function(method_route.func),
666+ # source=sentry_sdk.tracing.TRANSACTION_SOURCE_CUSTOM,
667+ # )
668+ # and we need `method_route` instance for that,
669+ # but method_route is optional and is harder to track it than adding event processor
670+ scope .clear_breadcrumbs ()
671+ scope .add_event_processor (self ._make_sentry_event_processor ())
679672 yield scope
680673
681674 def _make_sentry_event_processor (self ):
@@ -1599,4 +1592,4 @@ def echo(
15991592
16001593 app .bind_entrypoint (api_v1 )
16011594
1602- uvicorn .run (app , port = 5000 , debug = True , access_log = False ) # noqa
1595+ uvicorn .run (app , port = 5000 , debug = True , access_log = False ) # noqa
0 commit comments