@@ -711,6 +711,14 @@ def finish(self, scope=None, end_timestamp=None):
711711 scope = scope or sentry_sdk .get_current_scope ()
712712 maybe_create_breadcrumbs_from_span (scope , self )
713713
714+ client = sentry_sdk .get_client ()
715+ if client .is_active ():
716+ if (
717+ has_span_streaming_enabled (client .options )
718+ and self .containing_transaction .sampled
719+ ):
720+ client ._span_batcher .add (self )
721+
714722 return None
715723
716724 def to_json (self ):
@@ -857,6 +865,12 @@ def __init__( # type: ignore[misc]
857865 else :
858866 self ._sample_rand = _generate_sample_rand (self .trace_id )
859867
868+ self ._mode = "static"
869+ client = sentry_sdk .get_client ()
870+ if client .is_active ():
871+ if has_span_streaming_enabled (client .options ):
872+ self ._mode = "stream"
873+
860874 def __repr__ (self ):
861875 # type: () -> str
862876 return (
@@ -882,9 +896,11 @@ def _possibly_started(self):
882896 with sentry_sdk.start_transaction, and therefore the transaction will
883897 be discarded.
884898 """
885-
886- # We must explicitly check self.sampled is False since self.sampled can be None
887- return self ._span_recorder is not None or self .sampled is False
899+ if self ._mode == "static" :
900+ # We must explicitly check self.sampled is False since self.sampled can be None
901+ return self ._span_recorder is not None or self .sampled is False
902+ else :
903+ return True
888904
889905 def __enter__ (self ):
890906 # type: () -> Transaction
@@ -972,7 +988,8 @@ def finish(
972988 ):
973989 # type: (...) -> Optional[str]
974990 """Finishes the transaction and sends it to Sentry.
975- All finished spans in the transaction will also be sent to Sentry.
991+ If we're in non-streaming mode, all finished spans in the transaction
992+ will also be sent to Sentry at this point.
976993
977994 :param scope: The Scope to use for this transaction.
978995 If not provided, the current Scope will be used.
@@ -1000,7 +1017,7 @@ def finish(
10001017 # We have no active client and therefore nowhere to send this transaction.
10011018 return None
10021019
1003- if self ._span_recorder is None :
1020+ if self ._mode == "static" and self . _span_recorder is None :
10041021 # Explicit check against False needed because self.sampled might be None
10051022 if self .sampled is False :
10061023 logger .debug ("Discarding transaction because sampled = False" )
@@ -1482,5 +1499,6 @@ def calculate_interest_rate(amount, rate, years):
14821499 extract_sentrytrace_data ,
14831500 _generate_sample_rand ,
14841501 has_tracing_enabled ,
1502+ has_span_streaming_enabled ,
14851503 maybe_create_breadcrumbs_from_span ,
14861504)
0 commit comments