|
14 | 14 | from fastapi.dependencies.utils import _should_embed_body_fields # noqa |
15 | 15 | from fastapi.openapi.constants import REF_PREFIX |
16 | 16 |
|
17 | | - |
18 | 17 | from fastapi._compat import ModelField, Undefined # noqa |
19 | 18 | from fastapi.dependencies.models import Dependant |
20 | 19 | from fastapi.encoders import jsonable_encoder |
|
36 | 35 |
|
37 | 36 | logger = logging.getLogger(__name__) |
38 | 37 |
|
39 | | - |
40 | | -try: |
41 | | - import sentry_sdk |
42 | | - import sentry_sdk.tracing |
43 | | - from sentry_sdk.utils import transaction_from_function as sentry_transaction_from_function |
44 | | -except ImportError: |
45 | | - sentry_sdk = None |
46 | | - sentry_transaction_from_function = None |
47 | | - |
48 | 38 | try: |
49 | 39 | from fastapi._compat import _normalize_errors # noqa |
50 | 40 | except ImportError: |
51 | 41 | def _normalize_errors(errors: Sequence[Any]) -> List[Dict[str, Any]]: |
52 | 42 | return errors |
| 43 | +try: |
| 44 | + import sentry_sdk |
| 45 | + import sentry_sdk.tracing |
| 46 | + from sentry_sdk.utils import transaction_from_function as sentry_transaction_from_function |
53 | 47 |
|
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: |
| 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()) |
69 | 55 | yield scope |
70 | 56 |
|
71 | | - get_sentry_integration = lambda : None |
| 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 | + |
| 76 | +except ImportError: |
| 77 | + # no sentry installed |
| 78 | + sentry_sdk = None |
| 79 | + sentry_transaction_from_function = None |
| 80 | + get_sentry_integration = lambda: None |
72 | 81 |
|
73 | 82 |
|
| 83 | + @asynccontextmanager |
| 84 | + def _handle_disabled_sentry_integration(self: "JsonRpcContext"): |
| 85 | + yield None |
| 86 | + |
74 | 87 | class Params(fastapi.params.Body): |
75 | 88 | def __init__( |
76 | 89 | self, |
@@ -650,25 +663,19 @@ async def _handle_exception(self, reraise=True): |
650 | 663 |
|
651 | 664 | @contextmanager |
652 | 665 | def _enter_sentry_scope(self): |
653 | | - if get_sentry_integration() is not None: |
| 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: |
654 | 669 | yield |
655 | 670 | return |
656 | 671 |
|
| 672 | + |
657 | 673 | warnings.warn( |
658 | 674 | "Implicit Sentry integration is deprecated and may be removed in a future major release. " |
659 | 675 | "To ensure compatibility, use sentry-sdk 2.* and explicit integration:" |
660 | 676 | "`from fastapi_jsonrpc.contrib.sentry import FastApiJsonRPCIntegration`. " |
661 | 677 | ) |
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()) |
| 678 | + with _handle_disabled_sentry_integration(self) as scope: |
672 | 679 | yield scope |
673 | 680 |
|
674 | 681 | def _make_sentry_event_processor(self): |
|
0 commit comments