Skip to content

Commit 96c8d65

Browse files
committed
Refactoring
1 parent 20073a7 commit 96c8d65

File tree

2 files changed

+56
-38
lines changed

2 files changed

+56
-38
lines changed

sentry_sdk/api.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
from sentry_sdk.hub import Hub
55
from sentry_sdk.scope import Scope
66
from sentry_sdk.tracing import NoOpSpan, Transaction
7-
from sentry_sdk.tracing_utils import (
8-
has_tracing_enabled,
9-
normalize_incoming_data,
10-
)
117

128
if TYPE_CHECKING:
139
from typing import Any
@@ -254,47 +250,20 @@ def get_traceparent():
254250
"""
255251
Returns the traceparent either from the active span or from the scope.
256252
"""
257-
hub = Hub.current
258-
if hub.client is not None:
259-
if has_tracing_enabled(hub.client.options) and hub.scope.span is not None:
260-
return hub.scope.span.to_traceparent()
261-
262-
return hub.scope.get_traceparent()
253+
return Hub.current.get_traceparent()
263254

264255

265256
def get_baggage():
266257
# type: () -> Optional[str]
267258
"""
268259
Returns Baggage either from the active span or from the scope.
269260
"""
270-
hub = Hub.current
271-
if (
272-
hub.client is not None
273-
and has_tracing_enabled(hub.client.options)
274-
and hub.scope.span is not None
275-
):
276-
baggage = hub.scope.span.to_baggage()
277-
else:
278-
baggage = hub.scope.get_baggage()
279-
280-
if baggage is not None:
281-
return baggage.serialize()
282-
283-
return None
261+
return Hub.current.get_baggage()
284262

285263

286264
def continue_trace(environ_or_headers, op=None, name=None, source=None):
287265
# type: (Dict[str, Any], Optional[str], Optional[str], Optional[str]) -> Transaction
288266
"""
289267
Sets the propagation context from environment or headers and returns a transaction.
290268
"""
291-
with Hub.current.configure_scope() as scope:
292-
scope.generate_propagation_context(environ_or_headers)
293-
294-
transaction = Transaction.continue_from_headers(
295-
normalize_incoming_data(environ_or_headers),
296-
op=op,
297-
name=name,
298-
source=source,
299-
)
300-
return transaction
269+
return Hub.current.continue_trace(environ_or_headers, op, name, source)

sentry_sdk/hub.py

Lines changed: 53 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from datetime import datetime
55
from contextlib import contextmanager
66

7-
from sentry_sdk.api import get_traceparent, get_baggage
87
from sentry_sdk._compat import with_metaclass
98
from sentry_sdk.consts import INSTRUMENTER
109
from sentry_sdk.scope import Scope
@@ -18,7 +17,11 @@
1817
SENTRY_TRACE_HEADER_NAME,
1918
)
2019
from sentry_sdk.session import Session
21-
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+
2225
from sentry_sdk.utils import (
2326
exc_info_from_error,
2427
event_from_exception,
@@ -540,6 +543,22 @@ def start_transaction(
540543

541544
return transaction
542545

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+
543562
@overload
544563
def push_scope(
545564
self, callback=None # type: Optional[None]
@@ -706,6 +725,36 @@ def flush(
706725
if client is not None:
707726
return client.flush(timeout=timeout, callback=callback)
708727

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+
709758
def iter_trace_propagation_headers(self, span=None):
710759
# type: (Optional[Span]) -> Generator[Tuple[str, str], None, None]
711760
"""
@@ -740,14 +789,14 @@ def trace_propagation_meta(self, span=None):
740789

741790
meta = ""
742791

743-
sentry_trace = get_traceparent()
792+
sentry_trace = self.get_traceparent()
744793
if sentry_trace is not None:
745794
meta += '<meta name="%s" content="%s">' % (
746795
SENTRY_TRACE_HEADER_NAME,
747796
sentry_trace,
748797
)
749798

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

0 commit comments

Comments
 (0)