@@ -695,16 +695,13 @@ def __call__(
695
695
self ._sem_conv_opt_in_mode ,
696
696
)
697
697
iterable = self .wsgi (environ , start_response )
698
- return _end_span_after_iterating (iterable , span , token )
698
+ return _iterate_and_close_with_span (iterable , span , token )
699
699
except Exception as ex :
700
700
if _report_new (self ._sem_conv_opt_in_mode ):
701
701
req_attrs [ERROR_TYPE ] = type (ex ).__qualname__
702
702
if span .is_recording ():
703
703
span .set_attribute (ERROR_TYPE , type (ex ).__qualname__ )
704
704
span .set_status (Status (StatusCode .ERROR , str (ex )))
705
- span .end ()
706
- if token is not None :
707
- context .detach (token )
708
705
raise
709
706
finally :
710
707
duration_s = default_timer () - start
@@ -723,13 +720,16 @@ def __call__(
723
720
max (duration_s , 0 ), duration_attrs_new
724
721
)
725
722
self .active_requests_counter .add (- 1 , active_requests_count_attrs )
723
+ span .end ()
724
+ if token is not None :
725
+ context .detach (token )
726
726
727
727
728
728
# Put this in a subfunction to not delay the call to the wrapped
729
729
# WSGI application (instrumentation should change the application
730
730
# behavior as little as possible).
731
- def _end_span_after_iterating (
732
- iterable : Iterable [T ], span : trace .Span , token : object
731
+ def _iterate_and_close_with_span (
732
+ iterable : Iterable [T ], span : trace .Span
733
733
) -> Iterable [T ]:
734
734
try :
735
735
with trace .use_span (span ):
@@ -738,9 +738,6 @@ def _end_span_after_iterating(
738
738
close = getattr (iterable , "close" , None )
739
739
if close :
740
740
close ()
741
- span .end ()
742
- if token is not None :
743
- context .detach (token )
744
741
745
742
746
743
# TODO: inherit from opentelemetry.instrumentation.propagators.Setter
0 commit comments