1111from sentry_sdk .profiler import Profile
1212from sentry_sdk .tracing import NoOpSpan , Span , Transaction
1313from sentry_sdk .session import Session
14+ from sentry_sdk .tracing_utils import has_tracing_enabled
1415from sentry_sdk .utils import (
1516 exc_info_from_error ,
1617 event_from_exception ,
@@ -322,14 +323,8 @@ def bind_client(
322323 top = self ._stack [- 1 ]
323324 self ._stack [- 1 ] = (new , top [1 ])
324325
325- def capture_event (
326- self ,
327- event , # type: Event
328- hint = None , # type: Optional[Hint]
329- scope = None , # type: Optional[Any]
330- ** scope_args # type: Any
331- ):
332- # type: (...) -> Optional[str]
326+ def capture_event (self , event , hint = None , scope = None , ** scope_args ):
327+ # type: (Event, Optional[Hint], Optional[Scope], Any) -> Optional[str]
333328 """Captures an event. Alias of :py:meth:`sentry_sdk.Client.capture_event`."""
334329 client , top_scope = self ._stack [- 1 ]
335330 scope = _update_scope (top_scope , scope , scope_args )
@@ -341,14 +336,8 @@ def capture_event(
341336 return rv
342337 return None
343338
344- def capture_message (
345- self ,
346- message , # type: str
347- level = None , # type: Optional[str]
348- scope = None , # type: Optional[Any]
349- ** scope_args # type: Any
350- ):
351- # type: (...) -> Optional[str]
339+ def capture_message (self , message , level = None , scope = None , ** scope_args ):
340+ # type: (str, Optional[str], Optional[Scope], Any) -> Optional[str]
352341 """Captures a message. The message is just a string. If no level
353342 is provided the default level is `info`.
354343
@@ -362,13 +351,8 @@ def capture_message(
362351 {"message" : message , "level" : level }, scope = scope , ** scope_args
363352 )
364353
365- def capture_exception (
366- self ,
367- error = None , # type: Optional[Union[BaseException, ExcInfo]]
368- scope = None , # type: Optional[Any]
369- ** scope_args # type: Any
370- ):
371- # type: (...) -> Optional[str]
354+ def capture_exception (self , error = None , scope = None , ** scope_args ):
355+ # type: (Optional[Union[BaseException, ExcInfo]], Optional[Scope], Any) -> Optional[str]
372356 """Captures an exception.
373357
374358 :param error: An exception to catch. If `None`, `sys.exc_info()` will be used.
@@ -403,13 +387,8 @@ def _capture_internal_exception(
403387 """
404388 logger .error ("Internal error in sentry_sdk" , exc_info = exc_info )
405389
406- def add_breadcrumb (
407- self ,
408- crumb = None , # type: Optional[Breadcrumb]
409- hint = None , # type: Optional[BreadcrumbHint]
410- ** kwargs # type: Any
411- ):
412- # type: (...) -> None
390+ def add_breadcrumb (self , crumb = None , hint = None , ** kwargs ):
391+ # type: (Optional[Breadcrumb], Optional[BreadcrumbHint], Any) -> None
413392 """
414393 Adds a breadcrumb.
415394
@@ -449,13 +428,8 @@ def add_breadcrumb(
449428 while len (scope ._breadcrumbs ) > max_breadcrumbs :
450429 scope ._breadcrumbs .popleft ()
451430
452- def start_span (
453- self ,
454- span = None , # type: Optional[Span]
455- instrumenter = INSTRUMENTER .SENTRY , # type: str
456- ** kwargs # type: Any
457- ):
458- # type: (...) -> Span
431+ def start_span (self , span = None , instrumenter = INSTRUMENTER .SENTRY , ** kwargs ):
432+ # type: (Optional[Span], str, Any) -> Span
459433 """
460434 Create and start timing a new span whose parent is the currently active
461435 span or transaction, if any. The return value is a span instance,
@@ -500,12 +474,9 @@ def start_span(
500474 return Span (** kwargs )
501475
502476 def start_transaction (
503- self ,
504- transaction = None , # type: Optional[Transaction]
505- instrumenter = INSTRUMENTER .SENTRY , # type: str
506- ** kwargs # type: Any
477+ self , transaction = None , instrumenter = INSTRUMENTER .SENTRY , ** kwargs
507478 ):
508- # type: (... ) -> Union[Transaction, NoOpSpan]
479+ # type: (Optional[Transaction], str, Any ) -> Union[Transaction, NoOpSpan]
509480 """
510481 Start and return a transaction.
511482
@@ -577,7 +548,9 @@ def push_scope( # noqa: F811
577548 pass
578549
579550 def push_scope ( # noqa
580- self , callback = None # type: Optional[Callable[[Scope], None]]
551+ self ,
552+ callback = None , # type: Optional[Callable[[Scope], None]]
553+ continue_trace = True , # type: bool
581554 ):
582555 # type: (...) -> Optional[ContextManager[Scope]]
583556 """
@@ -595,7 +568,13 @@ def push_scope( # noqa
595568 return None
596569
597570 client , scope = self ._stack [- 1 ]
598- new_layer = (client , copy .copy (scope ))
571+
572+ new_scope = copy .copy (scope )
573+
574+ if continue_trace :
575+ new_scope .generate_propagation_context ()
576+
577+ new_layer = (client , new_scope )
599578 self ._stack .append (new_layer )
600579
601580 return _ScopeManager (self )
@@ -626,7 +605,9 @@ def configure_scope( # noqa: F811
626605 pass
627606
628607 def configure_scope ( # noqa
629- self , callback = None # type: Optional[Callable[[Scope], None]]
608+ self ,
609+ callback = None , # type: Optional[Callable[[Scope], None]]
610+ continue_trace = True , # type: bool
630611 ):
631612 # type: (...) -> Optional[ContextManager[Scope]]
632613
@@ -639,6 +620,10 @@ def configure_scope( # noqa
639620 """
640621
641622 client , scope = self ._stack [- 1 ]
623+
624+ if continue_trace :
625+ scope .generate_propagation_context ()
626+
642627 if callback is not None :
643628 if client is not None :
644629 callback (scope )
@@ -721,18 +706,19 @@ def iter_trace_propagation_headers(self, span=None):
721706 from the span representing the request, if available, or the current
722707 span on the scope if not.
723708 """
724- span = span or self .scope .span
725- if not span :
726- return
727-
728709 client = self ._stack [- 1 ][0 ]
729-
730710 propagate_traces = client and client .options ["propagate_traces" ]
731711 if not propagate_traces :
732712 return
733713
734- for header in span .iter_headers ():
735- yield header
714+ span = span or self .scope .span
715+
716+ if client and has_tracing_enabled (client .options ) and span is not None :
717+ for header in span .iter_headers ():
718+ yield header
719+ else :
720+ for header in self .scope .iter_headers ():
721+ yield header
736722
737723 def trace_propagation_meta (self , span = None ):
738724 # type: (Optional[Span]) -> str
0 commit comments