44import weakref
55from importlib import import_module
66
7+ import sentry_sdk
78from sentry_sdk ._types import TYPE_CHECKING
89from sentry_sdk .consts import OP , SPANDATA
910from sentry_sdk .db .explain_plan .django import attach_explain_plan_to_span
10- from sentry_sdk .hub import Hub , _should_send_default_pii
11- from sentry_sdk .scope import Scope , add_global_event_processor
11+ from sentry_sdk .scope import Scope , add_global_event_processor , should_send_default_pii
1212from sentry_sdk .serializer import add_global_repr_processor
1313from sentry_sdk .tracing import SOURCE_FOR_STYLE , TRANSACTION_SOURCE_URL
1414from sentry_sdk .tracing_utils import add_query_source , record_sql_queries
1919 SENSITIVE_DATA_SUBSTITUTE ,
2020 logger ,
2121 capture_internal_exceptions ,
22+ ensure_integration_enabled ,
2223 event_from_exception ,
2324 transaction_from_function ,
2425 walk_exception_chain ,
@@ -146,11 +147,9 @@ def setup_once():
146147
147148 old_app = WSGIHandler .__call__
148149
150+ @ensure_integration_enabled (DjangoIntegration , old_app )
149151 def sentry_patched_wsgi_handler (self , environ , start_response ):
150152 # type: (Any, Dict[str, str], Callable[..., Any]) -> _ScopedResponse
151- if Hub .current .get_integration (DjangoIntegration ) is None :
152- return old_app (self , environ , start_response )
153-
154153 bound_old_app = old_app .__get__ (self , WSGIHandler )
155154
156155 from django .conf import settings
@@ -229,11 +228,6 @@ def _django_queryset_repr(value, hint):
229228 if not isinstance (value , QuerySet ) or value ._result_cache :
230229 return NotImplemented
231230
232- # Do not call Hub.get_integration here. It is intentional that
233- # running under a new hub does not suddenly start executing
234- # querysets. This might be surprising to the user but it's likely
235- # less annoying.
236-
237231 return "<%s from %s at 0x%x>" % (
238232 value .__class__ .__name__ ,
239233 value .__module__ ,
@@ -400,8 +394,8 @@ def _set_transaction_name_and_source(scope, transaction_style, request):
400394
401395def _before_get_response (request ):
402396 # type: (WSGIRequest) -> None
403- hub = Hub . current
404- integration = hub . get_integration ( DjangoIntegration )
397+ integration = sentry_sdk . get_client (). get_integration ( DjangoIntegration )
398+
405399 if integration is None :
406400 return
407401
@@ -431,8 +425,7 @@ def _attempt_resolve_again(request, scope, transaction_style):
431425
432426def _after_get_response (request ):
433427 # type: (WSGIRequest) -> None
434- hub = Hub .current
435- integration = hub .get_integration (DjangoIntegration )
428+ integration = sentry_sdk .get_client ().get_integration (DjangoIntegration )
436429 if integration is None or integration .transaction_style != "url" :
437430 return
438431
@@ -490,7 +483,7 @@ def wsgi_request_event_processor(event, hint):
490483 with capture_internal_exceptions ():
491484 DjangoRequestExtractor (request ).extract_into_event (event )
492485
493- if _should_send_default_pii ():
486+ if should_send_default_pii ():
494487 with capture_internal_exceptions ():
495488 _set_user_info (request , event )
496489
@@ -501,22 +494,19 @@ def wsgi_request_event_processor(event, hint):
501494
502495def _got_request_exception (request = None , ** kwargs ):
503496 # type: (WSGIRequest, **Any) -> None
504- hub = Hub . current
505- integration = hub .get_integration (DjangoIntegration )
497+ client = sentry_sdk . get_client ()
498+ integration = client .get_integration (DjangoIntegration )
506499 if integration is not None :
507500 if request is not None and integration .transaction_style == "url" :
508501 scope = Scope .get_current_scope ()
509502 _attempt_resolve_again (request , scope , integration .transaction_style )
510503
511- # If an integration is there, a client has to be there.
512- client = hub .client # type: Any
513-
514504 event , hint = event_from_exception (
515505 sys .exc_info (),
516506 client_options = client .options ,
517507 mechanism = {"type" : "django" , "handled" : False },
518508 )
519- hub .capture_event (event , hint = hint )
509+ sentry_sdk .capture_event (event , hint = hint )
520510
521511
522512class DjangoRequestExtractor (RequestExtractor ):
@@ -612,62 +602,56 @@ def install_sql_hook():
612602 # This won't work on Django versions < 1.6
613603 return
614604
605+ @ensure_integration_enabled (DjangoIntegration , real_execute )
615606 def execute (self , sql , params = None ):
616607 # type: (CursorWrapper, Any, Optional[Any]) -> Any
617- hub = Hub .current
618- if hub .get_integration (DjangoIntegration ) is None :
619- return real_execute (self , sql , params )
620-
621608 with record_sql_queries (
622- hub , self .cursor , sql , params , paramstyle = "format" , executemany = False
609+ self .cursor , sql , params , paramstyle = "format" , executemany = False
623610 ) as span :
624611 _set_db_data (span , self )
625- if hub .client :
626- options = hub .client .options ["_experiments" ].get ("attach_explain_plans" )
627- if options is not None :
628- attach_explain_plan_to_span (
629- span ,
630- self .cursor .connection ,
631- sql ,
632- params ,
633- self .mogrify ,
634- options ,
635- )
612+ options = (
613+ sentry_sdk .get_client ()
614+ .options ["_experiments" ]
615+ .get ("attach_explain_plans" )
616+ )
617+ if options is not None :
618+ attach_explain_plan_to_span (
619+ span ,
620+ self .cursor .connection ,
621+ sql ,
622+ params ,
623+ self .mogrify ,
624+ options ,
625+ )
636626 result = real_execute (self , sql , params )
637627
638628 with capture_internal_exceptions ():
639- add_query_source (hub , span )
629+ add_query_source (span )
640630
641631 return result
642632
633+ @ensure_integration_enabled (DjangoIntegration , real_executemany )
643634 def executemany (self , sql , param_list ):
644635 # type: (CursorWrapper, Any, List[Any]) -> Any
645- hub = Hub .current
646- if hub .get_integration (DjangoIntegration ) is None :
647- return real_executemany (self , sql , param_list )
648-
649636 with record_sql_queries (
650- hub , self .cursor , sql , param_list , paramstyle = "format" , executemany = True
637+ self .cursor , sql , param_list , paramstyle = "format" , executemany = True
651638 ) as span :
652639 _set_db_data (span , self )
653640
654641 result = real_executemany (self , sql , param_list )
655642
656643 with capture_internal_exceptions ():
657- add_query_source (hub , span )
644+ add_query_source (span )
658645
659646 return result
660647
648+ @ensure_integration_enabled (DjangoIntegration , real_connect )
661649 def connect (self ):
662650 # type: (BaseDatabaseWrapper) -> None
663- hub = Hub .current
664- if hub .get_integration (DjangoIntegration ) is None :
665- return real_connect (self )
666-
667651 with capture_internal_exceptions ():
668- hub .add_breadcrumb (message = "connect" , category = "query" )
652+ sentry_sdk .add_breadcrumb (message = "connect" , category = "query" )
669653
670- with hub .start_span (op = OP .DB , description = "connect" ) as span :
654+ with sentry_sdk .start_span (op = OP .DB , description = "connect" ) as span :
671655 _set_db_data (span , self )
672656 return real_connect (self )
673657
@@ -679,7 +663,6 @@ def connect(self):
679663
680664def _set_db_data (span , cursor_or_db ):
681665 # type: (Span, Any) -> None
682-
683666 db = cursor_or_db .db if hasattr (cursor_or_db , "db" ) else cursor_or_db
684667 vendor = db .vendor
685668 span .set_data (SPANDATA .DB_SYSTEM , vendor )
0 commit comments