@@ -152,15 +152,33 @@ def __call__(self, request):
152152        if  excluded_url  or  is_asgi_request  and  not  _is_asgi_supported :
153153            return  self .get_response (request )
154154
155+         # the request creates an activation and potentially a token 
156+         # to clean up 
157+         self .process_request (request )
158+         activation  =  request .META [self ._environ_activation_key ]
155159        try :
156-             self .process_request (request )
160+             activation .__enter__ ()
161+             self ._run_request_hook (request )
157162            response  =  self .get_response (request )
158163            return  self .process_response (request , response )
159164        finally :
165+ 
160166            if  request .META .get (self ._environ_token , None ) is  not None :
161167                detach (request .META .get (self ._environ_token ))
162168                request .META .pop (self ._environ_token )
163169
170+     def  _run_request_hook (self , request ):
171+         span  =  request .META [self ._environ_span_key ]
172+         if  _DjangoMiddleware ._otel_request_hook :
173+             try :
174+                 _DjangoMiddleware ._otel_request_hook (  # pylint: disable=not-callable 
175+                     span , request 
176+                 )
177+             except  Exception :  # pylint: disable=broad-exception-caught 
178+                 # Raising an exception here would leak the request span since process_response 
179+                 # would not be called. Log the exception instead. 
180+                 _logger .exception ("Exception raised by request_hook" )
181+ 
164182    @staticmethod  
165183    def  _get_span_name (request ):
166184        method  =  sanitize_method (request .method .strip ())
@@ -274,24 +292,13 @@ def process_request(self, request):
274292                span .set_attribute (key , value )
275293
276294        activation  =  use_span (span , end_on_exit = True )
277-         activation .__enter__ ()  # pylint: disable=E1101 
278295        request_start_time  =  default_timer ()
279296        request .META [self ._environ_timer_key ] =  request_start_time 
280297        request .META [self ._environ_activation_key ] =  activation 
281298        request .META [self ._environ_span_key ] =  span 
282299        if  token :
283300            request .META [self ._environ_token ] =  token 
284301
285-         if  _DjangoMiddleware ._otel_request_hook :
286-             try :
287-                 _DjangoMiddleware ._otel_request_hook (  # pylint: disable=not-callable 
288-                     span , request 
289-                 )
290-             except  Exception :  # pylint: disable=broad-exception-caught 
291-                 # Raising an exception here would leak the request span since process_response 
292-                 # would not be called. Log the exception instead. 
293-                 _logger .exception ("Exception raised by request_hook" )
294- 
295302    # pylint: disable=unused-argument 
296303    def  process_view (self , request , view_func , * args , ** kwargs ):
297304        # Process view is executed before the view function, here we get the 
0 commit comments