Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
antonpirker committed Jun 27, 2023
1 parent 20073a7 commit 96c8d65
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 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)
57 changes: 53 additions & 4 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from datetime import datetime
from contextlib import contextmanager

from sentry_sdk.api import get_traceparent, get_baggage
from sentry_sdk._compat import with_metaclass
from sentry_sdk.consts import INSTRUMENTER
from sentry_sdk.scope import Scope
Expand All @@ -18,7 +17,11 @@
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 @@ -540,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 @@ -706,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 Down Expand Up @@ -740,14 +789,14 @@ def trace_propagation_meta(self, span=None):

meta = ""

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

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

Expand Down

0 comments on commit 96c8d65

Please sign in to comment.