@@ -695,16 +695,13 @@ def __call__(
695695 self ._sem_conv_opt_in_mode ,
696696 )
697697 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 )
699699 except Exception as ex :
700700 if _report_new (self ._sem_conv_opt_in_mode ):
701701 req_attrs [ERROR_TYPE ] = type (ex ).__qualname__
702702 if span .is_recording ():
703703 span .set_attribute (ERROR_TYPE , type (ex ).__qualname__ )
704704 span .set_status (Status (StatusCode .ERROR , str (ex )))
705- span .end ()
706- if token is not None :
707- context .detach (token )
708705 raise
709706 finally :
710707 duration_s = default_timer () - start
@@ -723,13 +720,16 @@ def __call__(
723720 max (duration_s , 0 ), duration_attrs_new
724721 )
725722 self .active_requests_counter .add (- 1 , active_requests_count_attrs )
723+ span .end ()
724+ if token is not None :
725+ context .detach (token )
726726
727727
728728# Put this in a subfunction to not delay the call to the wrapped
729729# WSGI application (instrumentation should change the application
730730# 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
733733) -> Iterable [T ]:
734734 try :
735735 with trace .use_span (span ):
@@ -738,9 +738,6 @@ def _end_span_after_iterating(
738738 close = getattr (iterable , "close" , None )
739739 if close :
740740 close ()
741- span .end ()
742- if token is not None :
743- context .detach (token )
744741
745742
746743# TODO: inherit from opentelemetry.instrumentation.propagators.Setter
0 commit comments