Skip to content

Commit

Permalink
Use new top level api in trace_propagation_meta (#2202)
Browse files Browse the repository at this point in the history
Use new top level api in trace_propagation_meta and also move the functions into the Hub, so they can be used in the Hub. (following the pattern of other top level API)

Refs #2186
  • Loading branch information
antonpirker authored Jun 28, 2023
1 parent d3f9568 commit d4ecab3
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 40 deletions.
37 changes: 3 additions & 34 deletions sentry_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
from sentry_sdk.hub import Hub
from sentry_sdk.scope import Scope
from sentry_sdk.tracing import NoOpSpan, Transaction
from sentry_sdk.tracing_utils import (
has_tracing_enabled,
normalize_incoming_data,
)

if TYPE_CHECKING:
from typing import Any
Expand Down Expand Up @@ -254,47 +250,20 @@ def get_traceparent():
"""
Returns the traceparent either from the active span or from the scope.
"""
hub = Hub.current
if hub.client is not None:
if has_tracing_enabled(hub.client.options) and hub.scope.span is not None:
return hub.scope.span.to_traceparent()

return hub.scope.get_traceparent()
return Hub.current.get_traceparent()


def get_baggage():
# type: () -> Optional[str]
"""
Returns Baggage either from the active span or from the scope.
"""
hub = Hub.current
if (
hub.client is not None
and has_tracing_enabled(hub.client.options)
and hub.scope.span is not None
):
baggage = hub.scope.span.to_baggage()
else:
baggage = hub.scope.get_baggage()

if baggage is not None:
return baggage.serialize()

return None
return Hub.current.get_baggage()


def continue_trace(environ_or_headers, op=None, name=None, source=None):
# type: (Dict[str, Any], Optional[str], Optional[str], Optional[str]) -> Transaction
"""
Sets the propagation context from environment or headers and returns a transaction.
"""
with Hub.current.configure_scope() as scope:
scope.generate_propagation_context(environ_or_headers)

transaction = Transaction.continue_from_headers(
normalize_incoming_data(environ_or_headers),
op=op,
name=name,
source=source,
)
return transaction
return Hub.current.continue_trace(environ_or_headers, op, name, source)
81 changes: 75 additions & 6 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@
from sentry_sdk.scope import Scope
from sentry_sdk.client import Client
from sentry_sdk.profiler import Profile
from sentry_sdk.tracing import NoOpSpan, Span, Transaction
from sentry_sdk.tracing import (
NoOpSpan,
Span,
Transaction,
BAGGAGE_HEADER_NAME,
SENTRY_TRACE_HEADER_NAME,
)
from sentry_sdk.session import Session
from sentry_sdk.tracing_utils import has_tracing_enabled
from sentry_sdk.tracing_utils import (
has_tracing_enabled,
normalize_incoming_data,
)

from sentry_sdk.utils import (
exc_info_from_error,
event_from_exception,
Expand Down Expand Up @@ -533,6 +543,22 @@ def start_transaction(

return transaction

def continue_trace(self, environ_or_headers, op=None, name=None, source=None):
# type: (Dict[str, Any], Optional[str], Optional[str], Optional[str]) -> Transaction
"""
Sets the propagation context from environment or headers and returns a transaction.
"""
with self.configure_scope() as scope:
scope.generate_propagation_context(environ_or_headers)

transaction = Transaction.continue_from_headers(
normalize_incoming_data(environ_or_headers),
op=op,
name=name,
source=source,
)
return transaction

@overload
def push_scope(
self, callback=None # type: Optional[None]
Expand Down Expand Up @@ -699,6 +725,36 @@ def flush(
if client is not None:
return client.flush(timeout=timeout, callback=callback)

def get_traceparent(self):
# type: () -> Optional[str]
"""
Returns the traceparent either from the active span or from the scope.
"""
if self.client is not None:
if has_tracing_enabled(self.client.options) and self.scope.span is not None:
return self.scope.span.to_traceparent()

return self.scope.get_traceparent()

def get_baggage(self):
# type: () -> Optional[str]
"""
Returns Baggage either from the active span or from the scope.
"""
if (
self.client is not None
and has_tracing_enabled(self.client.options)
and self.scope.span is not None
):
baggage = self.scope.span.to_baggage()
else:
baggage = self.scope.get_baggage()

if baggage is not None:
return baggage.serialize()

return None

def iter_trace_propagation_headers(self, span=None):
# type: (Optional[Span]) -> Generator[Tuple[str, str], None, None]
"""
Expand All @@ -723,13 +779,26 @@ def iter_trace_propagation_headers(self, span=None):
def trace_propagation_meta(self, span=None):
# type: (Optional[Span]) -> str
"""
Return meta tags which should be injected into the HTML template
to allow propagation of trace data.
Return meta tags which should be injected into HTML templates
to allow propagation of trace information.
"""
if span is None:
logger.warning(
"The parameter `span` in trace_propagation_meta() is deprecated and will be removed in the future."
)

meta = ""

for name, content in self.iter_trace_propagation_headers(span):
meta += '<meta name="%s" content="%s">' % (name, content)
sentry_trace = self.get_traceparent()
if sentry_trace is not None:
meta += '<meta name="%s" content="%s">' % (
SENTRY_TRACE_HEADER_NAME,
sentry_trace,
)

baggage = self.get_baggage()
if baggage is not None:
meta += '<meta name="%s" content="%s">' % (BAGGAGE_HEADER_NAME, baggage)

return meta

Expand Down

0 comments on commit d4ecab3

Please sign in to comment.