Skip to content

Commit d4ecab3

Browse files
authored
Use new top level api in trace_propagation_meta (#2202)
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
1 parent d3f9568 commit d4ecab3

2 files changed

Lines changed: 78 additions & 40 deletions

File tree

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: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,19 @@
99
from sentry_sdk.scope import Scope
1010
from sentry_sdk.client import Client
1111
from 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+
)
1319
from 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+
1525
from 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

Comments
 (0)