99from sentry_sdk .scope import Scope
1010from sentry_sdk .client import Client
1111from sentry_sdk .profiler import Profile
12- from sentry_sdk .tracing import NoOpSpan , Span , Transaction
12+ from sentry_sdk .tracing import (
13+ NoOpSpan ,
14+ Span ,
15+ Transaction ,
16+ BAGGAGE_HEADER_NAME ,
17+ SENTRY_TRACE_HEADER_NAME ,
18+ )
1319from sentry_sdk .session import Session
14- from sentry_sdk .tracing_utils import has_tracing_enabled
20+ from sentry_sdk .tracing_utils import (
21+ has_tracing_enabled ,
22+ normalize_incoming_data ,
23+ )
24+
1525from sentry_sdk .utils import (
1626 exc_info_from_error ,
1727 event_from_exception ,
@@ -533,6 +543,22 @@ def start_transaction(
533543
534544 return transaction
535545
546+ def continue_trace (self , environ_or_headers , op = None , name = None , source = None ):
547+ # type: (Dict[str, Any], Optional[str], Optional[str], Optional[str]) -> Transaction
548+ """
549+ Sets the propagation context from environment or headers and returns a transaction.
550+ """
551+ with self .configure_scope () as scope :
552+ scope .generate_propagation_context (environ_or_headers )
553+
554+ transaction = Transaction .continue_from_headers (
555+ normalize_incoming_data (environ_or_headers ),
556+ op = op ,
557+ name = name ,
558+ source = source ,
559+ )
560+ return transaction
561+
536562 @overload
537563 def push_scope (
538564 self , callback = None # type: Optional[None]
@@ -699,6 +725,36 @@ def flush(
699725 if client is not None :
700726 return client .flush (timeout = timeout , callback = callback )
701727
728+ def get_traceparent (self ):
729+ # type: () -> Optional[str]
730+ """
731+ Returns the traceparent either from the active span or from the scope.
732+ """
733+ if self .client is not None :
734+ if has_tracing_enabled (self .client .options ) and self .scope .span is not None :
735+ return self .scope .span .to_traceparent ()
736+
737+ return self .scope .get_traceparent ()
738+
739+ def get_baggage (self ):
740+ # type: () -> Optional[str]
741+ """
742+ Returns Baggage either from the active span or from the scope.
743+ """
744+ if (
745+ self .client is not None
746+ and has_tracing_enabled (self .client .options )
747+ and self .scope .span is not None
748+ ):
749+ baggage = self .scope .span .to_baggage ()
750+ else :
751+ baggage = self .scope .get_baggage ()
752+
753+ if baggage is not None :
754+ return baggage .serialize ()
755+
756+ return None
757+
702758 def iter_trace_propagation_headers (self , span = None ):
703759 # type: (Optional[Span]) -> Generator[Tuple[str, str], None, None]
704760 """
@@ -723,13 +779,26 @@ def iter_trace_propagation_headers(self, span=None):
723779 def trace_propagation_meta (self , span = None ):
724780 # type: (Optional[Span]) -> str
725781 """
726- Return meta tags which should be injected into the HTML template
727- to allow propagation of trace data .
782+ Return meta tags which should be injected into HTML templates
783+ to allow propagation of trace information .
728784 """
785+ if span is None :
786+ logger .warning (
787+ "The parameter `span` in trace_propagation_meta() is deprecated and will be removed in the future."
788+ )
789+
729790 meta = ""
730791
731- for name , content in self .iter_trace_propagation_headers (span ):
732- meta += '<meta name="%s" content="%s">' % (name , content )
792+ sentry_trace = self .get_traceparent ()
793+ if sentry_trace is not None :
794+ meta += '<meta name="%s" content="%s">' % (
795+ SENTRY_TRACE_HEADER_NAME ,
796+ sentry_trace ,
797+ )
798+
799+ baggage = self .get_baggage ()
800+ if baggage is not None :
801+ meta += '<meta name="%s" content="%s">' % (BAGGAGE_HEADER_NAME , baggage )
733802
734803 return meta
735804
0 commit comments